SSH 远程访问内网机器

通过 SSH 访问位于路由器后方的机器

典型场景是家里放置一台高性能 Linux 服务器,偶尔需要在外面远程访问。这时,需要一台带有公网 IP 的 VPS。

配置 VPS

打开 VPS 的 GatewayPorts 功能

1
sudo emacs /etc/ssh/sshd_config

修改:

1
GatewayPorts yes

Linux 服务器建立方向隧道

考虑到网络的波动会导致 ssh 断联,可以使用 autossh 规避该问题

1
2
# 语法:ssh -NfR [VPS监听端口]:[内网目标IP]:[内网目标端口] [VPS用户]@[VPS地址]
ssh -NfR 8080:localhost:22 user@vps-public-ip

远程访问

1
2
# 直接连接 VPS 的 8080 端口
ssh -p 8080 internal-user@vps-public-ip