系统相关
首页 > 系统相关> > linux – 如何将端口从一台机器转发到另一台机器?

linux – 如何将端口从一台机器转发到另一台机器?

作者:互联网

考虑以下情况:

在我家,我有一个路由器(连接到互联网),服务器(S)和我的主机(M). S可以从互联网上访问(它具有静态IP),并且它是24/7,而M则不是.

有时,我想从外部互联网访问一些应用程序(可以在M上的某个端口上监听,例如8888).

为此,我想在S(2222)上建立一个端口转发到M的端口8888,这样任何访问S:2222的人都会觉得他正在访问M:8888.

我尝试使用ssh端口转发,我的最佳尝试如下:

ssh -L 2222:M:8888 -N M

但这只允许我从服务器本身访问2222端口,而不是从其他机器访问.

有没有办法正确地做到这一点?最好,我希望它是一个简单的命令,当我不再需要转发时,我可以用^ C启动和关闭它.

解决方法:

是的,这在SSH中称为GatewayPorts.来自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不同.

标签:linux,port-forwarding
来源: https://codeday.me/bug/20190809/1626415.html