我有一个设备,配置了静态 IP 地址 (192.168.1.16),它直接连接到我的 Ubuntu Linux PC 上的第四个端口 (enp4s0)。 m/c 还通过第三个端口 (enp3s0) 连接到我的 LAN。我的 LAN 使用 DHCP 在 192.168.1.1/24 网络上分配 IP。
所以现在我希望能够通过 enp4s0 在 192.168.1.19 上 ping 我的设备,因此我使用此命令通过单个 IP 地址添加到它的路由:
sudo ip route add 192.168.1.19/32 dev enp4s0
不幸的是 ping 失败:
$ ping 192.168.1.19
PING 192.168.1.19 (192.168.1.19) 56(84) bytes of data.
From 192.168.1.168 icmp_seq=1 Destination Host Unreachable
From 192.168.1.168 icmp_seq=2 Destination Host Unreachable
From 192.168.1.168 icmp_seq=3 Destination Host Unreachable
查看一些状态,我看到它说链接已关闭:
$ ip route
default via 192.168.1.1 dev enp3s0 proto dhcp metric 101
default via 10.136.209.45 dev wwan0 proto static metric 700
10.136.209.40/29 dev wwan0 proto kernel scope link src 10.136.209.44 metric 700
169.254.0.0/16 dev enp3s0 scope link metric 1000
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1
192.168.1.0/24 dev enp3s0 proto kernel scope link src 192.168.1.168 metric 101
192.168.1.19 dev enp4s0 scope link linkdown
$ ip address
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: enp1s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN group default qlen 1000
link/ether 78:d0:04:31:91:bd brd ff:ff:ff:ff:ff:ff
3: enp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 78:d0:04:31:91:be brd ff:ff:ff:ff:ff:ff
4: enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 78:d0:04:31:91:bf brd ff:ff:ff:ff:ff:ff
inet 192.168.1.168/24 brd 192.168.1.255 scope global dynamic noprefixroute enp3s0
valid_lft 85575sec preferred_lft 85575sec
inet6 fe80::1191:4ec4:6844:2273/64 scope link noprefixroute
valid_lft forever preferred_lft forever
5: enp4s0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc mq state DOWN group default qlen 1000
link/ether 78:d0:04:31:91:c0 brd ff:ff:ff:ff:ff:ff
6: wwan0: <BROADCAST,MULTICAST,NOARP,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UNKNOWN group default qlen 1000
link/ether e6:83:24:bc:f6:81 brd ff:ff:ff:ff:ff:ff
inet 10.136.209.44/29 brd 10.136.209.47 scope global noprefixroute wwan0
valid_lft forever preferred_lft forever
7: docker0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default
link/ether 02:42:f1:f6:93:0b brd ff:ff:ff:ff:ff:ff
inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
valid_lft forever preferred_lft forever
inet6 fe80::42:f1ff:fef6:930b/64 scope link
valid_lft forever preferred_lft forever
9: veth36abd25@if8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue master docker0 state UP group default
link/ether ae:27:23:82:70:6a brd ff:ff:ff:ff:ff:ff link-netnsid 0
inet6 fe80::ac27:23ff:fe82:706a/64 scope link
valid_lft forever preferred_lft forever
我是否还缺少其他操作才能访问 192.168.1.19 的设备?
更新:
在尝试更换一些电缆后,我发现连接器的逻辑顺序不正确!事实上,它们不是 1,2,3,4,而是 1,4,3,2。
所以我以为是enp4s0,其实是en2s0。摆脱了这种混乱之后,现在似乎另一个问题如此处所示:
$ sudo ip route add 192.168.1.19/32 dev enp2s0
$ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default setup.ubnt.com 0.0.0.0 UG 102 0 0 enp3s0
default _gateway 0.0.0.0 UG 700 0 0 wwan0
10.63.138.32 0.0.0.0 255.255.255.248 U 700 0 0 wwan0
link-local 0.0.0.0 255.255.0.0 U 1000 0 0 enp3s0
172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
192.168.1.0 0.0.0.0 255.255.255.0 U 102 0 0 enp3s0
eZ80Acclaim 0.0.0.0 255.255.255.255 UH 0 0 0 enp2s0
$ ping 192.168.1.19
PING 192.168.1.19 (192.168.1.19) 56(84) bytes of data.
64 bytes from 192.168.1.19: icmp_seq=1 ttl=250 time=1.01 ms
64 bytes from 192.168.1.19: icmp_seq=2 ttl=250 time=1.06 ms
64 bytes from 192.168.1.19: icmp_seq=3 ttl=250 time=1.18 ms
64 bytes from 192.168.1.19: icmp_seq=4 ttl=250 time=1.27 ms
64 bytes from 192.168.1.19: icmp_seq=5 ttl=250 time=1.18 ms
64 bytes from 192.168.1.19: icmp_seq=6 ttl=250 time=1.04 ms
64 bytes from 192.168.1.19: icmp_seq=7 ttl=250 time=1.17 ms
64 bytes from 192.168.1.19: icmp_seq=8 ttl=250 time=1.18 ms
64 bytes from 192.168.1.19: icmp_seq=9 ttl=250 time=1.05 ms
64 bytes from 192.168.1.19: icmp_seq=10 ttl=250 time=1.17 ms
64 bytes from 192.168.1.19: icmp_seq=11 ttl=250 time=1.43 ms
64 bytes from 192.168.1.19: icmp_seq=12 ttl=250 time=0.941 ms
From 192.168.1.168 icmp_seq=13 Destination Host Unreachable
From 192.168.1.168 icmp_seq=14 Destination Host Unreachable
From 192.168.1.168 icmp_seq=15 Destination Host Unreachable
^C
--- 192.168.1.19 ping statistics ---
18 packets transmitted, 12 received, +3 errors, 33.3333% packet loss, time 17129ms
rtt min/avg/max/mdev = 0.941/1.139/1.433/0.124 ms, pipe 4
$ route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default setup.ubnt.com 0.0.0.0 UG 102 0 0 enp3s0
default _gateway 0.0.0.0 UG 700 0 0 wwan0
10.63.138.32 0.0.0.0 255.255.255.248 U 700 0 0 wwan0
link-local 0.0.0.0 255.255.0.0 U 1000 0 0 enp3s0
172.17.0.0 0.0.0.0 255.255.0.0 U 0 0 0 docker0
192.168.1.0 0.0.0.0 255.255.255.0 U 102 0 0 enp3s0
$
正如您所看到的,路由已添加到路由表中,并且 ping 设备可以正常工作。然而几秒钟后目的地就变得无法到达。再次查看路由表,我发现该路由已被删除!
所以现在的问题是“谁”删除了该路线以及为什么?
答案1
如果您的网卡未连接(看起来如此),您必须忽略运营商状态。
看这另一个问题欲了解详情
答案2
我想我找到了静态路由被丢弃的答案。看来 NetworkManger (nmcli) 不喜欢我直接添加路由,因此它会在后台杀死它们。
我得到了一些灵感这里关于如何使用 nmcli 为我的设备添加静态路由。所以我使用这个命令来添加我需要的路由。
$ sudo nmcli con add type ethernet con-name "static-ip" ifname enp2s0 ipv4.method manual ipv4.addresses 192.168.1.19/32 gw4 0.0.0.0
我不太确定我完全理解它的所有功能,但它似乎可以工作,现在我可以一直在 192.168.1.19 上 ping 我的设备。更好的是,这种更改在重新启动后仍然存在,而且我似乎还能够从同一 192.168.1.0/24 网络上的笔记本电脑 ping 设备。