SSH反向隧道的内网穿透
2018-02-13 16:12
555 查看
环境如下:
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:
B:
A:
外面ssh B:
添加服务:
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
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 开关打开:
vim /etc/ssh/sshd_config GatewayPorts yes
2、重启sshd服务,使用修改生效
systemctl restart sshd
ssh -ngfNTR 1222:192.168.100.3:22 root@192.168.0.173 -o ServerAliveInterval=300
-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.
[root@aiker01 ~]# netstat -antp | grep 1222 tcp 0 0 0.0.0.0:1222 0.0.0.0:* LISTEN 16182/sshd: root tcp6 0 0 :::1222 :::* LISTEN 16182/sshd: root
3、外部主机连接B就直接连接A的1222端口就可以了,1222要被防火墙允许
ssh -p 1222 root@192.168.0.173
root@aiker:/mnt/c/Users/aikera# ssh -p 1222 root@192.168.0.173 Last failed login: Tue Feb 13 11:19:53 CST 2018 from gateway on ssh:notty There was 1 failed login attempt since the last successful login. Last login: Tue Feb 13 10:52:09 2018 from gateway [root@aiker03 ~]#
4、A上查看端口的监听状态:
[root@aiker01 ~]# netstat -antp | grep 1222 tcp 0 0 0.0.0.0:1222 0.0.0.0:* LISTEN 16182/sshd: root tcp 0 0 192.168.0.173:1222 192.168.0.190:60738 ESTABLISHED 16182/sshd: root tcp6 0 0 :::1222 :::* LISTEN 16182/sshd: root
5、保持连接
我们需要这个隧道能够一直保持连接状态,在需要的时候可以随时接入,我们需要安装使用autossh
B:
yum install autossh -y
B:
autossh -p 22 -M 6777 -fNR 1322:127.0.0.1:22 root@192.168.0.173 #-M 参数指定的端口用来监听隧道的状态,与端口转发无关;同时需要在A防火墙打开1322端口主机之间可以使用不用密码的key
The authenticity of host '192.168.0.173 (192.168.0.173)' can't be established. ECDSA key fingerprint is d5:1c:36:d7:57:64:3d:5b:8a:e8:aa:93:54:1d:8c:22. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.0.173' (ECDSA) to the list of known hosts. Enter passphrase for key '/root/.ssh/id_rsa':
A:
[root@aiker01 ~]# netstat -antp | grep 1322 tcp 0 0 0.0.0.0:1322 0.0.0.0:* LISTEN 16798/sshd: root tcp6 0 0 :::1322 :::* LISTEN 16798/sshd: root
外面ssh B:
root@aiker:/mnt/c/Users/aikera# ssh -p 1322 root@192.168.0.173 root@192.168.0.173's password: Last login: Tue Feb 13 15:29:30 2018 from gateway [root@aiker03 ~]#
添加服务:
B:
useradd autosshuser passwd autosshuser
su - autossh ssh-keygen -t rsa #生成密匙对,按回车,不使用密码的密匙对 ssh-copy-id root@192.168.0.173 #copy密匙到A
B
创建以autosshuser 用户权限调用autosshd 的service 文件。将下面文本写入到文件/lib/systemd/system/autosshd.service,并设置权限为644:
[Unit]Description=Auto SSH Tunnel After=network-online.target [Service] User=autosshuser Type=simple ExecStart=/bin/autossh -p 22 -M 5689 -NR '*1322:127.0.0.1:22' usera@192.168.0.173 -i /home/autossh/.ssh/id_rsa ExecReload=/bin/kill -HUP $MAINPID KillMode=process Restart=always [Install] WantedBy=multi-user.target WantedBy=graphical.target
systemctl enable autosshd #允许自启动
systemctl start autosshd
A
使用这条反向隧道穿透B 所在的NAT SSH 连接到B
ssh -p 1322 root@localhost
外部:
ssh -p 1322 root@192.168.0.173
C主机:
通过ssh做端口转发代理上网:
ssh -p 1322 -qngfNTD 3128 userb@192.168.0.173
C 是外面的电脑,A 是你的云主机,B 是你公司的电脑。这样做就可以给浏览器设置端口为3128 的sock4 本地(127.0.0.1)代理
浏览公司内网web
相关文章推荐
- 使用SSH反向隧道进行内网穿透(远程端口转发)
- 使用SSH反向隧道进行内网穿透
- 内网穿透系列——SSH反向隧道 (最简单的内网穿透方案)
- SSH 反向隧道 内网穿透,AutoSSH自动重连
- ssh 反向隧道访问内网机器
- ssh反向隧道实验——没有端口映射的前提下,让外网访问内网服务器
- Ubuntu SSH反向隧道从外网连接内网
- 内网接口调用,ssh反向隧道与nginx反向代理
- 用ssh反向隧道访问内网机器
- 用ssh反向隧道访问内网机器
- 用ssh反向隧道访问内网机器
- SSH隧道与端口转发及内网穿透
- 用ssh反向隧道访问内网机器
- 使用 ssh -R 穿透局域网访问内部服务器主机,反向代理 无人值守化
- 使用SSH反向隧道进行内网穿透
- ssh 反向代理 外网访问内网
- 使用SSH反向隧道进行内网穿透
- 【Linux】ssh建立隧道tunnel连接到内网设备
- 通过ssh反向连接内网主机的方法(防火墙的主机)