知乎专栏 |
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 不会退出。
编辑 /etc/ssh/sshd_config 文件,修改 GatewayPorts 配置项为 yes
GatewayPorts yes
如果没有配置 GatewayPorts yes 所有映射端口为 127.0.0.1:XXXX,配置 GatewayPorts yes 后可默认是 *:XXXX 以绑定任意接口。
重启 sshd
systemctl reload sshd.service
建立链接
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