一度SSH接続した後に、OSを再インストールして繋ぎなおすと REMOTE HOST IDENTIFICATION HAS CHANGED
のWARNINGが出て、接続できなくなる
$ ssh localhost
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ 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:x+3G7YmVt74mJbrp2YDCdo9N5uv4Q9fb4m2U2jzYBNA.
Please contact your system administrator.
Add correct host key in /home/suzutsuki/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /home/suzutsuki/.ssh/known_hosts:7
ECDSA host key for localhost has changed and you have requested strict checking.
Host key verification failed.
これは、SSHクライアントが前回繋いだホストとは異なっていることをユーザーに知らせるためのもので、偽装ホストへの接続を防ぐための仕組みである
ssh-keygen -R HOST_NAME
で ~/.ssh/known_hosts
にキャッシュされているフィンガープリントを削除することで解決するが、VM や Dockerコンテナのように頻繁に作り直すホストに対して毎回行うのは煩わしい
/etc/ssh/ssh_config
で StrictHostKeyChecking
を no
に設定すればフィンガープリントの確認が省略できるが、全てのホストに対して無効にするのはセキュリティーレベルを緩くしすぎである
そこで、特定のホスト(今回はlocalhostのみ)のSSH接続だけをチェックしないように設定したい
~/.ssh/config
を開き、以下のように設定を記述する
Host localhost
StrictHostKeyChecking no
Hostには *
(0 個または以上の任意の文字に一致する)や ?
(任意の 1 文字だけに一致する) も使える
複数のホストを書くには半角スペースで区切る
Host host1 host2 ...
設定した後は、WARNINGは出るが接続できるようになる
$ ssh localhost
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ 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:MJJBD6MuXKz1tQ13ADlJfNZp4au6KqaY61L7BCtlHiU.
Please contact your system administrator.
Add correct host key in /home/suzutsuki/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /home/suzutsuki/.ssh/known_hosts:7
Password authentication is disabled to avoid man-in-the-middle attacks.
Keyboard-interactive authentication is disabled to avoid man-in-the-middle attacks.