2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

踏み台をいくつか経由した上でSSHポートフォワーディング

Posted at

概要

次のような構成において、Clientから直接RemoteServiceにアクセスしたい。

多段SSH_ポートフォワーディング.png

どういうことかというと、RemoteServiceはNW内(図中の外部NW)からのアクセスしか許可されていない。例えば踏み台2からならアクセスできるが、図中の内部NW(Client、踏み台1)からはアクセスできない。

方針

多段SSHとSSHポートフォワーディングを使う。

Clientから自身の10080番ポートにアクセスすると、RemoteServiceの80番ポートにつながるように設定する。

設定

Clientの~/.ssh/configに下記を設定する。

~/.ssh/config
Host fumidai-1
    HostName [踏み台1のIPアドレス]

Host fumidai-2
    HostName [踏み台2のIPアドレス]
    ProxyCommand ssh -W %h:%p fumidai-1

その上で、下記コマンドを実行する。

$ ssh -f -N -L 10080:[RemoteServiceのIPアドレス]:80 fumidai-2

[RemoteServiceのIPアドレス]は、fumidai-2からアクセス可能なものを指定する。今回の例では、RemoteServiceホストのプライベートIPアドレスになるはず。

試してみる

Clientにて下記を実行する。

curl localhost:10080

トラブルシューティング

10080番ポートつながらない

ローカル(Client)はLISTENしている?
確認:Linuxならnetstat -nltp、Macならlsof -i -P -n

認証のエラーになる

ローカル(Client)からssh fumidai-1およびssh fumidai-2ができる?

できない場合

  • 踏み台サーバのSSHのログをみる(RedHat系なら/var/log/secure、Ubuntuなら/var/log/auth.log)
  • 踏み台1、踏み台2のユーザの設定を確認する
    • ユーザ名が揃っているか
    • ローカル(Client)で使用している秘密鍵のペアとなる公開鍵が正しく設定されているか
  • ssh-agent周りを確認する
    • ローカル(Client)にある秘密鍵を使って、踏み台1を超えて、踏み台2にログインするためにはssh-agentの力を借りる必要がある
    • Macならデフォルトで起動しているはず

できる場合

  • RemoteServiceホストで、サービス起動している?(そもそも)
  • 他に何かあるかな…?

ClientがWindows

GitBashを使って、同様の手順で成功することを確認した。
ssh-agentの起動で一癖あったので、気をつける。(https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/#adding-your-ssh-key-to-the-ssh-agent)

以上です。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?