OpenSSHの警告メッセージを黙らせる

OpenSSHでサーバ接続した時に表示される警告メッセージを、黙らせる方法をメモ。

警告メッセージなしにリモートサーバにsshログイン

などといった時に、OpenSSH は警告メッセージを表示するため、ユーザーが何らかの操作を行わないと、次の処理に移れない。
諸般の事情により、このメッセージを黙らせるには、ssh の接続オプションに -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null を指定すると良い。

ssh のオプションで渡す

$ ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null server_name

ssh の設定ファイルで指定する

$ cat ~/.ssh/config
Host 10.0.0.*
   StrictHostKeyChecking no
   UserKnownHostsFile=/dev/null

ssh オプションの意味

StrictHostKeyChecking オプションは データベースにないホストに接続するときに

  • ask(default) : 確認させる
  • yes : 接続しない
  • no : 接続する

を決定する。

“no” にすることで、無条件に接続される。

ssh_config(5) から

If this flag is set to “yes”, ssh(1) will never automatically add host keys to the ~/.ssh/known_hosts file, and refuses to con‐nect to hosts whose host key has changed. This provides maximum protection against trojan horse attacks, though it can be annoy‐ing when the /etc/ssh/ssh_known_hosts file is poorly maintained or when connections to new hosts are frequently made. This option forces the user to manually add all new hosts. If this flag is set to “no”, ssh will automatically add new host keys to the user known hosts files. If this flag is set to “ask”, new host keys will be added to the user known host files only after the user has confirmed that is what they really want to do, and ssh will refuse to connect to hosts whose host key has changed. The host keys of known hosts will be verified automatically in all cases. The argument must be “yes”, “no”, or “ask”. The default is “ask”.

UserKnownHostsFile オプションはホストのデータベースファイルを指定する。

/dev/null を指定することで、 read しても空なので、新規ホストに接続した時と同じ挙動になる。
新規ホストに接続後、ホスト情報を write しても何も起きないので、既存の known_hosts ファイルに影響を与えない。

ssh_config(5) から

Specifies one or more files to use for the user host key database, separated by whitespace. The default is ~/.ssh/known_hosts,

接続テスト

初めてのホストに接続する

.ssh/known_hosts を削除していつものように接続する

$ rm .ssh/known_hosts
$ ssh localhost
The authenticity of host 'localhost (127.0.0.1)' can't be established.
ECDSA key fingerprint is b0:42:c8:10:0f:7e:d0:42:20:62:94:ea:45:f9:d1:7c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.
Welcome to Ubuntu 12.04.4 LTS (GNU/Linux 3.2.0-23-generic x86_64)
...

ログイン時に、接続を続けてよいか確認される。

警告無視オプションをつけてログインすると

$ rm .ssh/known_hosts
$ ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null localhost
Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.
Welcome to Ubuntu 12.04.4 LTS (GNU/Linux 3.2.0-23-generic x86_64)
...
$ cat .ssh/known_hosts
cat: .ssh/known_hosts: No such file or directory

新規ホストが known hosts に追加されるが、追加先が /dev/null のため、~/.ssh/known_hosts ファイルは作成されない。

接続先ホストのフィンガープリントが変わった

現在のフィンガープリントを確認

$ ssh-keygen -l -f /etc/ssh/ssh_host_rsa_key
2048 f8:fd:a5:6f:bf:7f:92:a2:0c:b0:fe:24:1d:4d:6f:96  root@u1204a (RSA)

鍵を削除して、ホストの鍵を再生成させる。

$ sudo rm /etc/ssh/ssh_host_*
$ sudo dpkg-reconfigure openssh-server
Creating SSH2 RSA key; this may take some time ...
Creating SSH2 DSA key; this may take some time ...
Creating SSH2 ECDSA key; this may take some time ...
ssh stop/waiting
ssh start/running, process 16680

新しいフィンガープリントを確認

$ ssh-keygen -l -f /etc/ssh/ssh_host_rsa_key
2048 06:8e:68:8b:17:54:3b:59:68:6e:1f:bd:4a:0b:9d:90  root@u1204a (RSA)

この状態で localhost に ssh してみる

$ 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
bd:41:80:21:8d:42:65:a9:9b:21:d7:3c:e9:f0:7a:23.
Please contact your system administrator.
Add correct host key in /home/jsmith/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /home/jsmith/.ssh/known_hosts:1
  remove with: ssh-keygen -f "/home/jsmith/.ssh/known_hosts" -R localhost
ECDSA host key for localhost has changed and you have requested strict checking.
Host key verification failed.

フィンガープリントが変わったと怒られた。

警告無視オプションをつけてログインすると

$ ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null localhost
Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.
Welcome to Ubuntu 12.04.4 LTS (GNU/Linux 3.2.0-23-generic x86_64)

knwon_hosts ファイルに /dev/null を指定したため、フィンガープリントが変わったことに気づかず、ログインに成功。

ご利用は At Your Own Risk で。

resources

4 thoughts on “OpenSSHの警告メッセージを黙らせる

Leave a comment