環境介紹:
主機 | 位置 | 公網 | 內網IP | 角色 |
host-a | 局域網1 | 否 | 192.168.0.1 | ssh client |
host-b | 局域網2、公網 | 是 | 192.168.1.1 | ssh server |
host-c | 局域網2 | 否 | 192.168.1.2 | ftp server |
-
動態轉發(socks5代理)
場景:host-a無法直接通過nat網關訪問一些互聯網資源
[root@host-a ~]# ssh -NfD *:1080 root@host-b
在host-a上面監聽socks5端口代理數據,數據通過SSH隧道動態轉發到host-c訪問互聯網資源, 常用於本地代理上網,相當於SS提供的功能
-
本地轉發(正向代理)
場景:host-a不允許直接訪問host-c的ftp服務
[root@host-a ~]# ssh -NfL *:21:192.168.1.2:21 root@host-b
在host-a上面監聽21端口代理數據,通過SSH隧道安全與host-b傳輸數據,常用於正向一對一代理,相當於iptables dnat
-
遠程轉發(反向代理)
場景:要從公網連接host-a ssh服務,但是nat網關無法映射端口
#修改SSH server的配置文件(否則sshd只能監聽127.0.0.1)
[root@host-b ~]# vim /etc/ssh/sshd_config
GatewayPorts yes
[root@host-a ~]# ssh -NfR *:2222:127.0.0.1:22 root@host-b
或
[root@host-a ~]# ssh -NfR *:2222:192.168.1.1:22 root@host-b
在host-b上面監聽2222端口,數據通過SSH隧道穿透nat設備訪問host-a的22端口,常用於內網穿透,相當於frp、ngrok