LoginSignup
0
0

踏み台サーバ経由の多段SSH接続を設定する

Posted at

WebサーバやDBサーバをプライベートサブネットに作成した場合、踏み台サーバを用意し、踏み台サーバにSSHで接続し、そこからWebサーバやDBサーバに接続します。
この場合、踏み台サーバが乗っ取られた場合に、他のサーバにも侵入されるリスクが大きくなってしまいます。
また、sshコマンドを2回入力する必要がある、秘密鍵ファイルを踏み台サーバに転送する必要があるといった作業の手間があります。
そこで、ローカル端末にある秘密鍵のみを利用した、多段SSH接続を設定します。

多段SSH接続を設定する

ホームディレクトリの.sshフォルダにconfigという名前のファイルを作成し、以下を記述してください。

Host bation
Hostname xx.xx.xx.xx (踏み台サーバのパブリックIP)
User ec2-user
IdentityFile ~/.ssh/xxxxx.pem

Host web01
Hostname xx.xx.xx.xx  (WebサーバのプライベートIP)
User ec2-user
IdentityFile ~/.ssh/xxxxx.pem
ProxyCommand ssh bation -W %h:%p

Host db01
Hostname xx.xx.xx.xx  (DBサーバのプライベートIP)
User ec2-user
IdentityFile ~/.ssh/xxxxx.pem
ProxyCommand ssh bation -W %h:%p

sshコマンドで接続する

ssh web01

 
以上です。

0
0
0

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
0
0