1

I have a wired connection and a wireless connection configured in this way

[Match]
Name=eth01

[Network]
Address=10.0.0.120/24
Gateway=10.0.0.1

[Route]
Destination=0.0.0.0/0
Metric=1000

The wireless configuration is like this:

[Match]
Name=wlan0

[Network]
Address=10.0.0.129/24
Gateway=10.0.0.1

[Route]
Destination=0.0.0.0/0
Metric=2000

Again I have tried setting the Destination to the Gateway.

Whatever I try I get a routing like this :

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 

But I want the default route to go through the wired device unless the wired device is not present.

2 Answers 2

2

It should be something like this:

[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
11
  • 1
    Would you please add some explanation around your solution so that I can understand why it is correct. I don't doubt that it is, and I thank you for it, but II like to know what I'm doing. Commented Aug 17, 2021 at 8:07
  • Well, Gateway= is essentially a shortcut of a [Route] section with Destination=0.0.0.0/0, which is the implied default when the option is unspecified. Therefore if you want to specify some more "details" for the default route (like Metric=), you can / should use a [Route] section instead of the shortcut. PreferredSource= is probably optional anyway if each interface has only one address configured.
    – Tom Yan
    Commented Aug 17, 2021 at 8:22
  • Route that has a gateway/nexthop is called indirect route. For it to work you need to have a direct route that covers the gateway address. While the kernel-added prefix routes should technically work, the routes are not managed by networkd. IIIRC it will hiccup when trying to configure an indirect route when it does not manage a direct route that serves the former, that's why the additional [Route] section for Destination=10.0.0.1. Scope=link make sure that the section is processed / known before that of the default route.
    – Tom Yan
    Commented Aug 17, 2021 at 8:28
  • The suggestion in the last paragraph is to avoid the prefix/subnet route to be added/managed by the kernel but by systemd instead (so that they can have metrics set as well). 10.0.0.0/24 covers 10.0.0.1, so you can just replace the destination instead of adding yet another [Route] section.
    – Tom Yan
    Commented Aug 17, 2021 at 8:32
  • Hm I find the /32 breaks the network init. What's the idea there? Commented Aug 17, 2021 at 15:40
0

Here's what works for me. Clean and simple.

/etc/systemd/network/eth.network

[Match]
Name=eth01

[Network]
Address=10.0.0.120/24

[Route]
Gateway=10.0.0.1
Metric=1000

And

/etc/systemd/network/wlan.network

[Match]
Name=wlan0

[Network]
Address=10.0.0.129/24

[Route]
Gateway=10.0.0.1
Metric=2000

Gives

$ 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 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .