LoginSignup
219
175

More than 1 year has passed since last update.

SSH接続で WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! って言われて接続を拒否られるとき

Last updated at Posted at 2013-08-14

About

(2020-03-04)更新 最近の OpenSSH クライアントであれば自動的に消すかどうか聞いてくれるのでこの記事の必要性はあんまりなくなって気がします。
(2020-03-04)更新 毎回聞かれるのしんどいので刹那的につなぐ自分用のサーバーであればホストキー検証をスキップするエイリアスを紹介しました

仮想マシンを何回も作りなおしていろんなホストにSSH接続していくと known_hosts にフィンガープリントのゴミが溜まっていくのですが、仮想マシンを全く同じ構成で作りなおして、同じIPで同じポートに接続しようとすると

localhost:~ noguchiwataru$ ssh root@192.168.0.114 -i ~/.ssh/openstack-default.key 
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    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 RSA key sent by the remote host is
0b:1d:3b:5e:b8:ba:ad:07:b5:71:7a:16:32:91:8d:2d.
Please contact your system administrator.
Add correct host key in /Users/noguchiwataru/.ssh/known_hosts to get rid of this message.
Offending RSA key in /Users/noguchiwataru/.ssh/known_hosts:14
RSA host key for 192.168.0.114 has changed and you have requested strict checking.
Host key verification failed.

こんな感じで接続を拒否されます。
なんだか大げさなメッセージが出て来ましたが、何もした覚えがないのに本番サイトでこれが出たらなりすましの可能性があるので 絶対接続しないほうがいいです。

今回のケースは実験目的なので特に問題はなくて、 known_hosts に記録された定義を削除して解決したいと思います。

2020-03-20 最近の OpenSSH クライアントの親切なメッセージ

fred@localhost:~$ ssh ubuntu@10.0.0.123
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    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:tfZBzQ16o7O7SH6u4ixBmL061Sxz8DOo1cFZ9oMuGjE.
Please contact your system administrator.
Add correct host key in /home/fred/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /home/fred/.ssh/known_hosts:4
  remove with:
  ssh-keygen -f "/home/fred/.ssh/known_hosts" -R "10.0.0.123"
ECDSA host key for 10.0.0.123 has changed and you have requested strict checking.
Host key verification failed.
fred@localhost:~$ ssh -V
OpenSSH_7.6p1 Ubuntu-4ubuntu0.3, OpenSSL 1.0.2n  7 Dec 2017

あなたがつなごうとしているサーバーがたしかにあなたがつなごうとしているサーバーだとしたら下記のようなコマンドで消せばいい、と書いてあります。

ssh-keygen -f "/home/fred/.ssh/known_hosts" -R "10.0.0.123"

sedで消す原始的な方法

vimで開いて消すのもいいのですが、ファイルの内容が長くて探すの大変なのでsed で消したいと思います。

sed -i -e '/0.114/d' ~/.ssh/known_hosts

d でマッチする行を削除するコマンドになります。
これで消えました。便利ですねー。

localhost:~ noguchiwataru$ ssh root@192.168.0.114 -i ~/.ssh/openstack-default.key 
The authenticity of host '192.168.0.114 (192.168.0.114)' can't be established.
RSA key fingerprint is 0b:1d:3b:5e:b8:ba:ad:07:b5:71:7a:16:32:91:8d:2d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.114' (RSA) to the list of known hosts.

wnoguchi Customized AMI Linux Image
Base: CentOS 6.4 x64
[root@sample ~]#

接続できた。

ご指摘いただいた ssh-keygenでやる方法

こっちのほうが正統派なのかもしれません。

localhost:~ noguchiwataru$ ssh-keygen -R 192.168.0.10
/Users/noguchiwataru/.ssh/known_hosts updated.
Original contents retained as /Users/noguchiwataru/.ssh/known_hosts.old
localhost:~ noguchiwataru$ ssh wnoguchi@192.168.0.10
The authenticity of host '192.168.0.10 (192.168.0.10)' can't be established.
RSA key fingerprint is 16:97:60:84:18:da:ab:7b:23:6e:e0:72:ab:e5:75:db.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.10' (RSA) to the list of known hosts.
wnoguchi@192.168.0.10's password: 
Welcome to Ubuntu 12.04.2 LTS (GNU/Linux 3.5.0-23-generic x86_64)

 * Documentation:  https://help.ubuntu.com/

  System information as of Fri Aug 16 15:09:33 JST 2013

  System load:  0.0               Processes:           86
  Usage of /:   1.9% of 54.11GB   Users logged in:     0
  Memory usage: 0%                IP address for eth0: 192.168.0.10
  Swap usage:   0%

  Graph this data and manage this system at https://landscape.canonical.com/

84 packages can be updated.
45 updates are security updates.


The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

wnoguchi@wstack:~$

上記クラウドインスタンスで何千万回も同じインスタンス作っていて毎回やってられない人向け

開発用、検証用のサーバーだったり何度も何度もインスタンスを作り直すことが確定しており、明らかに自分がつなぐ刹那的に信頼できるサーバーだったら毎度上のようなことやってられないので下記のようなエイリアス定義してやっています。

下記は開発用のものであって、間違っても本番サーバー等でホストキー検証をすっ飛ばさないようにしてください。

alias issh='ssh -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" -q'
alias iscp='scp -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" -q'
alias isftp='sftp -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" -q'
  1. How to disable strict host key checking in ssh? - Ask Ubuntu
  2. Swiss Army Knife [PG1X]
219
175
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
219
175