具有两个接口的 systemd-networkd 路由配置

具有两个接口的 systemd-networkd 路由配置

我有一个有线连接和一个无线连接以这种方式配置

[Match]
Name=eth01

[Network]
Address=10.0.0.120/24
Gateway=10.0.0.1

[Route]
Destination=0.0.0.0/0
Metric=1000

无线配置是这样的:

[Match]
Name=wlan0

[Network]
Address=10.0.0.129/24
Gateway=10.0.0.1

[Route]
Destination=0.0.0.0/0
Metric=2000

我再次尝试将目标设置为网关。

无论我尝试什么,我都会得到这样的路由:

default via 10.0.0.1 dev wlan0 proto static 
default via 10.0.0.1 dev eth01 proto static 
10.0.0.0/24 dev wlan0 proto kernel scope link src 10.0.0.129 
10.0.0.0/24 dev eth01 proto kernel scope link src 10.0.0.120 
10.0.0.1 dev wlan0 proto static metric 2000 

但我希望默认路由通过有线设备,除非有线设备不存在。

答案1

它应该是这样的:

[Match]
Name=eth01 # or wlan0

[Network]
Address=10.0.0.120/24 # or 10.0.0.129/24
RouteMetric=1000 # or 2000

# This section is probably optional
[Route]
Destination=10.0.0.1
Scope=link
PreferredSource=10.0.0.120 # or 10.0.0.129
Metric=1000 # or 2000

[Route]
# Destination=0.0.0.0/0 is implied when the option is not explicitly set
Gateway=10.0.0.1
PreferredSource=10.0.0.120 # or 10.0.0.129
Metric=1000 # or 2000

答案2

这对我有用。干净简单。

/etc/systemd/network/eth.network

[Match]
Name=eth01

[Network]
Address=10.0.0.120/24

[Route]
Gateway=10.0.0.1
Metric=1000

/etc/systemd/network/wlan.network

[Match]
Name=wlan0

[Network]
Address=10.0.0.129/24

[Route]
Gateway=10.0.0.1
Metric=2000

给予

$ ip r
default via 10.0.0.1 dev eth01 proto static metric 1000 
default via 10.0.0.1 dev wlan0 proto static metric 2000 
10.0.0.0/24 dev wlan0 proto kernel scope link src 10.0.0.129 
10.0.0.0/24 dev eth01 proto kernel scope link src 10.0.0.120 

相关内容