切记:该方法不可用于生产!因为非常不稳定!只能用于开发
1. 把服务器的 SSH KEY 添加到云服务器
$ ssh root@10.x.x.x
$ cat /root/.ssh/id_rsa.pub
添加到云服务器的 /root/.ssh/authorized_keys 文件中
2. 在服务器创建 SSH Config 文件
$ ssh root@10.x.x.x
$ cat /root/.ssh/config
## 服务器SSH隧道专用
## 公网的IP:119.3.x.x.x
## 防火墙开通的端口:40800-40900
Host test-ssh-app
HostName 119.3.x.x
Port 22
User root
IdentityFile ~/.ssh/id_rsa
ExitOnForwardFailure yes
ServerAliveInterval 60
ServerAliveCountMax 60
StrictHostKeyChecking no
UserKnownHostsFile /dev/null
LogLevel ERROR
## 示例:把 10.x.x.x:22 映射到华为服务器的 119.3.x.x:40800
RemoteForward 40800 10.x.x.x:22
3. 在服务器安装 AutoSSH
$ yum info autossh
$ yum install autossh
4. 在服务器添加 Crontab
SHELL=/usr/bin/bash
AUTOSSH_GATETIME=0
@reboot /usr/bin/autossh -M 0 -NTC test-ssh-app
如果手动运行,则命令行稍有不同:
$ /usr/bin/autossh -f -M 0 -NTC test-ssh-app