Home | 简体中文 | 繁体中文 | 杂文 | Github | 知乎专栏 | 51CTO学院 | CSDN程序员研修院 | OSChina 博客 | 腾讯云社区 | 阿里云栖社区 | Facebook | Linkedin | Youtube | 打赏(Donations) | About
知乎专栏多维度架构

32.4. OpenSSH Tunnel

32.4.1. SOCKS v5 Tunnel

			
ssh -D 1080 <远程主机地址>
or
ssh -D 7070 <远程主机地址>
			
			

I prefer 1080 to 7070. the reason is 1080 default for SOCKS port.

			
ssh neo@www.example.com -D 1080
			
			
			
			
ssh -D 1080 -f -C -q -N neo@example.com			
			
			



Explanation of arguments

-D: Tells SSH that we want a SOCKS tunnel on the specified port number (you can choose a number between 1025-65536)
-f: Forks the process to the background
-C: Compresses the data before sending it
-q: Uses quiet mode
-N: Tells SSH that no command will be sent once the tunnel is up

脚本

			
ssh -D 1080 -f -C -q -N neo@vpn.netkiller.cn
pkill ping
ping -i 30 8.8.8.8 > /dev/null &			
			
			

ping 是保持隧道活跃,每个 30秒 ping 访问一次外部主机以保持 ssh 不会退出。

32.4.2. 从公网穿透局域网

WAN 服务区

编辑 /etc/ssh/sshd_config 文件,修改 GatewayPorts 配置项为 yes

				
GatewayPorts yes			
				
				

如果没有配置 GatewayPorts yes 所有映射端口为 127.0.0.1:XXXX,配置 GatewayPorts yes 后可默认是 *:XXXX 以绑定任意接口。

重启 sshd

				
systemctl reload sshd.service			
				
				
LAN 服务器

建立链接

				
ssh -NTf -R 3306:127.0.0.1:3306 root@www.netkiller.cn
# 多个端口可以这样写
ssh -NTCf -R 80:192.168.10.10:80 -R 443:192.168.10.10:443 root@www.netkiller.cn
				
				
MySQL 应用案例

mysql tunnel

		
$ ssh -L 3306:127.0.0.1:3306 user@example.org
		
				

testing

		
$ mysql -h 127.0.0.1 -uroot -p test