SSH反向隧道


ssh反向隧道:用于无法配置路由器做NAT映射,或者运营商不提供外网可访问的公网IP。
原理:内网服务器通过ssh连接到外网 某中转服务器,外网用户访问中转服务器即可连接到内网服务器。
配置说明:
1.中转服务器配置SSH转发功能和连接检查功能。
vi /etc/ssh/sshd_config 
GatewayPorts yes
ClientAliveInterval 30    #30秒检查一次。
ClientAliveCountMax 2 #2次客户端无响应则断开。
2.内网服务器配置
配置好ssh证书认证登陆。
临时连接可直接使用ssh命令:
    ssh -NfR 30038:localhost:30038 -p 62201 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 2" root@test.gateares.com
    ssh -NfR 30036:localhost:30036 -p 62201 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 2" root@test.gateares.com
    ssh -NfR 10021:localhost:10021 -p 62201 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 2" root@test.gateares.com
    ssh -NfR 10020:localhost:10020 -p 62201 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 2" root@test.gateares.com
    #-N:禁止执行远程命令
    #-f:后台运行
    #-R:远程转发
    #30038:localhost:30038 -p 62201  把中转服务器的30038端口转发到localhost:30038端口 -p表示ssh的连接端口。
    #-o 接配置参数30秒检查一次,服务器两次没相应断开连接
长期使用可使用autossh命令:
autossh -M 0 -NfR  50022:localhost:22 root@test.gateares.com -p 62201 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 2"
    #-M 0 :表示在ssh退出后会自动重启ssh.


D:\\ips\\local\\zabbix_agents\\rsync\\ssh.exe -p 62201 -R 10021:localhost:10021 root@test.gateares.com -i /cygdrive/d/key
ssh -fNR 50022:localhost:22 root@test.gateares.com -p 62201 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 2"