请考虑以下情况:
在家里,我有一个路由器(已连接到Internet),服务器(S)和主机(M)。S可以从Internet到达(它具有静态IP),并且24/7可达,而M则不能。
有时,我想使某些应用程序(可以监听M的某个端口,例如8888)可以从外部互联网访问。
为此,我想在S(2222)上设置一些端口以转发到M的端口8888,以便访问S:2222的任何人都感觉自己正在访问M:8888。
我尝试使用ssh端口转发,我的最佳尝试如下:
ssh -L 2222:M:8888 -N M
但这仅允许我从服务器本身而不是其他计算机访问2222端口。
有什么办法可以正确地做到这一点吗?最好是,我希望它是一个简单的命令,当我不再需要转发时,可以使用^ C启动和关闭它。
是的,这GatewayPorts
在SSH中称为。摘录自ssh_config(5)
:
GatewayPorts
Specifies whether remote hosts are allowed to connect to local
forwarded ports. By default, ssh(1) binds local port forwardings
to the loopback address. This prevents other remote hosts from
connecting to forwarded ports. GatewayPorts can be used to spec‐
ify that ssh should bind local port forwardings to the wildcard
address, thus allowing remote hosts to connect to forwarded
ports. The argument must be “yes” or “no”. The default is “no”.
而且,您可以使用localhost
而不是M
用于转发,因为您可以将您转发到与SSH进行连接的同一台计算机上-如果我正确理解了您的问题。
因此,命令将变为:
ssh -L 2222:localhost:8888 -N -o GatewayPorts=yes hostname-of-M
看起来像这样netstat -nltp
:
tcp 0 0 0.0.0.0:2222 0.0.0.0:* LISTEN 5113/ssh
现在,任何在此端口2222 TCP上访问此计算机的人实际上都将与在机器M中看到的localhost:8888进行通信。请注意,这与向M的端口8888的普通转发不同。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句