known_hostsを手動で編集しなくてもssh-keygen -R hostname で良かった / sshのremote hostが変更になってsshでエラーが出るとき

仮想マシンを作っては壊して、などをやっていると良く起こる。仮想マシンじゃなくても物理サーバが壊れて、IPを引き継いで復旧させた場合なども同様。

clinetのknown_hostsに登録されているものと、remote hostのsshdの認証鍵が変わっていて、中間者攻撃の可能性があるで、と怒られる。

$ ssh 192.168.0.10
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    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 the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
11:59:12:23:54:7a:fc:d5:ff:59:ec:3b:4f:dd:8d:1c.
Please contact your system administrator.
Add correct host key in /home/hogem/.ssh/known_hosts to get rid of this message.
Offending key in /home/hogem/.ssh/known_hosts:111
Password authentication is disabled to avoid man-in-the-middle attacks.
Keyboard-interactive authentication is disabled to avoid man-in-the-middle attacks.

この例だとknown_hostsの111行目がおかしいので、今までは vim ~/.ssh/known_hosts でそこを消してたんだけど、ssh-keygen -R hostname するだけで良かったやないか。

## ssh-keygen -R hostname # Remove host from known_hosts file.
$ ssh-keygen -R 192.168.0.10
$ ssh 192.168.0.10