Docker 容器内的远程端口转发

sta*_*oat 5 ssh ssh-tunnel docker

我正在尝试设置一个 docker 容器,用于绕过防火墙/NAT 以允许 SSH 访问这些路由屏障后面的计算机。本质上,我有一个 SSH 服务在 docker 容器内侦听,我的其他计算机连接到该服务,打开反向 SSH 端口转发,然后如果我想连接到防火墙后面的计算机,我改为连接到我的 dockerized反向端口上的代理服务器。例子:

防火墙计算机“鲍勃”连接到代理服务器:

ssh -R 2024:localhost:22 -N remote.server
Run Code Online (Sandbox Code Playgroud)

接下来,我通过端口连接到远程服务器,2024以便沿着隧道返回并连接到localhost:22bob:

ssh -p 2024 remote.server
Run Code Online (Sandbox Code Playgroud)

当它没有被 dockerized 时,这一切都很好,但是当我试图将它移动到一个 dockerized 服务时,我发现我sshd在docker容器中的服务器顽固地拒绝打开远程端口转发。ssh -vvv在上面的第一步中与 with 连接给出:

...
debug1: Entering interactive session.
debug1: pledge: network
debug3: receive packet: type 4
debug1: Remote: Server has disabled port forwarding.
debug3: receive packet: type 82
debug1: remote forward failure for: listen 2024, connect localhost:22
Warning: remote port forwarding failed for listen port 2024
debug1: All remote forwarding requests processed
Run Code Online (Sandbox Code Playgroud)

这听起来很像我sshd没有设置为允许远程端口转发。但是,我sshd_config似乎认为它是:

# tail /etc/ssh/sshd_config -n 5
GatewayPorts yes
AllowTcpForwarding yes
AllowStreamLocalForwarding yes
PermitTunnel yes
UsePrivilegeSeparation no
Run Code Online (Sandbox Code Playgroud)

实际上,ssh -ddd在 docker 容器内运行,然后与上面的行连接首先显示:

debug3: /etc/ssh/sshd_config:91 setting GatewayPorts yes
debug3: /etc/ssh/sshd_config:92 setting AllowTcpForwarding yes
debug3: /etc/ssh/sshd_config:93 setting AllowStreamLocalForwarding yes
debug3: /etc/ssh/sshd_config:94 setting PermitTunnel yes
debug3: /etc/ssh/sshd_config:95 setting UsePrivilegeSeparation no
Run Code Online (Sandbox Code Playgroud)

其次是:

debug1: server_input_global_request: rtype tcpip-forward want_reply 1
debug1: server_input_global_request: tcpip-forward listen localhost port 2024
debug1: server_input_global_request: rtype no-more-sessions@openssh.com want_reply 0
Run Code Online (Sandbox Code Playgroud)

很明显我的配置设置正确,但客户端似乎仍然认为服务器无法进行端口转发。如何说服 openssh 服务器执行远程转发?什么可能导致这种失败?

客户端正在运行OpenSSH_7.2p2 Ubuntu-4ubuntu2.4,服务器正在运行OpenSSH_6.7p1 Debian-5+deb8u4,docker 版本已17.09.1-ce开启Amazon Linux 2017.09

谢谢!

sta*_*oat 4

啊哈!我想到了。这是因为docker正在为我的容器创建ipv6内部网络,而我的内核没有ipv6启用转发。因此,当在sshd容器外部运行时,它会正常工作ipv4,但是当sshd在 docker 桥接网络上的容器内部运行时,它会侦听ipv6并且无法打开端口转发。

一旦我启用ipv6转发,(添加net.ipv6.conf.all.forwarding = 1/etc/sysctl.conf重新启动)一切就开始正常工作。