我最近从 16.04 升级到了 ubuntu 18.04,并且正在尝试弄清楚如何让我以前在 netplan 下运行的ip route
命令。ip rule
/etc/network/interfaces
这些是我之前尝试重现的命令/etc/network/interfaces
:
sudo ip rule add table 129 from 192.168.1.160
sudo ip route add table 129 to 204.8.230.0/24 dev enp0s3
sudo ip route add table 129 to 192.168.1.0/24 dev enp0s3
sudo ip route add table 129 default via 192.168.1.1
这是我第一次进行 netplan 配置/etc/netplan/01-netcfg.yaml
:
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
version: 2
renderer: networkd
ethernets:
enp0s3:
dhcp4: yes
routes:
- from: 192.168.1.160
to: 204.8.230.0/24
via: 192.168.1.1
- from: 192.168.1.160
to: 192.168.1.0/24
via: 192.168.1.1
但是,重启后这些路由不会显示在ip route
输出中。如何保留这些路由?
请注意,我也尝试根据我找到的一些文档将这些命令放入脚本中/usr/lib/networkd-dispatcher/routable.d
,但这似乎也不起作用。
编辑:我越来越接近了。这是新的配置,但现在的问题是,虽然表格出现在 中ip rule
,但ip route show table 129
却是空的:
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
version: 2
renderer: networkd
ethernets:
enp0s3:
dhcp4: yes
routing-policy:
- from: 192.168.1.160
table: 129
routes:
- to: 204.8.230.0/24
via: 192.168.1.1
table: 129
- to: 192.168.1.0/24
via: 192.168.1.1
table: 129
- to: 0.0.0.0/0
via: 192.168.1.1
table: 129
我使用的是 netplan 版本 0.36.1
答案1
我找到了答案。问题在于 systemd-networkd 试图在网络启动之前设置路由,但失败了。修复方法on-link: True
在路由上:
# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
version: 2
renderer: networkd
ethernets:
enp0s3:
dhcp4: yes
routing-policy:
- from: 192.168.1.160
table: 129
routes:
- to: 204.8.230.0/24
via: 192.168.1.1
table: 129
on-link: True
- to: 192.168.1.0/24
via: 192.168.1.1
table: 129
on-link: True
- to: 0.0.0.0/0
via: 192.168.1.1
table: 129
on-link: True