SSH反向隧道的内网穿透

简介:

环境如下:

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


使用这条反向隧道穿透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



本文转自 喵来个鱼 51CTO博客,原文链接:http://blog.51cto.com/m51cto/2071530,如需转载请自行联系原作者

相关文章
|
7月前
|
网络协议 Shell 网络安全
在外SSH远程连接macOS服务器【cpolar内网穿透】
macOS系统自带有Secure Shell 客户端,它可让您登录到侦听传入SSH连接的远程服务器和台式机。我们可以用ssh username@ip来ssh到服务器,但通常局限于局域网内的远程。
68 0
|
7月前
|
网络协议 Ubuntu 网络安全
使用VScode SSH公网远程连接本地服务器开发【无公网IP内网穿透】
使用VScode SSH公网远程连接本地服务器开发【无公网IP内网穿透】
|
4月前
|
网络协议 安全 网络安全
Android Termux安装SSH结合内网穿透实现远程SFTP文件传输
Android Termux安装SSH结合内网穿透实现远程SFTP文件传输
55 0
|
4月前
|
运维 监控 网络安全
小米路由器R2D开启SSH+Frp内网穿透,让爸妈在老家轻松追剧
小米路由器R2D开启SSH+Frp内网穿透,让爸妈在老家轻松追剧
185 0
|
3天前
|
安全 Linux 网络安全
|
9天前
|
Ubuntu 网络安全 数据安全/隐私保护
使用SSH隧道将Ubuntu云服务器Jupyter Notebook端口映射到本地
这样,你就成功地将Ubuntu云服务器上的Jupyter Notebook端口映射到本地,使你能够通过本地浏览器访问并使用Jupyter Notebook。
30 1
|
20天前
|
网络协议 安全 Linux
IDEA通过内网穿透实现固定公网地址远程SSH连接本地Linux服务器
IDEA通过内网穿透实现固定公网地址远程SSH连接本地Linux服务器
|
20天前
|
存储 网络协议 Linux
如何使用内网穿透工具实现远程SSH访问Deepin系统
如何使用内网穿透工具实现远程SSH访问Deepin系统
|
7月前
|
网络协议 网络安全 数据安全/隐私保护
内网穿透——SSH远程连接树莓派
内网穿透——SSH远程连接树莓派
|
4月前
|
网络协议 关系型数据库 Linux
无需公网IP,在家SSH远程连接公司内网服务器「cpolar内网穿透」
本次教程我们来实现如何在外公网环境下,SSH远程连接家里/公司的Linux CentOS服务器,无需公网IP,也不需要设置路由器。