环境如下:
A机器两块网卡eth0(192.168.0.173)、eth1(192.168.100.1),eth0可以上外网,eth1仅仅是内部网络,B机器只有eth1(192.168.100.3),和A机器eth1可以通信互联,外网无法ssh进入B主机,可以使用ssh的反向隧道实现。
A:
1、首先在A 上编辑sshd 的配置文件/etc/ssh/sshd_config,将GatewayPorts 开关打开:
2、重启sshd服务,使用修改生效
-f 表示后台执行
-N 表示不执行任何命令
-R 建立反向隧道
1222 A机用来外面ssh的监听端口
-o ServerAliveInterval=300 的意思是让ssh client每300秒就给server发个心跳,以免链路被RST.
-f Requests ssh to go to background just before command execution. 让该命令后台运行 .
-n Redirects stdin from /dev/null (actually, prevents reading from stdin).
-N Do not execute a remote command. 不执行远程命令 .
-T Disable pseudo-tty allocation. 不占用 shell .
-g Allows remote hosts to connect to local forwarded ports.
3、外部主机连接B就直接连接A的1222端口就可以了,1222要被防火墙允许
4、A上查看端口的监听状态:
5、保持连接
我们需要这个隧道能够一直保持连接状态,在需要的时候可以随时接入,我们需要安装使用autossh
B:
外面ssh B:
添加服务:
B
创建以autosshuser 用户权限调用autosshd 的service 文件。将下面文本写入到文件/lib/systemd/system/autosshd.service,并设置权限为644:
A
使用这条反向隧道穿透B 所在的NAT SSH 连接到B
外部:
C主机:
通过ssh做端口转发代理上网:
C 是外面的电脑,A 是你的云主机,B 是你公司的电脑。这样做就可以给浏览器设置端口为3128 的sock4 本地(127.0.0.1)代理
浏览公司内网web
本文转自 喵来个鱼 51CTO博客,原文链接:http://blog.51cto.com/m51cto/2071530,如需转载请自行联系原作者