我使用的是 Ubuntu 18.04.4 LTS,它在 VMWare Fusion VM 中运行,需要在启动时添加静态路由:
到网络
192.168.11.0/24
,可通过 IP 访问192.168.10.4
,该 IP 与分配给本地接口 ( ) 的 IP 之一位于同一网络上eth0
。到网络
192.168.2.0/24
,可通过 IP 访问,该 IP 与分配给本地接口 ( )192.168.122.101
的 IP 之一位于同一网络上virbr0
10-static.network
为了实现它,我已经配置了网络文件,如下所示:
root@gns3local:~# cat /etc/systemd/network/10-static.network
[Route]
Gateway=192.168.10.5
Destination=192.168.11.0/24
root@gns3local:~# netstat -nr
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 192.168.10.1 0.0.0.0 UG 0 0 0 eth0
192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.10.1 0.0.0.0 255.255.255.255 UH 0 0 0 eth0
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
root@gns3local:~# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.5 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::250:56ff:fe3e:70a6 prefixlen 64 scopeid 0x20<link>
ether 00:50:56:3e:70:a6 txqueuelen 1000 (Ethernet)
RX packets 234 bytes 22886 (22.8 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 156 bytes 22033 (22.0 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:01:a3:e5 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
root@gns3local:~# systemctl status systemd-networkd
● systemd-networkd.service - Network Service
Loaded: loaded (/lib/systemd/system/systemd-networkd.service; enabled-runtime; vendor preset: enabled)
Active: active (running) since Mon 2020-07-27 16:52:47 UTC; 12min ago
Docs: man:systemd-networkd.service(8)
Main PID: 515 (systemd-network)
Status: "Processing requests..."
Tasks: 1 (limit: 5861)
CGroup: /system.slice/systemd-networkd.service
└─515 /lib/systemd/systemd-networkd
Jul 27 16:52:47 gns3local systemd-networkd[515]: virbr0-nic: Lost carrier
Jul 27 16:52:47 gns3local systemd-networkd[515]: virbr0-nic: Removing non-existent address: fe80::5054:ff:fe01:a3e5/64
Jul 27 16:52:48 gns3local systemd-networkd[515]: eth0: Gained IPv6LL
Jul 27 16:52:48 gns3local systemd-networkd[515]: eth0: Configured
现在,系统重新启动后,路由没有被添加,并且没有与之相关的错误:
root@gns3local:~# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.5 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::250:56ff:fe3e:70a6 prefixlen 64 scopeid 0x20<link>
ether 00:50:56:3e:70:a6 txqueuelen 1000 (Ethernet)
RX packets 83 bytes 12321 (12.3 KB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 77 bytes 11639 (11.6 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:01:a3:e5 txqueuelen 1000 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
root@gns3local:~# netstat -nr
Kernel IP routing table
Destination Gateway Genmask Flags MSS Window irtt Iface
0.0.0.0 192.168.10.1 0.0.0.0 UG 0 0 0 eth0
192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.10.1 0.0.0.0 255.255.255.255 UH 0 0 0 eth0
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
root@gns3local:~# systemctl status systemd-networkd
● systemd-networkd.service - Network Service
Loaded: loaded (/lib/systemd/system/systemd-networkd.service; enabled-runtime; vendor preset: enabled)
Active: active (running) since Mon 2020-07-27 17:39:02 UTC; 2min 22s ago
Docs: man:systemd-networkd.service(8)
Main PID: 539 (systemd-network)
Status: "Processing requests..."
Tasks: 1 (limit: 5860)
CGroup: /system.slice/systemd-networkd.service
└─539 /lib/systemd/systemd-networkd
Jul 27 17:39:02 gns3local systemd-networkd[539]: virbr0-nic: Could not set route: Network is unreachable
Jul 27 17:39:02 gns3local systemd-networkd[539]: virbr0: Link UP
Jul 27 17:39:02 gns3local systemd-networkd[539]: virbr0-nic: Link DOWN
Jul 27 17:39:02 gns3local systemd-networkd[539]: virbr0-nic: Lost carrier
Jul 27 17:39:02 gns3local systemd-networkd[539]: virbr0-nic: Removing non-existent address: fe80::5054:ff:fe01:a3e5/64
Jul 27 17:39:03 gns3local systemd-networkd[539]: eth0: DHCPv4 address 192.168.10.5/24 via 192.168.10.1
Jul 27 17:39:03 gns3local systemd-networkd[539]: eth0: Gained IPv6LL
Jul 27 17:39:03 gns3local systemd-networkd[539]: eth0: Configured
问题:
路由在启动时不保留的原因可能是什么?
在启动时添加静态路由的最佳实践是什么?
如何使用添加相同的路线
crontab
?