首先在内网主机上执行该命令:
ssh -R <remote_port>:<local_host>:<local_port> <user>@<remote_host> -p <ssh_port>
remote_host: 是外网vps的IP
remote_port:是准备映射到VPS的哪个端口
local_host:内网主机的IP
local_port: 内网主机的端口
举个实际的例子:
ssh -R 10000:127.0.0.1:8089 egg@112.24.4.222 -p 22
就是把内网主机的8089端口映射到外网服务器(112.24.4.222)的10000端口上
以上面的命令为例,ssh命令执行成功后,如果在服务器的内网下可以访问127.0.0.1:10000
,但是在访问不了112.24.4.222:10000
则需要把/etc/ssh/sshd_config
配置文件下的GatewayPorts
属性修改为yes
GatewayPorts:
Specifies whether remote hosts are allowed to connect to ports forwarded for the client. 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. 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. The default is ‘no’.
最后使用下面的命令重新加载ssh的配置
# sudo reload ssh