假定有三台服务器host1,host2,host3.想要通过host1达到不了host3服务器,host2可以到达host3,这个时候host2作为ssh隧道的forward也就是转发服务器.为方便理解port对应服务器.
ssh 命令
-p 端口
-i
-F 秘钥文件
-N 不打开shell
-f 后台开启
-R 远程
-L 本地
T 不分配tty
首先设置host2隧道转发
vim /etc/ssh/sshd_config
设置
PermitTunnel yes #允许隧道转发
GatewayPorts yes #允许端口转发
PasswordAuthentication yes #允许使用密码登入服务器
本地转发
ssh -fNL port1:host3:port3 root@host2
本地转发本地端口,目标服务器ip,目标服务器端口
使用root链接forward服务器
注:必须是在host1上执行这条命令
port1 | port2 | sshd | run | forward | |
---|---|---|---|---|---|
host1 | yes | no | no | yes | no |
host2 | no | no | yes | no | yes |
host3 | no | listen | no | no | no |
远程转发
ssh -RNf port1:host2:host2 root@host1
远程转发host1的端口,目标服务器ip,目标服务器端口
使用root链接客户端服务器
相当于反向代理
注:必须是在host2上执行这条命令,host1必须有sshd,这里前提是host1和host2能相互访问.
port1 | port2 | sshd | run | forward | |
---|---|---|---|---|---|
host1 | yes | no | yes | no | no |
host2 | no | no | no | yes | yes |
host3 | no | listen | no | no | no |
实战
假设host1和host3分别在不同内网,想通过host1直接访问host3
第一步host3 链接forward
ssh -NfR 0.0.0.0:port2:127.0.0.1(host3):port3 root@host2
host3上执行,forward的port2在公网监听,通过port2转发到host3服务端口port3
在host1上执行 ssh -p port2 host2,可以直接访问到host2
第二步
在host1上执行
ssh -NfL port1-1:127.0.0.1(host1):port1-2 root@host2
这一步某些时候有用只是在本地端口port1-1 相当于在host1上执行
ssh -p port1-1 host1
开启目标服务器所有端口
ssh -D :1116 && \
ssh -RNf 9999(forword):127.0.0.1:1116 host(forword)