大网数据广告活动
服务器运维问答

Remote port forwarding inside Docker containers

· 52CE 我爱测

I'm trying to setup a docker container that I use to bypass firewalls/NAT's to allow SSH access to the computers behind these routing barriers. In essence, I have an SSH service listening inside of a docker container, that my oth…

问题原文

原帖链接 · staticfloat · CC BY-SA 3.0

I'm trying to setup a docker container that I use to bypass firewalls/NAT's to allow SSH access to the computers behind these routing barriers. In essence, I have an SSH service listening inside of a docker container, that my other computers connect to, opening a reverse-SSH port forwarding, and then if I want to connect to a computer behind a firewall, I instead connect to my dockerized proxy server on the reverse port. Example:

Firewalled computer "Bob" connects to proxy server:

ssh -R 2024:localhost:22 -N remote.server

Next, I connect to the remote server on port 2024 so as to follow the tunnel back down and connect to localhost:22 on bob:

ssh -p 2024 remote.server

This all works great when it's not dockerized, however when I tried to move this to a dockerized service, I found that my sshd server within the docker container stubbornly refuses to open remote port forwards. Connecting with ssh -vvv in the first step above gives:

...
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

Which sounds a lot like my sshd isn't setup to allow remote port forwarding. However, my sshd_config seems to think it is:

# tail /etc/ssh/sshd_config -n 5
GatewayPorts yes
AllowTcpForwarding yes
AllowStreamLocalForwarding yes
PermitTunnel yes
UsePrivilegeSeparation no

Indeed, running with ssh -ddd inside the docker container, then connecting with the line above shows first:

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

Followed by:

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 [email omitted] want_reply 0

So clearly my configuration is being set correctly, but it seems that the client still thinks the server cannot do port forwarding. How can I convince the openssh server to perform remote forwarding? What could cause this failure?

The client is running OpenSSH_7.2p2 Ubuntu-4ubuntu2.4, the server is running OpenSSH_6.7p1 Debian-5+deb8u4, the docker version is 17.09.1-ce on Amazon Linux 2017.09.

Thanks!

已采纳解答

原帖链接 · staticfloat · CC BY-SA 3.0

Aha! I figured it out. This was because docker was creating an ipv6 internal network for my containers, and my kernel did not have ipv6 forwarding enabled. So when running sshd outside of the container, it would work over ipv4, but when running sshd inside of the container on the docker bridge network, it would listen on ipv6 and be unable to open port forwards.

Once I enabled ipv6 forwarding, (adding net.ipv6.conf.all.forwarding = 1 in /etc/sysctl.conf and rebooting) everything starting working just fine.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-03-03)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。