上下文如下:
我在家里的一台设备上安装了 Ubuntu 服务器,并在这台设备中设置了一个 ssh 服务器。如果客户端设备(我的情况是笔记本电脑)位于同一个本地网络中,我可以通过 ssh 毫无问题地连接到这台 Ubuntu 服务器。由于我想远程访问服务器,我安装了 openvpn3,此连接也运行正常。但是当我尝试 ssh(连接到另一个网络,我们称之为“网络 B”,服务器位于该网络,但 VPN 连接处于活动状态)时,我收到以下消息:
ssh: connect to host 192.168.0.200 port 22: No route to host
我猜是因为网络 B 的网关试图路由到网络 B 的 LAN 内的设备,而不是服务器 LAN。但我需要访问服务器的 LAN,才能通过 ssh 连接到我为服务器设置的私有静态 IP 地址。
服务器的公网IP:
$ curl ifconfig.me
181.31.117.40
回答@cocomac
只是为了检查一下显而易见的东西...是 192.168.0. 200 还是 192.168.1.200?通常 192.168.1.XYZ 是默认值 –
192.168.0.200
服务器 IP 的证明:
$ ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: enp37s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 2c:f0:5d:78:39:a8 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.200/24 brd 192.168.0.255 scope global dynamic enp37s0
valid_lft 2528sec preferred_lft 2528sec
inet6 fe80::2ef0:5dff:fe78:39a8/64 scope link
valid_lft forever preferred_lft forever
当我的笔记本电脑位于同一局域网时,使用 SSH:
sebastian@sebastian-Vostro-3405:~$ ssh [email protected]
Welcome to Ubuntu 20.04.4 LTS (GNU/Linux 5.4.0-100-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
System information as of Mon 28 Feb 2022 12:54:15 AM UTC
System load: 0.09
Usage of /: 13.5% of 97.93GB
Memory usage: 45%
Swap usage: 0%
Processes: 311
Users logged in: 0
IPv4 address for br-5c5a41a7cc73: 172.19.0.1
IPv4 address for br-974f16cf42ed: 172.18.0.1
IPv4 address for docker0: 172.17.0.1
IPv4 address for enp37s0: 192.168.0.200
* Super-optimized for small spaces - read how we shrank the memory
footprint of MicroK8s to make it the smallest full K8s around.
https://ubuntu.com/blog/microk8s-memory-optimisation
0 updates can be applied immediately.
Last login: Mon Feb 28 00:48:56 2022 from 192.168.0.110
sebademasi10@home:~$
现在,换成我的邻居的网络,我连接了VPN:
~$ openvpn3 session-start -c home
Using pre-loaded configuration profile 'home'
Session path: /net/openvpn/v3/sessions/ca62d16csfcfcs43f1sa320s14f8bd92193e
Connected
检查公网 IP:
sebastian@sebastian-Vostro-3405:~$ curl ifconfig.me
181.31.117.40
ip route
:
$ ip route
0.0.0.0/1 via 192.168.255.5 dev tun0
default via 192.168.0.1 dev wlp3s0 proto dhcp metric 600
128.0.0.0/1 via 192.168.255.5 dev tun0
169.254.0.0/16 dev wlp3s0 scope link metric 1000
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 linkdown
172.18.0.0/16 dev br-aa3d18e9a788 proto kernel scope link src 172.18.0.1 linkdown
172.19.0.0/16 dev br-7c64d12635b8 proto kernel scope link src 172.19.0.1 linkdown
172.20.0.0/16 dev br-118925d85563 proto kernel scope link src 172.20.0.1 linkdown
181.31.117.40 via 192.168.0.1 dev wlp3s0
192.168.0.0/24 dev wlp3s0 proto kernel scope link src 192.168.0.110 metric 600
192.168.255.1 via 192.168.255.5 dev tun0
192.168.255.4/30 dev tun0 proto kernel scope link src 192.168.255.6
笔记:
- VPN 服务器在 Docker 容器内运行
我希望这很容易理解,如果您认为需要任何其他详细信息,请随时询问。提前谢谢您。
答案1
我已经通过手动添加(连接到 VPN 后)路由解决了这个问题,如下所示:
ip route add 192.168.0.200 via 192.168.255.5
在哪里:
- 192.168.0.200 是服务器的私有 IP
- 192.168.255.5 是服务器网络中网关的 IP
我通过检查意识到了这一点ip route
:
$ ip route
0.0.0.0/1 via 192.168.255.5 dev tun0
default via 192.168.0.1 dev wlp3s0 proto dhcp metric 600
128.0.0.0/1 via 192.168.255.5 dev tun0
169.254.0.0/16 dev wlp3s0 scope link metric 1000
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 linkdown
172.18.0.0/16 dev br-aa3d18e9a788 proto kernel scope link src 172.18.0.1 linkdown
172.19.0.0/16 dev br-7c64d12635b8 proto kernel scope link src 172.19.0.1 linkdown
172.20.0.0/16 dev br-118925d85563 proto kernel scope link src 172.20.0.1 linkdown
181.31.117.40 via 192.168.0.1 dev wlp3s0
192.168.0.0/24 dev wlp3s0 proto kernel scope link src 192.168.0.110 metric 600
192.168.0.200 via 192.168.255.5 dev tun0
192.168.255.1 via 192.168.255.5 dev tun0
192.168.255.4/30 dev tun0 proto kernel scope link src 192.168.255.6