18.04.1 LTS systemd-network 未应用

18.04.1 LTS systemd-network 未应用

我有一个具有两个接口的服务器实例,并且我创建了一条规则来确保进入 ens160 的流量尽管与 ens192 的默认路由匹配,但仍会从 ens160 流出。

使用 手动创建时,路由和规则按预期工作ip,当通过 netplan 应用时,路由和规则按预期工作netplan apply,但当使用 netplan 生成的配置文件重新启动 systemd-networkd 时则不然。具体来说,规则未应用。

netplan.yaml:

network:
    ethernets:
        ens160:
            addresses:
            - 192.168.1.238/26
            - 192.168.1.240/26
            - 192.168.1.241/26
            dhcp4: false
            gateway4: 192.168.1.193
            routes:
            - to: 0.0.0.0/0
              via: 192.168.1.193
              table: 200
            routing-policy:
            - from: 192.168.1.192/26
              table: 200
        ens192:
            addresses:
            - 10.125.210.38/23
            dhcp4: false
    version: 2

/运行/systemd/网络/10-netplan-ens160.网络:

[Match]
Name=ens160

[Network]
Address=192.168.1.238/26
Address=192.168.1.240/26
Address=192.168.1.241/26
Gateway=192.168.1.193

[Route]
Destination=0.0.0.0/0
Gateway=192.168.1.193
Table=200

[RoutingPolicyRule]
From=192.168.1.192/26
Table=200

IP 路由列表:

default via 192.168.1.193 dev ens160 proto static
10.125.210.0/23 dev ens192 proto kernel scope link src 10.125.210.38
192.168.1.192/26 dev ens160 proto kernel scope link src 192.168.1.238

ip 路由列表表 200:

default via 192.168.1.193 dev ens160 proto static

之后netplan applyip rule list(此配置有效):

0:  from all lookup local
0:  from 192.168.1.192/26 lookup 200
32766:  from all lookup main
32767:  from all lookup default

然而,之后systemctl restart systemd-networkd

0:  from all lookup local
32766:  from all lookup main
32767:  from all lookup default

重启 systemd-networkd 后的 systemd 日志:

Nov 20 17:30:28 lb1 systemd[1]: Stopping Network Service...
Nov 20 17:30:28 lb1 systemd[1]: Starting Network Service...
Nov 20 17:30:28 lb1 systemd-networkd[1810]: ens192: Gained IPv6LL
Nov 20 17:30:28 lb1 systemd-networkd[1810]: ens160: Gained IPv6LL
Nov 20 17:30:28 lb1 systemd-networkd[1810]: Enumeration completed
Nov 20 17:30:28 lb1 systemd[1]: Started Network Service.
Nov 20 17:30:28 lb1 systemd-networkd[1810]: ens160: Link is not managed by us
Nov 20 17:30:28 lb1 systemd-networkd[1810]: lo: Link is not managed by us
Nov 20 17:30:28 lb1 systemd-networkd[1810]: lo: Link is not managed by us
Nov 20 17:30:28 lb1 systemd-networkd[1810]: ens192: Configured
Nov 20 17:30:28 lb1 systemd-networkd[1810]: ens160: Configured

更新 1:发出两个命令systemctl restart systemd-networkd将在第二次调用后应用规则。再发出一次,规则就会消失。日志显示每次都会应用规则,但不是明确应用的:

Nov 20 18:57:52 lb1 systemd-networkd[1461]: ens160: Setting routing policy rules
Nov 20 18:57:52 lb1 systemd-networkd[1461]: ens160: Setting routes
Nov 20 18:57:52 lb1 systemd-networkd[1461]: ens160: Routing policy rule configured
Nov 20 18:57:52 lb1 systemd-networkd[1461]: ens160: Routes set
Nov 20 18:57:52 lb1 systemd-networkd[1461]: ens160: Configured

更新 2:连续netplan apply切换规则。

相关内容