
2台のサーバーがあります。サーバーAにはパブリックIPアドレス(43.24.24.29
)があります。サーバーBにはプライベートIPアドレス(10.10.10.1
)があります。
サーバーAはパブリックIPアドレス(一部の固定パスを使用)を介してサーバーBに接続できるため、サーバーAはサーバーBに接続できます。
別のPCを使ってサーバーBに接続したいssh
ので、サーバーAにSSHポートを追加しました。
[ldl@ServerA ~]$ sudo ssh -N -f -L 222:43.24.24.29:22 [email protected]
これでサーバーから起動します。
[ldl@ ServerA ~]$ ps -ef | grep ssh
root 10228 1 0 4th mon 13 ? 00:03:19 /usr/sbin/sshd -D
root 91304 10228 0 04:46 ? 00:00:01 sshd: ldl [priv]
ldl 91308 91304 0 04:46 ? 00:00:00 sshd: ldl@pts/0
root 100491 1 0 10:57 ? 00:00:00 ssh -N -f -L 222:43.24.24.29:22 [email protected]
root 100501 10228 1 10:57 ? 00:00:00 sshd: root [priv]
sshd 100502 100501 0 10:57 ? 00:00:00 sshd: root [net]
ldl 100505 91309 0 10:57 pts/0 00:00:00 grep --color=auto ssh
[ldl@ServerA ~]$ netstat -tlnp | grep 222
(No info could be read for "-p": geteuid()=1000 but you should be root.)
tcp 0 0 127.0.0.1:222 0.0.0.0:* LISTEN -
tcp6 0 0 ::1:222 :::* LISTEN -
しかし、私のローカルPCからサーバーAのポート222にSSHを試みると、次の「接続が拒否されました」エラーが発生します。
aircrafts-MBP:~ ldl$ ssh [email protected] -p 222 -vvv
OpenSSH_7.7p1, LibreSSL 2.7.3
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 48: Applying options for *
debug2: resolve_canonicalize: hostname 43.24.24.29 is address
debug2: ssh_connect_direct: needpriv 0
debug1: Connecting to 43.24.24.29 [43.24.24.29] port 222.
debug1: connect to address 43.24.24.29 port 222: Connection refused
ssh: connect to host 43.24.24.29 port 222: Connection refused
これが43.24.24.29
私のデモIPアドレスです。
編集-01
私は次のコマンドを試しました。
ssh -N -f -L 2222:localhost:22 [email protected]
接続すると、次のエラーが発生しますssh_exchange_identification: read: Connection reset by peer
。詳しく説明してください:
aircrafts-MBP:~ ldl$ ssh 43.24.24.29 -p2222 -vvv
OpenSSH_7.7p1, LibreSSL 2.7.3
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 48: Applying options for *
debug2: resolve_canonicalize: hostname 43.24.24.29 is address
debug2: ssh_connect_direct: needpriv 0
debug1: Connecting to 43.24.24.29 [43.24.24.29] port 2222.
debug1: Connection established.
debug1: identity file /Users/ldl/.ssh/id_rsa type 0
debug1: key_load_public: No such file or directory
debug1: identity file /Users/ldl/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/ldl/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/ldl/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/ldl/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/ldl/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/ldl/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/ldl/.ssh/id_ed25519-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/ldl/.ssh/id_xmss type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/ldl/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_7.7
ssh_exchange_identification: read: Connection reset by peer
答え1
リモートホストに転送する場合は、リモートポート転送に次のコマンドを使用する必要があります。-アル字型-Lの代わりに引数を使用すると、トンネルはローカル(localhost)に設定されます。それ以外の場合は、作成するすべてのトンネルがlocalhostに設定されます。また、GatewayPorts yes
ファイル/etc/sshd_config
にパラメータを設定したことを確認する必要があります。10.10.10.1それ以外の場合は渡されません。
これを試す前に、GatewayPortsパラメーターがyesであることを確認してください。
ssh -N -f [email protected] -R 222:43.24.24.29:22
その後(編集):
ssh [email protected] -p 222
答え2
ポート転送には2つのIP /ポートペアがあります。
<from IP>:<from PORT>:<to IP>:<to PORT>
IPを1つだけ設定すると、次のように表示されます。
<from PORT>:<to IP>:<to PORT>
「ip からポートへ」は ssh クライアントに移動し、「ip からポートへ」はサーバーから来るので混乱する可能性があります。
サーバーAで次のことを試してください。
ssh -N -f -L 2222:localhost:22 [email protected]
それではあなたはできます。
ssh 43.24.24.29:2222
これは、SSH 接続がサーバー A からサーバー B になっているために機能します。したがって、上記のコマンドでは、「localhost」はサーバーBを表します。
答え3
最初の試行で発生したエラーは、コマンドを介してServerAが独自にのみアクセスできる、いわゆる「localhost」アドレスである127.0.0.1のみを受け取るようにするためです。実際、ServerA自体はServerAのポート222にのみアクセスできます。
また、-L 222:43.24.24.29:22
これを指定すると、ServerB(10.10.10.1)が実際にServerAのポート22に再接続されます。これはあまり役に立たないと思います。
2番目の試み(EDIT-01)は良いですが、ServerAはまだlocalhostでのみリッスンします。したがって、どのプロセスがSSHサーバーではなくServerAポート2222への接続を受信したかを想像することはできません。以前に失敗した試行で、ServerAに存在する可能性のあるすべてのkill
コマンドを試して、使用してssh -N ...
いるポート(例では2222)が使用中であることを確認する必要があります。
これらすべてを考えると、何をすべきかは次のようになります。
[ldl@ ServerA ~]$ ssh -N -f -L :2222:localhost:22 [email protected]
上記は、ServerAにポート2222(1023以降では実行する必要はありませんsudo
)を設定してリッスンするように指示します。みんなそのアドレス(:
前の2222を参照)は特別に確立された接続を使用します。[Eメール保護](ServerB)は次に接続します。ポート22サーバーB「ローカルホスト」。
動作方法は次のとおりです。まず、「ローカルマシン」(上記の例ではServerA)に設定したいアドレスとポート(上記の例では)と場所を-L
知らせます。:2222
リモートマシンに接続しました。 「リモートシステム」は、[email protected]
sshコマンドがこの構文を介して接続されるシステムです。
最初はぎこちないように見えるかもしれませんが、SSHへのポート転送はあるホストから別のホストにジャンプするためのソリューションだけでなく、SSHプロトコル自体を転送する良い方法では設計されていません。
したがって、ServerAをServerBのプレーンMBPホップとして使用するだけで、次のことをお勧めします。
ssh -t [email protected] 'ssh [email protected]'
飛行機から直接 - MBP。
答え4
最初の試みには2つの問題があります。
[ldl@ServerA ~]$ sudo ssh -N -f -L 222:43.24.24.29:22 [email protected]
これはSSHをrootとして実行するため、1024を超えるローカルポート番号のみを受信する制限は適用されません。ただし、これを指定しなかったため、bind_address
SSHクライアントはlocalhostでのみリッスンするクライアントGatewayPortsのデフォルト設定に従い、PCから接続できません。
また、トンネルの遠隔端を 43.24.24.29:22 と指定したので、トンネルを使用できても ServerA のsshd
。
この問題を解決するには:
[ldl@ServerA ~]$ sudo ssh -N -f -L "*:222:localhost:22" [email protected]
追加*:
部分では、SSH クライアントに localhost だけでなく、すべての IP アドレスからトンネルへの接続を許可するように指示します。シェルがアスタリスクのワイルドカード拡張を防ぐために二重引用符が追加されました。そして、「localhost:22」のトンネルターゲットは、すでにServerBにあるこのSSH接続のリモート側に適用されます。
これにより、PCで次のことができます。
aircrafts-MBP:~ ldl$ ssh [email protected] -p 222
...possibly a SSH host key warning and/or a password prompt...
[root@ServerB ~]#
ポート222の専用使用が必要ない場合は、1023より大きいポート番号を使用する必要はありませんsudo
。
[ldl@ServerA ~]$ ssh -N -f -L "*:2222:localhost:22" [email protected]
aircrafts-MBP:~ ldl$ ssh [email protected] -p 2222
...possibly a SSH host key warning and/or a password prompt...
[root@ServerB ~]#
この猫の皮をむく別の方法があります。 PCでこれを行うことができます。
aircrafts-MBP:~ ldl$ ssh -L 2222:10.10.10.1:22 [email protected]
[ldl@ServerA ~]$
...ServerAで利用可能なシェルを取得し、同時にServerBに渡します。
その後、別のターミナルウィンドウで:
aircrafts-MBP:~ ldl$ ssh -p 2222 root@localhost
...possibly a SSH host key warning and/or a password prompt...
[root@ServerB ~]#
この設定では、PCの最初のSSHセッションはPCのTCP / 2222でリスンプロキシを確立し、すべてのトラフィックをServerAに転送し、ポート22でServerAからServerBへの「一般」TCP接続を確立してクライアントを介して入ります。ポートのトラフィックをリッスンします。ただし、プロキシを使用する2番目のセッションもSSHセッションであるため、PCからServerAへのホップはSSH経由のSSHになり、ServerAからServerBへのホップはSSHのみです。