简体   繁体   English

如何从容器访问主机端口(与ssh -R绑定)?

[英]How to access a host port (bind with ssh -R) from a container?

Using Docker 1.12.1, I face a strange behaviour trying to access a host port created with ssh -R . 使用Docker 1.12.1时,我尝试访问使用ssh -R创建的主机端口时遇到一个奇怪的行为。

Basically I try to access a service running on port 12345 on my local machine from a docker container running on a server. 基本上,我尝试从服务器上运行的docker容器访问在本地计算机上的端口12345上运行的服务。

I opened a ssh connection with ssh -R *:12345:localhost:12345 user@server to open a port 12345 on server that forwards to port 12345 on my local machine. 我使用ssh -R *:12345:localhost:12345 user@server打开了ssh连接,以在server上打开端口12345 ,该端口转发到本地计算机上的端口12345

Now when I try curl https://172.17.42.1:12345 inside the container ( 172.17.42.1 is the IP to access the docker host from the docker container) I get : 现在,当我尝试在容器内curl https://172.17.42.1:12345172.17.42.1是从172.17.42.1容器访问172.17.42.1主机的IP)我得到:

root@f6873fe1109b:/# curl https://172.17.42.1:12345
curl: (7) Failed to connect to 172.17.42.1 port 12345: Connection refused

But on server the command curl http://localhost:12345 succeeds (eg. no Connection refused ) 但是在servercurl http://localhost:12345命令成功 (例如,未拒绝任何连接

server$ curl http://localhost:12345
curl: (52) Empty reply from server

I don't really understand how the port binding done with ssh differs from a test with nc on server (it works) : 我真的不明白用ssh完成的端口绑定与在服务器上使用nc进行的测试有什么不同(它可以工作):

# on server
nc -l -p 12345
# inside a container
root@f6873fe1109b:/# curl http://172.17.42.1:12345
curl: (52) Empty reply from server

NB: the container was started with docker run -it --rm maven:3-jdk-8 bash . 注意:容器是使用docker run -it --rm maven:3-jdk-8 bash

What can I do to allow my container to access the host port corresponding to a ssh binding ? 如何允许我的容器访问与ssh绑定相对应的主机端口?

From man ssh : man ssh

-R [...] -R [...]

... Specifying a remote bind_address will only succeed if the server's GatewayPorts option is enabled ...仅当启用服务器的GatewayPorts选项时,指定远程bind_address才会成功

And man sshd_config : man sshd_config

GatewayPorts 网关端口

Specifies whether remote hosts are allowed to connect to ports forwarded for the client. 指定是否允许远程主机连接到为客户端转发的端口。 By default, sshd(8) binds remote port forwardings to the loopback address. 默认情况下,sshd(8)将远程端口转发绑定到环回地址。 This prevents other remote hosts from connecting to forwarded ports. 这样可以防止其他远程主机连接到转发的端口。 GatewayPorts can be used to specify that sshd should allow remote port forwardings to bind to non-loopback addresses, thus allowing other hosts to connect. 可以使用GatewayPorts指定sshd应该允许远程端口转发绑定到非环回地址,从而允许其他主机进行连接。 The argument may be “no” to force remote port forwardings to be available to the local host only, “yes” to force remote port forwardings to bind to the wildcard address, or “clientspecified” to allow the client to select the address to which the forwarding is bound. 该参数可以是“否”,以强制远程端口转发仅对本地主机可用;“是”,以强制远程端口转发绑定到通配符地址,或者是“ clientspecified”以允许客户端选择要向其发送地址的地址。转发是绑定的。 The default is “no”. 默认为“否”。

This means that a default sshd server installation only allows to create forwards that bind to the local interface. 这意味着默认的sshd服务器安装仅允许创建绑定到本地接口的转发。 If you want to allow forwards to other interfaces then loopback, you need to set the GatewayPorts option to yes or clientspecified in your /etc/ssh/sshd_config 如果你要允许转发到其他接口则回送,你需要设置GatewayPorts选项yesclientspecified在你/etc/ssh/sshd_config

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM