安装autossh:
yum install autossh #centos
apt-get install autossh #ubuntu
使用ssh建立tunnel,在shell中执行:
ssh -NfR 1234:localhost:22 user@masterIp
##隧道开启:将node的22端口映射到master的1234端口,此时在master上ssh localhost -p 1234就能连接到node
使用autossh建立永久tunnel
autossh -M 5678 -NfR 1234:localhost:22 user@masterIp
## 5678为监控端口,此方法需要输入密码
使用autossh建立永久tunnel 无需密码
1.在master主机上生产公钥和私钥
$ ssh-keygen ...(一直按Enter,最后在~/.ssh/下生成密钥) $ ls ~/.ssh/ id_rsa id_rsa.pub known_hosts
2.复制B主机上生成的id_rsa.pub公钥到外网A主机上,并将内容加入到~/.ssh/authorized_keys中
$ cat id_rsa.pub >> ~/.ssh/authorized_keys
3. 然后执行
autossh -M 5678 -NfR 1234:localhost:22 user@masterIp
## 若是没有吧pub文件移动到authorized_keys中 需要在命令后加上-i参数 后跟pub 的文件路径
autossh -M 5678 -NfR 1234:localhost:22 user@masterIp -i id_rsa.pub