mochikoAsTechのdig日記

当方好きなコマンドはdigです!お友達から!!よろしくお願いします!!!

CentOSにSSHでログインしようとすると「WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!」と出てログインできなかった

自宅にあるDELLのマシンにCentOSを入れて、同じく自宅のMacBookから鍵認証でログインできるように設定してから数週間・・・同じ鍵で同じように接続しようとするとエラーが!

$ ssh -i 秘密鍵のフルパス ユーザ名@192.168.1.**

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ 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
d7:93:6f:04:f6:3f:89:02:**:**:**:**:**:**:87:69. ←フィンガープリント
Please contact your system administrator.
Add correct host key in /Users/ユーザ名/.ssh/known_hosts to get rid of this message.
Offending key in /Users/ユーザ名/.ssh/known_hosts:1
RSA host key for 192.168.1.xx has changed and you have requested strict checking.

Host key verification failed.

えー、公開鍵も秘密鍵もなんにも設定変更してないよー?と思い、DELLのマシンでもターミナルを開いて、

tailf /var/log/secure

を見てみると、

Aug 21 22:50:08 ホスト名 sshd[2912]: Connection closed by 192.168.1.**

うーん・・・?

もう一度、MacBookのターミナルで出たエラーメッセージをよく読んでみる。前回のログインと今回のログインで違うのは、DHCPIPアドレスを割り振ってるので、接続先のCentOSDELL)のIPアドレスが違う、という点。それで「MacBookの.ssh/known_hostsに書いてある初回のフィンガープリントと、今回接続しようとして受け取ったフィンガープリントが違うよ!これ前回と違うサーバじゃない?騙されてない?接続しない方がいいって!」となってる模様。

じゃあknown_hostsの該当箇所をさくっと消せばいいの?と思ったけど、調べたらssh-keygenコマンドに専用のオプションがあったー!

$ man ssh-keygen

ssh-keygen -R hostname [-f known_hosts_file]
(中略)
-R hostname
Removes all keys belonging to hostname from a known_hosts file.
This option is useful to delete hashed hosts (see the -H option above).

という訳で、MacBookのターミナルで

$ ssh-keygen -R 192.168.1.**
/Users/ユーザ名/.ssh/known_hosts updated.
Original contents retained as /Users/ユーザ名/.ssh/known_hosts.old

としてみた後に、再度接続を試す。

$ ssh -i 秘密鍵のフルパス ユーザ名@192.168.1.**
The authenticity of host '192.168.1.** (192.168.1.**)' can't be established.
RSA key fingerprint is d7:93:6f:04:f6:3f:89:02:ca:**:**:**:58:7a:87:69.
Are you sure you want to continue connecting (yes/no)? yes

Warning: Permanently added '192.168.1.14' (RSA) to the list of known hosts.

Identity added: 秘密鍵 (秘密鍵)
Last login: Fri Jun 28 23:47:54 2013 from 192.168.1.**

ログインできたー!(そして暫く接続してなかったことが分かった・・・)

ああ、またも「エラーメッセージをよく読め」って話だった。反省。