意気揚々とawsのec2に完璧なまでに環境構築を行い全てが準備万端で、キーペアを.sshに置いて.pemファイルを利用していざssh接続してログインしようとした時にそれは起こりました。
コンテンツ
ssh接続で拒否される
.sshディレクトリで、key.pemファイルを利用して、ssh -i key.pem ec2-user@xx.xx.xxx.xxxとsshログインを試みます。
sample@MacBook-Pro .ssh % ssh -i key.pem ec2-user@xx.xx.xxx.xxx
The authenticity of host 'xx.xx.xxx.xxx (xx.xx.xxx.xxx)' can't be established.
ECDSA key fingerprint is SHA256:Uig+oFqHSzSpruf8VTF3GoYdhCy12d+JtNMs3SvZRlM.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'xx.xx.xxx.xxx' (ECDSA) to the list of known hosts.
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: UNPROTECTED PRIVATE KEY FILE! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0644 for 'key.pem' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key "key.pem": bad permissions
ec2-user@xx.xx.xxx.xxx: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
なんでじゃー!!
ファイルの権限確認
てことでまず気になったのは、key.pemファイルの権限です。
key.pemファイルの権限をlsで確認してみます。
sample@MacBook-Pro .ssh % ls -l
total 16
-rw-r--r--@ 1 sample staff 1678 1 15 21:39 key.pem
-rw-r--r-- 1 sample staff 175 1 15 22:30 known_hosts
違う!!
権限が600じゃない!!
基本的に、.pemファイルのような秘密鍵は秘密鍵だけに権限も十全に割り振る必要があります。
ファイル権限の修正
ということで権限を600に修正してみます。
sample@MacBook-Pro .ssh % chmod 600 key.pem
準備ができたので、再度のsshログインを実行します。
sample@MacBook-Pro .ssh % ssh -i key.pem ec2-user@xx.xx.xxx.xxx
__| __|_ )
_| ( / Amazon Linux 2 AMI
___|\___|___|
https://aws.amazon.com/amazon-linux-2/
[ec2-user@ip-10-0-0-84 ~]$ Connection to xx.xx.xxx.xxx closed by remote host.
無事ログインすることができました!!
以上。
最後に
いかがでしたでしょうか。
以上が、「【AWS】初めてのキーペアを使ったssh接続でいきなり拒否される」の紹介記事になります。




コメントを残す