開発

EC2でAMIを変更した際、SSH接続できなくなる件の解消法

状況分析

  • EC2でAMIを変更した際、SSH接続できなくなった

AWSでStrapiを使おうと思って四苦八苦していたのですが、そもそもAmazoon LinuxのAMIがサポート対象外であることが判明しました。そこで、EC2インスタンスを新たに作成(今回はAMIをubuntuに)してElastic IPの関連付けを切り替えてターミナルからSSH接続を試みたのですが、接続できませんでした。

エラー内容

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: POSSIBLE DNS SPOOFING DETECTED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
The ECDSA host key for ec2-xx-xx-xx-xx.ap-northeast-1.compute.amazonaws.com has changed,
and the key for the corresponding IP address xx.xx.xx.xx
is unchanged. This could either mean that
DNS SPOOFING is happening or the IP address for the host
and its host key have changed at the same time.
Offending key for IP in /Users/apple/.ssh/known_hosts:28
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
Please contact your system administrator.
Add correct host key in /Users/apple/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /Users/apple/.ssh/known_hosts:27
ECDSA host key for ec2-xx-xx-xx-xx.ap-northeast-1.compute.amazonaws.com has changed and you have requested strict checking.
Host key verification failed.

中間者攻撃(Man in the middle(MITM)攻撃)の可能性があるから確認してね。ということみたいです。

解決の方向性

エラー文に書かれている通り/Users/apple/.ssh/known_hostsファイルを修正することで解消します。

参照:How to fix: "WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED" on Mac and Linux

解消方法

  • エラー箇所の削除

/Users/apple/.ssh/known_hostsファイル該当箇所を削除したところ、問題なくSSH接続できるようになっていました。前のインスタンスに接続する際に使用していたSSH接続用のfingerprintが残っており、同じパブリックIPでの接続であったことで、この記述がエラーを引き起こしたようです。