1

I have an lxd container configured with two network interfaces (using systemd-networkd).

The first one has a static IP address:

testuser@lxd-7e5cbece-180b-427f-a8b1-b2c12f6e9d79:~$ cat /etc/systemd/network/20-net0.network 
[Match]
Name=net0

[Network]
Address=162.132.242.249/26
Gateway=162.132.192.252
DNS=162.132.192.252
DNS=162.132.170.6

And the default interface from lxd (which uses DHCP):

testuser@lxd-7e5cbece-180b-427f-a8b1-b2c12f6e9d79:~$ cat /etc/systemd/network/eth0.network 
[Match]
Name=eth0

[Network]
DHCP=ipv4

[DHCP]
SendHostname=True

Now the problem is that the routing table is generated with 162.132.192.252 (from net0) as the default gateway, but I would like the gateway from eth0 to be the default gateway.

testuser@lxd-7e5cbece-180b-427f-a8b1-b2c12f6e9d79:~$ sudo route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         162.132.242.252 0.0.0.0         UG    0      0        0 net0
0.0.0.0         10.245.111.1    0.0.0.0         UG    1024   0        0 eth0
10.245.111.0    0.0.0.0         255.255.255.0   U     0      0        0 eth0
10.245.111.1    0.0.0.0         255.255.255.255 UH    1024   0        0 eth0
162.132.242.192 0.0.0.0         255.255.255.192 U     0      0        0 net0

1) Why is the routing table generated in that order? (first net0 and then eth0)

2) Is it possible to change the order of the default gateways?

2

For anyone having the same issue, you can actually do this using metrics. Instead of defining the default Gateway in the [Network] section you can add a default route with a lower metric to the static interface:

[Match]
Name=net0

[Network]
Address=162.132.242.249/26
DNS=162.132.192.252
DNS=162.132.170.6

[Route]
Gateway=162.132.192.252
Destination=0.0.0.0/0
Metric=1024

After this you can do the same for a DHCP interface (note that you don't have to define the Gateway because this will be provided by your DHCP server):

[Match]
Name=eth0

[Network]
DHCP=ipv4

[DHCP]
SendHostname=True
UseDomains=True

[Route]
Destination=0.0.0.0/0
Metric=1000

Now you get the desired routing table with the eth0 interface as default gateway:

> ip route list
default dev eth0 proto static metric 1000 
default dev net0 proto static metric 1024 
default via 10.245.111.1 dev eth0 proto dhcp src 10.245.111.96 metric 1024 
10.245.111.0/24 dev eth0 proto kernel scope link src 10.245.111.96 
10.245.111.1 dev eth0 proto dhcp scope link src 10.245.111.96 metric 1024 
162.132.242.192/26 dev net0 proto kernel scope link src 162.132.242.249 
| improve this answer | |
  • Is worth to mention that if you're using DHCP in multiple interfaces you might want to consider using RouteMetric=xyz under the [DHCP] section instead of the [Route] section. – TCB13 Jan 11 at 9:50
0

this isn't really a programming question. You'd be better off asking things like this over on StackExchange Unix & Linux.

I had a similar scenario: multiple interfaces using DHCP causing multiple default routes.

What I did, it may work for you too, is to tell the interfaces where we don't want the DHCP routes that we don't want them. In the .network file for the interface, add the DHCP UseRoutes=false option like this example:

[Match]
Name=eno3

[Network]
DHCP=ipv4

[DHCP]
UseRoutes=false
| improve this answer | |
  • Your first paragraph is exactly why you shouldn't be answering this question at all and, instead, voting to close for that reason. Not all questions should be answered. stackoverflow.com/help/how-to-answer – Rob Nov 28 '19 at 0:10
  • This for sure helps if you do not want the DHCP interface to be a default route. In the case of the static interface you could just remoe the Gateway= and it wouldn't create the route. – randombenj Nov 28 '19 at 7:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.