SSH経由でプライベートIPに

SSH経由でプライベートIPに

実際のIP wxyzを使用してインターネットとArchLinux VPS(コンピュータB)にアクセスできるプライベートIP 10.150.5.141(制限付きファイアウォールを含む)で構成されたCentOSコンピュータ(コンピュータA)があります。

コンピュータBにアクセスできる他のコンピュータ(コンピュータC)をコンピュータAに接続しますが、コンピュータCはコンピュータAに直接接続できません(Aの独自のプライベートネットワークにあるため)。

トンネルが別のコンピュータのローカルポート:ポートで開くことができることを知っていますが、その逆はどうですか?

コンピュータBを介してコンピュータAにアクセスしたいのですが、sshコンピュータAのネットワークが制限されているため、コンピュータBはコンピュータAにアクセスできません(ルータにアクセスできないため出ることはできますが、入ることはできません)。

私は次のようなものが欲しい:

ssh -connect-to w.x.y.z:22 -open-port vvv -forward-to 10.150.5.141 -port 22

これにより、ssh w.x.y.z:vvvコンピュータCから移動したときにプライベートネットワークに転送されます10.150.5.141:22

ベストアンサー1

あなたが探しているものをリバーストンネリングと呼びます。sshスイッチを介して利用可能-R

-R [bind_address:]port:host:hostport
       Specifies that the given port on the remote (server) host is to 
       be forwarded to the given host and port on the local side.  This 
       works by allocating a socket to listen to port on the remote side, 
       and whenever a connection is made to this port, the connection is
       forwarded over the secure channel, and a connection is made to host 
       port hostport from the local machine.

OPが答えで見つけたように、構文は次のとおりです。

$ ssh -f -N -R vvv:localhost:22 w.x.y.z

はい

ネットワークに2台のコンピュータがあり、lappy次のremoteyコマンドを実行しますlappy

$ ssh -f -N -R 12345:localhost:22 remotey

動作していることを確認できます。

$ ps -eaf|grep "[l]ocalhost:22"
saml     27685     1  0 11:10 ?        00:00:00 ssh -f -N -R 12345:localhost:22 remotey

ssh単独でリモートシステムに移動してこのコマンドを実行すると、remoteyリモートシステムのローカルインターフェイスからポート12345の接続を受け入れることがわかります。

$ netstat -an|grep :12345
tcp        0      0 127.0.0.1:12345             0.0.0.0:*                   LISTEN      
tcp        0      0 ::1:12345                   :::*                        LISTEN      

接続テスト

リバースSSHトンネルが次のように動作することがわかります。

  1. ログインremotey

    [user@lappy ~]$ ssh remotey
    
  2. リバーストンネルポートテスト

    [user@remotey ~]$ ssh -p 12345 localhost
    
  3. 今再びラピに戻る時間

    user@localhost's password: 
    Last login: Thu Aug  1 17:53:54 2013
    /usr/bin/xauth:  creating new authority file /home/user/.Xauthority
    [user@lappy ~]$ 
    

localhost()lo以外のインターフェイスにポートがありますか?

このようなコマンドを試しましたが、うまくいかないような場合、または常にlolocalhost()インターフェイスのポートにバインドされていると、頭が傷つく可能性があります。

たとえば、

lappy$ ssh -f -N -R remotey:12345:lappy:22 remotey

メモ:このコマンドは、ポート 12345@remotey を開き、すべての接続をポート 22@lappy にトンネリングすることを意味します。

その後、リモコンで次の操作を行います。

remotey$ netstat -an|grep 12345
tcp        0      0 127.0.0.1:12345              0.0.0.0:*                   LISTEN   

何が起こっているのかは、sshd構成がこれを許可しないことです。実際にこの機能()を有効にしないと、トンネルポートをlocalhost以外のポートにバインドGatewayPortsできません。ssh

ゲートウェイポートの有効化

remotey$ grep GatewayPorts /etc/ssh/sshd_config
#GatewayPorts no

有効にするには、次のファイルを編集してください/etc/ssh/sshd_config

GatewayPorts clientspecified

再起動してくださいsshd

remotey$ sudo service sshd restart

もう一度やり直すと、あなたが望む効果を見ることができます。

lappy$ ssh -f -N -R remotey:12345:lappy:22 remotey

今回は遠隔で注意深く確認してください。

remotey$ netstat -anp | grep 12345
tcp        0      0 192.168.1.3:12345           0.0.0.0:*                   LISTEN      9333/sshd

メモ:sshd上記では、プロセスがポート12345の接続のためにIPアドレス192.168.1.3のインターフェイスでリッスンしていることがわかります。

テスト接続(二重部分)

これで、このテストの設定を変更しました。最大の違いは、もはやlocalhostに接続する必要はないということです!

  1. ログインremotey

    [user@lappy ~]$ ssh remotey
    
  2. リバース接続テスト

    [user@remotey ~]$ ssh -p 12345 remotey
    
  3. 今再びラピに戻る時間

    root@remotey's password: 
    Last login: Wed Aug 21 01:49:10 2013 from remotey
    [user@lappy ~]$ 
    

引用する

おすすめ記事