2017年2月4日土曜日

ssh の ProxyCommand で踏み台経由のコマンド実行


以下のような環境があるとします。
  • クライアント: 192.168.56.102(centos0601)
  • 踏み台: 192.168.56.101(centos0702)
  • 内部サーバ: 172.18.0.2(docker01)
「クライアント」から「内部サーバ」には直接アクセスできないので、「クライアント」からは「踏み台」経由で「内部サーバ」にアクセスします。

「踏み台」は、両方にアクセスできます。
[root@centos0702 centos6]# uname -n
centos0702
[root@centos0702 centos6]# sshpass -p p@ssw0rd ssh 192.168.56.102 uname -n
Warning: Permanently added '192.168.56.102' (RSA) to the list of known hosts.
centos601
[root@centos0702 centos6]# ssh -i ./centos.pem centos@172.18.0.2 uname -n
Warning: Permanently added '172.18.0.2' (RSA) to the list of known hosts.
docker01

ProxyCommand を使用すると、「クライアント」から「踏み台」を経由して「内部サーバ」にコマンドを実行できます。
[root@centos601 workspace]# ssh -o ProxyCommand='ssh -i ./root.pem root@192.168.56.101 -W 172.18.0.2:22' -i ./centos.pem centos@172.18.0.2 uname -n
Warning: Permanently added '192.168.56.101' (RSA) to the list of known hosts.
Warning: Permanently added '172.18.0.2' (RSA) to the list of known hosts.
docker01
Killed by signal 1.

秘密鍵はクライアント側においてあります。
[root@centos601 workspace]# ls
centos.pem  root.pem

また、上記例ではクライアント側のconfig設定に koun_hosts を無視する設定をいれています。
[root@centos601 workspace]# cat ~/.ssh/config
Host *
   StrictHostKeyChecking no
   UserKnownHostsFile=/dev/null