如何访问没有固定公网 IP 的内网服务器 -- SSH 远程转发搞定

场景:小明同学在家里和办公室里各有一台 Linux 设备,但是在公司的时候有时需要连接到家里的 Linux ,而在家里的时候有时也需要连接到公司里的 Linux ,苦于公司和家里都没有固定公网 IP 地址,该如何解决这个问题呢?可以使用一台有公网 IP 的云服务器 + SSH 的远程转发功能来解决。
示意图如下:
在这里插入图片描述
*重要说明:云服务器的安全组策略需开放对应代理端口,如 88

一、云服务器启用 GatewayPorts

默认情况下,OpenSSH 只允许从服务器主机连接到远程转发端口

[root@cloud ~]# echo "GatewayPorts yes" >> /etc/ssh/sshd_config
[root@cloud ~]# systemctl restart sshd

二、办公室中的服务器运行命令,开启远程端口转发

[root@office ~]# ssh -R 0.0.0.0:88:127.0.0.1:22 root@134.175.220.42

说明:
1、 0.0.0.0 表示任意 IP 均可连接
2、 88 表示映射到云服务器中的端口
3、 127.0.0.1 表示办公司服务器的地址
4、 22 表示办公司服务器的端口
5、 root@134.175.220.42 表示云服务器的登录方式

此时,云服务器新增端口监听 88

[root@cloud ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      24659/docker-proxy  
tcp        0      0 0.0.0.0:81              0.0.0.0:*               LISTEN      28911/docker-proxy  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      32440/sshd          
tcp        0      0 0.0.0.0:88              0.0.0.0:*               LISTEN      10151/sshd: root@pt 
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1429/master         
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      8837/nginx: worker  
tcp6       0      0 :::3306                 :::*                    LISTEN      24665/docker-proxy  
tcp6       0      0 :::81                   :::*                    LISTEN      28917/docker-proxy  
tcp6       0      0 :::22                   :::*                    LISTEN      32440/sshd          
tcp6       0      0 :::88                   :::*                    LISTEN      10151/sshd: root@pt 
tcp6       0      0 ::1:25                  :::*                    LISTEN      1429/master 

三、家中的服务器连接办公司的服务器

[root@home ~]# ssh -p 88 root@134.175.220.42
The authenticity of host '[134.175.220.42]:88 ([134.175.220.42]:88)' can't be established.
ECDSA key fingerprint is SHA256:ZxLzakxf0cI96FLJAbW1aWHqHzx4nrsZ+u0YOD/ALIM.
ECDSA key fingerprint is MD5:a2:cb:63:76:5e:db:50:a5:7e:e6:cc:d6:5a:1d:78:8c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[134.175.220.42]:88' (ECDSA) to the list of known hosts.
root@134.175.220.42's password: 
Last login: Tue Jun 13 21:32:00 2023 from 10.1.240.90
[root@office ~]# 
  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值