Linux – How to override systemd’s choice of default route with two network interfaces

arch linuxroutingsystemdvirtualbox

Update: /etc/network/interfaces does not exist because I am using netctl profiles.

I set up a VirtualBox instance running ArchLinux with two network adapters. Adapter 1 (eth0) is attached to a NAT (10.0.2.0/24) and Adapter 2 (eth1) is on the host only network (192.168.56.0/24). I use udev rules to use old-fashioned names eth0 and eth1 for these adapters (but the issue existed with the enp*** names as well).

When I log on, I get this:

$ ip route
default via 192.168.56.1 dev eth1 
default via 10.0.2.2 dev eth0 proto dhcp src 10.0.2.15 metric 202 
10.0.2.0/24 dev eth0 proto dhcp scope link src 10.0.2.15 metric 202 
192.168.56.0/24 dev eth1 proto kernel scope link src 192.168.56.31

which results in

$ ping google.com
ping: google.com: Name or service not known

As I understand it, this is because 192.168.56.1 (the host only interface) is added with a lower metric than the NAT interface.

If I then manually do:

$ sudo ip route delete default
$ ip route
default via 10.0.2.2 dev eth0 proto dhcp src 10.0.2.15 metric 202 
10.0.2.0/24 dev eth0 proto dhcp scope link src 10.0.2.15 metric 202 
192.168.56.0/24 dev eth1 proto kernel scope link src 192.168.56.31

I get

$ ping google.com
PING google.com (172.217.12.174) 56(84) bytes of data.
64 bytes from ....1e100.net (172.217.12.174): icmp_seq=1 ttl=52 time=11.9 ms
64 bytes from ....1e100.net (172.217.12.174): icmp_seq=2 ttl=52 time=11.7 ms
^C
--- google.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 3ms
rtt min/avg/max/mdev = 11.698/11.782/11.867/0.137 ms

So, how can I ensure that outbound traffic is routed through eth0/10.0.2.0/24 (NAT interface to host) rather than eth1/192.168.56.0/24 (host only network).

The question is only tangentially related to VirtualBox (if there is a VirtualBox setting that allows me to change the metrics, I wouldn't mind it).

I believe there must be a way to do one of 1) Stop systemd from adding the default via 192.168.56.1 dev eth1 route; or 2) Add the default via 10.0.2.2 dev eth0 route with a lower metric, so I don't have to delete the default route manually every time I start the instance.

There might be another work-around I haven't considered yet. So far fiddling with names, static IP vs DHCP did not help.

The guest OS is ArchLinux.

Best Answer

Add in the /etc/network/interfaces file to corresponding interface the line:

post-up route del default via 192.168.56.1 dev eth1

which will delete the route default via 192.168.56.1 dev eth1 on interface startup.

Related Question