平臺: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