Docker コンテナ内に SSH トンネルを正常に設定できません。

Docker コンテナ内に SSH トンネルを正常に設定できません。

これまでホームサーバーを使用しています。この技術家の外からもアクセスできます。

しかし、今は他の要件があるので、ホームサーバー内のDockerコンテナでこれを作成したいと思います。ただし、トンネルに接続しようとすると、接続拒否エラーが発生します。

だから、このようなものを設定するためにオンラインVPSとホームサーバーがあります。私のホームサーバーの内部にはコンテナが実行されています。authorized_keysお互いのファイルを正しく設定して、VPSとコンテナ間で公開鍵を交換します。コンテナを実行しており-p 22:22、ホストのポート22を使用している可能性があるコンテナの外部で実行されているSSHサービスがないことを確認しました。

次に、コンテナで次のコマンドを実行します。

$ ssh -vvvfN -oStrictHostKeyChecking=no -R 20007:localhost:22 [email protected]

それから私のVPSにこれを入力して出力します。

$ ssh -vvv container_user@localhost -p 20007
OpenSSH_7.2p2 Ubuntu-4ubuntu2.1, OpenSSL 1.0.2g  1 Mar 2016
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug2: resolving "localhost" port 20009
debug2: ssh_connect_direct: needpriv 0
debug1: Connecting to localhost [127.0.0.1] port 20009.
debug1: Connection established.
debug1: identity file /home/raspi/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /home/raspi/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/raspi/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/raspi/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/raspi/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/raspi/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/raspi/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /home/raspi/.ssh/id_ed25519-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.1
ssh_exchange_identification: Connection closed by remote host

コンテナログを確認すると、次の内容が表示されます。

debug1: client_input_global_request: rtype [email protected] 
want_reply 0
debug1: remote forward success for: listen 20007, connect localhost:22
debug1: All remote forwarding requests processed
debug1: client_input_channel_open: ctype forwarded-tcpip rchan 2 win 2097152 max 32768
debug1: client_request_forwarded_tcpip: listen localhost port 20007, originator 127.0.0.1 port 60364
debug2: fd 7 setting O_NONBLOCK
debug2: fd 7 setting TCP_NODELAY
debug1: connect_next: host localhost ([::1]:22) in progress, fd=7
debug3: fd 7 is O_NONBLOCK
debug3: fd 7 is O_NONBLOCK
debug1: channel 0: new [127.0.0.1]
debug1: confirm forwarded-tcpip
debug3: channel 0: waiting for connection
debug1: channel 0: connection failed: Connection refused
debug2: fd 8 setting O_NONBLOCK
debug2: fd 8 setting TCP_NODELAY
debug1: connect_next: host localhost ([127.0.0.1]:22) in progress, fd=8
debug3: channel 0: waiting for connection
debug1: channel 0: connection failed: Connection refused
connect_to localhost port 22: failed.
debug2: channel 0: zombie
debug2: channel 0: garbage collecting
debug1: channel 0: free: 127.0.0.1, nchannels 1
debug3: channel 0: status: The following connections are open:

これらの手順はすべてコンテナの外部で実行すると完全に機能しますが、とにかくコンテナ内ではVPSがトンネルを使用して接続を確立できないことに注意してください。

編集する:コンテナがリモートサーバとトンネルを確立するコード。

FROM ubuntu:16.04

RUN apt-get update && apt-get install -y ssh htop nano autossh
RUN ssh-keygen -f $HOME/.ssh/id_rsa -t rsa -N '';                                             \
COPY authorized_keys $HOME/.ssh/
echo '=======SAVE THIS KEY TO ~/.ssh/authorized_keys in the Cloud Server=======';             \
cat $HOME/.ssh/id_rsa.pub;                                                                    \
echo '=========================================================================';

RUN sleep 20 # Give me time to put the key in the cloud server

RUN echo "/usr/bin/ssh -vvvfN -oStrictHostKeyChecking=no -R 20009:localhost:22 [email protected]" > $HOME/connect.sh; \
chmod 777 $HOME/connect.sh

EXPOSE 22
EXPOSE 20009
CMD ["sh", "-c", "$HOME/connect.sh"]

このDockerfileは次のようになります。

  • キーペアを生成し、
  • リモートサーバーの公開鍵を保存します。
  • そのキーペアをリモートサーバーのAuthorized_keysに入れるまで20秒待ってください。
  • その後、トンネルを開きます。

エラーメッセージに示すように、うまく動作しません。しかし、コンテナの代わりに物理マシンで同じ手順を実行すると正常に動作します。

ベストアンサー1

おすすめ記事