LoginSignup
4
2

More than 3 years have passed since last update.

なぜ?ec2インスタンスの踏み台サーバ経由のSSH接続がうまくいかない

Last updated at Posted at 2020-06-04

TL;DR

接続したいサーバーのホスト名にパブリックIPを指定していた、プライベートIPの間違いでした
気づくのに3時間ほどかかった分、ProxyCommandとかsshの設定に関して理解が深まっていい経験になりました😇

やりたかったこと

パブリックなサブネットに踏み台用のec2インスタンスを作成して、プライベートなサーバにsshがしたい!
多段sshは初めてなので見様見真似でやってみる

ProxyCommandなるものを使うらしいので、こちらの記事を参考に.ssh/configを修正
踏み台サーバーを経由してSSH接続する設定を~/.ssh/configに書く

症状

.ssh/configは以下のように設定

Host humidai
User ec2-user
HostName 踏み台サーバのパブリックIP
IdentityFile ~/.ssh/秘密鍵のファイル名.pem

Host honnmei
User ec2-user
HostName 接続したいサーバのパブリックIP 
IdentityFile ~/.ssh/秘密鍵のファイル名.pem
ProxyCommand ssh humidai -W %h:%p 

ssh honnmei とすると

channel 0: open failed: connect failed: Connection timed out
stdio forwarding failed
kex_exchange_identification: Connection closed by remote host

。。。🙃

こちらを参考にいろいろ試行錯誤
ssh_exchange_identification: Connection closed by remote host エラーが発生した時の対処

しかし一通り検証したが解消できず、、、投げたい

原因

途方にくれた末にこちらの記事にたどり着く
【AWS 再入門】VPC 環境に踏み台サーバーを構築して SSH 接続してみよう

Hostnameにはアプリサーバーインスタンスに割り当てられているPrivate IPsを指定すればOKです。

あ。。。

さっそく
.ssh/configのHostName部分を修正

ユーザー@usernoMacBook-Pro-5 ~> ssh honnmei

       __|  __|_  )
       _|  (     /   Amazon Linux 2 AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-2/
[ec2-user@ip-*.*.*.* ~]$

どっつかれた〜〜

4
2
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
4
2