1、简介
问题来源:在家里的电脑,希望从办公的网络可以访问;手里还有一个云虚拟机,云有公网地址。
解决方案:使用ssh做端口转发;
2、步骤
1)环境:
家里的电脑:CentOS7的系统,地址是192.168.1.100
云:CentOS7的系统,地址是1.1.1.1
2)家里电脑执行
》》建立反链,命令如下:命令执行后,在后台建立了一个连接;
ssh -fCNR 2222:localhost:22 -i ~/.ssh/id_rsa aliyun用户A@1.1.1.1
》》参数说明:
2222:是云的反向代理的端口,任意设置的,用来代理设备云机器22端口的端口
-i:使用私钥连接服务器
id_rsa:建立的私钥,公钥已经放在了云服务器上
3)云执行
》》执行命令,从云可以连接到家里的服务器
sshpass -p '家里系统的密码' ssh 家里系统某用户@localhost -p 2222
》》参数说明
sshpass 可以直接输入命令,避免等待ssh弹出后输入密码
3、其他
1)每隔一段时间检查一下,若断开连接,就自动重连
[xx@xxxx work]$ crontab -l
*/30 * * * * curl -o /home/xxx/remoteIP.png http://www.xxxxx.com/nginx-logo.png > /dev/null 2>&1
*/5 * * * * sh /home/xxx/work/autossh.sh > /dev/null 2>&1
[xx@xxxx work]$ cat /home/xxx/work/autossh.sh
#!/bin/bash
ssh_d_process_num=`ps aux|grep -E 'ssh \-' |grep -v grep |wc -l`
if [ "$ssh_d_process_num" == "0" ]; then
ssh -fCNR 2222:localhost:22 -i ~/.ssh/id_rsa xxxxxx@1.1.1.1
fi
[xx@xxxx work]$2)ssh反链的用途
》》端口反链用于SSH
》》端口反链用于HTTP
linux通过ssh实现反向连接)主要参考1:https://blog.csdn.net/u010817321/article/details/52119654
SSH -R 反向端口转发)参考2:https://blog.csdn.net/autoliuweijie/article/details/80283689
sshpass使用)参考3:https://www.cnblogs.com/misswangxing/p/10637718.html
CentOS7下使用ssh私钥登录)参考4:https://blog.csdn.net/z_ipython/article/details/93649245
时间:2021-03-01