多段SSH経由でのアクセスについて

Last-modified: Thu, 11 Aug 2016 20:38:11 JST (2816d)
Top > 多段SSH経由でのアクセスについて

セキュリティ強化のため、踏み台サーバ経由でしかSSH接続できないサーバ。ってのがよくあります。

その際、~/.ssh/configにこのような設定をして、つなぎます。

Host humidai
  HostName 192.168.0.5
  User root
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  IdentityFIle ~/.ssh/root_id_rsa

Host target
  HostName 192.168.20.4
  User webmaster
  UserKnownHostsFIle /dev/null
  StrictHostKeyCHecking no
  IdentityFile ~/.ssh/webmaster_id_rsa
  ProxyCommand ssh -W %h:%p humidai

しかし、この方法ですと、SSH接続するマシンごとに設定しないといけないので面倒です。
そこで、ここを参考に、各Ansibleのプロジェクトにssh_configという設定ファイルを作成し、
Ansible実行時に参照するようにします。
なお、ansible.cfgは本体の設定を修正しなくても、~/.ansible.cfgというファイルに記載すれば、
読んでくれるようです。

では、テストで、SSH接続を試してみます。
先ほど作成した設定ファイルを./ssh_configに移動させます。

$ mv ~/.ssh/config ~/ssh_config
$ ssh humidai -F ./ssh_config
Warning: Permanently added '192.168.0.5' (RSA) to the list of known hosts.
Last login: Thu Aug 11 15:53:17 2016 from 192.168.0.104
[root@humidai ~]#

うまくいきました。ではターゲットサーバへの接続を試します。

$ ssh target -F ./ssh_config
ssh: Could not resolve hostname humidai: Name or service not known
ssh_exchange_identification: Connection closed by remote host

あれ?踏み台サーバが見つからないと怒られます。

/.ssh/configに記述した時には問題がなかったのですが、どうやら-Fで読み込ました場合は
うまく読んでくれないようです。

あれこれ試してみたのですが、解決策としては、

Host humidai
  HostName 192.168.0.5
  User root
  UserKnownHostsFile /dev/null
  StrictHostKeyChecking no
  IdentityFIle ~/.ssh/root_id_rsa

Host target
  HostName 192.168.20.4
  User webmaster
  UserKnownHostsFIle /dev/null
  StrictHostKeyCHecking no
  IdentityFile ~/.ssh/webmaster_id_rsa
  ProxyCommand ssh -F ssh_config -W %h:%p humidai

のように、ProxyCommandで実行しているsshにも-Fで設定ファイルを渡してあげると動作します。

$ ssh target -F ./ssh_config
Warning: Permanently added '192.168.0.5' (RSA) to the list of known hosts.
Warning: Permanently added '192.168.20.4' (ECDSA) to the list of known hosts.
Welcome to Ubuntu 14.04.4 LTS (GNU/Linux 3.13.0-92-generic x86_64)

Counter: 1018, today: 2, yesterday: 0

このページの参照回数は、1018です。