ssh隧道,实现到VPS的端口转发,用于公网访问本地服务

平台:Ubuntu14.04
软件:openssh_6.6.1p1
这种方法通畅可以轻松实现本地服务在公网环境下的访问

1、SSH转发

通过查看openssh的manual,发现以下几个参数:

ssh -p [serverport] -C -g -f -N -R [remoteport]:[localaddress]:[localport] root@[remoteaddress]

参数解释:
-p 默认22,以远程server端为准
-C 文章说是采用压缩传输,我没有深究
-g-L/-R/-D配合使用,否则仅提供本地主机连接
-f 后台认证用户密码,和-N结合使用
-N 不执行脚本或命令。如此做,连接成功后回到shell
-R 本地主机连接远程主机,如果需要远程主机连接本地则考虑其他参数

然而,在server端看到了服务端口,外网访问不到
后来发现openssh默认配置绑定到了本地回环接口上:
(应该是下面的0.0.0.0)

tcp    0       0 127.0.0.1:2222    0.0.0.0:*
tcp    0       0 0.0.0.0:2222      0.0.0.0:*

2、绑定0.0.0.0

查找官方文档,发现了关于gatewayports的说明:

GatewayPorts GatewayPorts Specifies whether remote hosts are allowed to connect to ports forwarded for the client. By default, sshd binds remote port forwardings to the loopback address. 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. 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.

增加gatewayports配置项,重启:

vim /etc/ssh/sshd_config
#GatewayPorts yes
/etc/init.d/ssh restart

More info: Click

你可能感兴趣的:(ssh隧道,实现到VPS的端口转发,用于公网访问本地服务)