autossh 内网穿透
原理
现有如下三台服务器
服务器 | 描述 |
---|---|
aliyun | 外网服务器,默认端口都开放(CentOS) |
inner | 内网服务器,能访问外网(CentOS) |
local | 用户本地 |
大致流程:
inner
通过autossh
与aliyun
建立长链接local
可以访问aliyun
- 再由
aliyun
通过autossh
链接访问到inner
实现
-
在
inner
中,安装autossh
yum -y install autossh
-
在
inner
生成ssh公钥,并复制到aliyun
中ssh-keygen -t rsa ssh-copy-id -i ~/.ssh/id_rsa.pub -p 22 [aliyun用户名]@[aliyun的IP] # root@192.168.10.111
-
在
aliyun
中配置ssh
参数vim /etc/ssh/sshd_config # 检查如下配置是否开启 systemctl restart sshd # 重启ssh服务
GatewayPorts yes TCPKeepAlive yes ClientAliveInterval 60 ClientAliveCountMax 3 PubkeyAuthentication yes AuthorizedKeysFile .ssh/authorized_keys
-
在
inner
中调用autossh
实现反向代理autossh -p 22 -M 9997 -fNR 9998:localhost:22 [aliyun用户名]@[aliyun的IP]
将inner的22端口,映射到aliyun的9998端口
-
在
local
通过aliyun
访问inner
ssh -p9998 [aliyun用户名]@[aliyun的IP]