autossh 内外网穿透配置

AutoSSH 能让 SSH 隧道一直保持执行,他会启动一个 SSH 进程,并监控该进程的健康状况;当 SSH 进程崩溃或停止通信时,AutoSSH 将重启动 SSH 进程

1.  安装autossh

[root@localhost ~]# apt install autossh

2.  配置SSH免密登录

[root@localhost ~]# ssh-keygen

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /root/.ssh/id_rsa.

Your public key has been saved in /root/.ssh/id_rsa.pub.

The key fingerprint is:SHA256:0GkZLmelyX6WVKK=============

将密钥拷贝到远程服务器上。假设ip:123.123.123.123

[root@localhost ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub root@123.123.123.123

3. autossh的配置

autossh -M 5678 -NR 1234:localhost:8080 -f [email protected]

备注:

5678 端口:负责通过这个端口监视连接状态,连接有问题时就会自动重连

1234 端口:远程服务器的端口

localhost:8080: 本地或内网IP地址、端口

-f : 后台运行

如果想要断开AUTOSSH的隧道连接,只需要把 AUTOSSH监听端口的进程 kill 掉就可以了

例如

[root@localhost ~]# netstat -apn | grep 5678
tcp        0      0 127.0.0.1:5678          0.0.0.0:*               LISTEN     8843/ssh
tcp6       0      0 ::1:5678                :::*                    LISTEN      8843/ssh
[root@localhost ~]# kill -9 8843

为查看内外网穿透是否成功,可通过netstat -atunp命令查看,成功效果如下:

4.可能存在的问题(随时更新)

配置完AUTO SSH之后,发现还是无法穿透,解决方法是配置下ssh, 开启 GatewayPorts 参数即可。

#修改配置

[root@localhost ~] vi /etc/ssh/sshd_config

GatewayPorts yes

#重启SSHD

[root@localhost ~]  systemctl restart sshd.service 或者 sudo /etc/init.d/ssh restart

GatewayPorts原理:

当请求一个TCP端口的时候,默认情况下,SSH只监听本机地址,这就导致AUTOSSH虽然穿透到阿里云服务器,但是外网还是无法通过映射的端口 访问局域网资源。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章