您的位置:首页 > 其它

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 开关打开:

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 autossh