ssh 到私有 IP

Kok*_*zzu 19 ssh ssh-tunneling

我有一台装有 CentOS 的计算机(计算机 A),配置为具有私有 ip 10.150.5.141(带有受限防火墙),可以使用真实 ip wxyz 访问互联网和我的 ArchLinux VPS(计算机 B)

如何让另一台可以访问计算机B的PC(计算机C)连接到计算机A,但计算机C无法直接连接到计算机A(因为它在A自己的专用网络上)?

我知道隧道可以打开到另一台计算机的本地端口:端口,但如何做相反的事情?

我想ssh通过计算机B访问计算机A,但计算机B无法访问计算机A,因为计算机A上的网络受到限制(可以出去,但不能进入,因为我无法访问他们的路由器)

我想要这样的东西:

ssh -connect-to w.x.y.z:22 -open-port vvv -forward-to 10.150.5.141 -port 22
Run Code Online (Sandbox Code Playgroud)

这样当我ssh w.x.y.z:vvv从计算机 C 转发到专用网络时10.150.5.141:22

slm*_*slm 14

您正在寻找的称为反向隧道。ssh通过-R开关提供它:

-R [bind_address:]port:host:hostport
       Specifies that the given port on the remote (server) host is to 
       be forwarded to the given host and port on the local side.  This 
       works by allocating a socket to listen to port on the remote side, 
       and whenever a connection is made to this port, the connection is
       forwarded over the secure channel, and a connection is made to host 
       port hostport from the local machine.
Run Code Online (Sandbox Code Playgroud)

正如 OP 在他们的回答中发现的那样,语法如下:

$ ssh -f -N -R vvv:localhost:22 w.x.y.z
Run Code Online (Sandbox Code Playgroud)

例子

我在网络上有 2 台计算机,lappy并且remotey. 所以我运行以下命令lappy

$ ssh -f -N -R 12345:localhost:22 remotey
Run Code Online (Sandbox Code Playgroud)

我可以确认它正在工作:

$ ps -eaf|grep "[l]ocalhost:22"
saml     27685     1  0 11:10 ?        00:00:00 ssh -f -N -R 12345:localhost:22 remotey
Run Code Online (Sandbox Code Playgroud)

现在,如果我ssh单独转到远程系统,remotey并运行此命令,我可以看到它现在正在接受远程系统本地接口上端口 12345 上的连接:

$ netstat -an|grep :12345
tcp        0      0 127.0.0.1:12345             0.0.0.0:*                   LISTEN      
tcp        0      0 ::1:12345                   :::*                        LISTEN      
Run Code Online (Sandbox Code Playgroud)

测试连接

您可以看到反向 ssh 隧道的工作方式如下。

  1. 登录到 remotey

    [user@lappy ~]$ ssh remotey
    
    Run Code Online (Sandbox Code Playgroud)
  2. 测试反向隧道端口

    [user@remotey ~]$ ssh -p 12345 localhost
    
    Run Code Online (Sandbox Code Playgroud)
  3. 现在应该回到 lappy

    user@localhost's password: 
    Last login: Thu Aug  1 17:53:54 2013
    /usr/bin/xauth:  creating new authority file /home/user/.Xauthority
    [user@lappy ~]$ 
    
    Run Code Online (Sandbox Code Playgroud)

localhost ( lo)以外的接口上的端口?

如果您尝试这样的命令但它似乎不起作用,或者它总是绑定到 localhost ( lo) 接口上的端口,您可能会摸不着头脑。

例如:

lappy$ ssh -f -N -R remotey:12345:lappy:22 remotey
Run Code Online (Sandbox Code Playgroud)

注意:此命令表示打开端口 12345@remotey 并将任何连接隧道连接到端口 22@lappy。

然后在远程:

remotey$ netstat -an|grep 12345
tcp        0      0 127.0.0.1:12345              0.0.0.0:*                   LISTEN   
Run Code Online (Sandbox Code Playgroud)

发生的事情是sshd的配置不允许您这样做。事实上,如果不启用此功能 ( GatewayPorts),您将无法将任何ssh隧道端口绑定到除 localhost 之外的任何端口。

启用网关端口

remotey$ grep GatewayPorts /etc/ssh/sshd_config
#GatewayPorts no
Run Code Online (Sandbox Code Playgroud)

要启用它,请编辑此文件/etc/ssh/sshd_config

GatewayPorts clientspecified
Run Code Online (Sandbox Code Playgroud)

并重新启动sshd

remotey$ sudo service sshd restart
Run Code Online (Sandbox Code Playgroud)

现在再试一次,我们应该会看到我们所追求的效果:

lappy$ ssh -f -N -R remotey:12345:lappy:22 remotey
Run Code Online (Sandbox Code Playgroud)

这次在 remotey 上仔细检查一下:

remotey$ netstat -anp | grep 12345
tcp        0      0 192.168.1.3:12345           0.0.0.0:*                   LISTEN      9333/sshd
Run Code Online (Sandbox Code Playgroud)

注意:在上面我们可以看到sshd进程现在正在侦听 IP 地址为 192.168.1.3 的接口,以获取端口 12345 上的连接。

测试连接(部分双人)

现在在我们这次测试时更改了设置。主要区别在于我们不再需要连接到 localhost!

  1. 登录到 remotey

    [user@lappy ~]$ ssh remotey
    
    Run Code Online (Sandbox Code Playgroud)
  2. 测试反向连接

    [user@remotey ~]$ ssh -p 12345 remotey
    
    Run Code Online (Sandbox Code Playgroud)
  3. 现在应该回到 lappy

    root@remotey's password: 
    Last login: Wed Aug 21 01:49:10 2013 from remotey
    [user@lappy ~]$ 
    
    Run Code Online (Sandbox Code Playgroud)

参考