ポート転送とローカルマシンへのコピー

ポート転送とローカルマシンへのコピー

私は通常、SSH 経由でネットワークにログインし、その後、別のコンピューターに SSH 経由で再度ログインして、最終目的地のコンピューターに接続します。たとえば、ホーム サーバーから家族のコンピューターの 1 つにログインします。これは次のようになります。

user0@inital:> ssh -P port_number user1@server
user1@server:> ssh -P port_number user2@final
user2@final:>

オンになったら、user2@final(scp) を にコピーし直したいと思いますuser0@inital

たとえば、ローカルポート転送を行い、ローカルコンピュータからサーバー経由でリモートコンピュータにコピーすることができます。user0@initial

 user0@initial:> ssh -L4321:final:22 -p 443 user1@server

これはローカルポート4321フォームuser0@initialを経由し、ポートuser1@serverに転送します。次に、を実行して にします。22user2@finaluser0@initial

  scp -P 4321 some_file  [email protected]:~/

user2@final以上にコピーできますuser1@server

問題は、user2@finalをから に逆にコピーする方法ですuser0@initial

助けてくれてありがとう。

答え1

final のコマンド プロンプトで scp コマンドを実行するとします。

# have the local client tell the remote server's sshd to listen on
# port 8765 (randomly chosen) and forward any connection it receives
# to the client which will connect to port 22 locally.
user0@initial:> ssh -R127.0.0.1:8765:127.0.0.1:22 -p 443 user1@intermediate

# On this machine have the client tell this remote server's (final's)
# to listen on port 9876 (randomly chosen) and forward any connection
# that it receives back to this client which will connect it to poirt
# 8765 locally.
user1@intermediate:> ssh -R127.0.0.1:9876:127.0.0.1:8765 user2@final

# Now that you are on the final server (final) you run scp, telling
# it to connect to localhost on port 9876.
# 
# So scp will connec to local (final's) port 9876, which is listened
# to by the local sshd based on our second command above.  That sshd
# will forward the connection to the ssh client that connected to it
# (on intermediate).
# 
# The ssh client on intermediate will connect to localhost:8765 as
# instructed which is a conenction to the sshd on intermediate that
# is listening on that port because it was instructed to do so by the
# ssh client on initial when it connected.
# 
# The sshd on intermediate will forward the conenction back to the
# client on initial which will, as instructed, connect to localhost:22
# on initial.
# 
# All this monkey motion means that now scp on final is "directly"
# connected to port 22 (sshd) on initial and can initiate a login
# and file transfer. to the ssh client that connected to it (on
# intermediate).
user2@final:> scp -P 9876 file_from_final 127.0.0.1:back_at_the_house

すべてのポートを 127.0.0.1 に設定していることに注意してください。これにより、インターネット上の他のユーザーによる悪用から保護されます (ただし、「サーバー」または「最終」上の他のユーザーによる悪用からは保護されません)。

答え2

はい。ssh_configキーワードを確認してください。プロキシコマンド

サーバーに接続するために使用するコマンドを指定します。コマンド文字列は行末まで拡張され、シェル プロセスが長引かないようにユーザーのシェルの 'exec' ディレクティブを使用して実行されます。

コマンド文字列では、'%h' は接続するホスト名に、'%p' はポートに、'%r' はリモート ユーザー名に置き換えられます。コマンドは基本的に何でもかまいませんが、標準入力から読み取り、標準出力に書き込む必要があります。最終的には、どこかのマシンで実行されている sshd(8) サーバーに接続するか、どこかで sshd -i を実行する必要があります。ホスト キーの管理は、接続するホストの HostName を使用して行われます (デフォルトでは、ユーザーが入力した名前になります)。コマンドを "none" に設定すると、このオプションは完全に無効になります。プロキシ コマンドによる接続では、CheckHostIP は利用できないことに注意してください。

このディレクティブは、nc(1) とそのプロキシ サポートと組み合わせて使用​​すると便利です。たとえば、次のディレクティブは 192.0.2.0 の HTTP プロキシ経由で接続します。

ProxyCommand /usr/bin/nc -X connect -x 192.0.2.0:8080 %h %p

関連情報