LoginSignup
1
0

More than 5 years have passed since last update.

sshログインに失敗する

Last updated at Posted at 2018-10-24

初歩的なことだが、戒めメモ
AWSのEC2インスタンスにログインしようとすると失敗した

$ ssh hogehoge@ipアドレス
Permission denied (publickey).

あれ?

$ ls -lah ~/.ssh
-rw-------@  1 hogehoge  hogehoge   1.7K 10 24 15:50 hogehoge.pem

権限はちゃんと「600」になっている...
まさかディレクトリか? いや、今更そんなわけ... でも一応確認

$ ls -lah ~
drwx------   10 hogehoge  hogehoge   340B 10 24 15:59 .ssh

「700」問題ない...

解決
簡単なことでした

$ ssh -i .ssh/hogehoge.pem hogehoge@ipアドレス

でも普段(ローカル環境ログイン時)はわざわざ指定しなくても読み込んでくれているような...
※後ほど勉強して追記予定

2018/10/27追記

sshの設定値を確認する

$ ssh -G hogehoge@ipアドレス

user hogehoge
hostname ipアドレス
port 22
...他にもいっぱい...
identityfile ~/.ssh/id_rsa
identityfile ~/.ssh/id_dsa
identityfile ~/.ssh/id_ecdsa
identityfile ~/.ssh/id_ed25519

秘密鍵は「~/.ssh/id_rsa」などを使用するというのがデフォルトの設定になっている

と、いうわけで
configファイルを用意して、設定を記載すればオプションで秘密鍵を指定せずにログインできました

$ cd ~/.ssh
$ vi config

今回の私の例でいくと最低限必要だったのは3種類

Host hogehoge
    HostName ipアドレス
    user hogehoge
    IdentityFile ~/.ssh/hogehoge.pem

再度、sshの設定値を確認
意図した設定になっていました

$ ssh -G hogehoge

user hogehoge
hostname ipアドレス
port 22
identityfile ~/.ssh/hogehoge.pem

今まで特に指定せずにログインできていたのは
・全てのローカル環境に同一の公開鍵を登録していたから
・接続元の秘密鍵のファイル名が「id_rsa」
・sshコマンドはデフォルトで「~/.ssh/id_rsa」を使用する (他にも3種)
ということでした

@forl_head_officer
@angel_p_57
ご教示いただきありがとうございました

参考

1
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0