CentOS7通过反向SSH隧道(Reverse SSH Tunneling) 实现内网穿透

CentOS7通过反向SSH隧道(Reverse SSH Tunneling) 实现内网穿透

1、By default, SSH daemon is only listening on 127.0.0.1, so we won’t be able to access to our forwarded ports from outside. To get it listen on the interface connected to Internet we must enable GatewayPorts option in SSH server's configuration.
默认情况下,SSH守护程序只侦听127.0.0.1,所以我们将无法从外部访问我们转发的端口。 要让它在连接到Internet的接口上侦听,我们必须在SSH服务器的配置中启用GatewayPorts选项。

vim /etc/ssh/sshd_config
然后GatewayPorts yes在文件的底部添加。
或者sed -i s/"#GatewayPorts no"/"GatewayPorts yes"/g /etc/ssh/sshd_config

2、为防止SSH登录自动超时,修改下面参数每60秒会发一个KeepAlive请求
vim /etc/ssh/sshd_config
修改server端的配置文件/etc/ssh/sshd_config

# server每隔60秒给客户端发送一次保活信息包给客户端
ClientAliveInterval 60

# server端发出的请求客户端没有回应的次数达到86400次的时候就断开连接,正常情况下客户端都会相应
ClientAliveCountMax 86400

sed -i s/"#ClientAliveInterval 0"/"ClientAliveInterval 60"/g /etc/ssh/sshd_config
sed -i s/"#ClientAliveCountMax 3"/"ClientAliveCountMax 86400"/g /etc/ssh/sshd_config

3、ssh -fNR 公网服务器需要监听的端口:localhost:内网需要监听的端口 用户@公网服务器
-N:不执行何指令
-f:后台执行
-R:建立reverse tunnel

在内网服务器上执行,那么就可以通过公网服务器的1688端口来连接处于多层内网的服务器了
ssh -fNR 1688:localhost:22 root@122.114.224.224 -p 12345

公网服务器执行netstat -tulpn
tcp 0 0 0.0.0.0:1688 0.0.0.0:* LISTEN 2442/sshd

4、也可以用 autossh 来创建一个连接到公网服务器的永久 SSH 隧道。

5、Windows 通过隧道实现内网穿透
plink -R [Port to forward to on your VPS]:localhost:[Port to forward on your local machine] [VPS IP]
http://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html

6、这里是用CentOS7,当然也可以使用Ubuntu、Debian、Fedora等等

ssh -fNR 公网服务器需要监听的端口:localhost:内网需要监听的端口 用户@公网服务器

#本地22端口映射到服务器上面的1688端口
ssh -fN -R 122.114.224.224:1688:localhost:22 root@122.114.224.224 -p 12345

#本地80端口映射到服务器上面的18888端口,localhost 可以换成本地IP地址,或者局域网中的其他IP地址
ssh -fN -R 18888:localhost:80 root@122.114.224.224 -p 12345

#局域网中的远程桌面映射到服务器的3389端口
ssh -fN -R 3389:192.168.0.183:3389 root@122.114.224.224 -p 12345

#本地22端口映射到服务器上面的1688端口并打印调试信息
ssh -fN -R 122.114.224.224:1688:localhost:22 root@122.114.224.224 -p 12345 -v

转载至https://www.zhangfangzhou.cn/category/linux/centos/page/3#2

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值