0

My PC has two NICs,ethernet and wifi. I wnat to set higher metric to wifi NIC. I use systemd-networkd.


$ cat /etc/systemd/network/10-wired.network
[Match]
Name=enp0s25

[Network]
Address=192.168.3.102/24
Gateway=192.168.3.1
DNS=192.168.3.1

$ cat /etc/systemd/network/20-wireless.network
[Match]
Name=wlp2s0

[Network]
Address=192.168.3.103/24
DNS=192.168.3.1

[Route]
Gateway=0.0.0.0
Destination=192.168.3.0/24
Metric=200

[Route]
Gateway=192.168.3.1
Destination=0.0.0.0/0
Metric=200

After reboot,

$ route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         local.gateway   0.0.0.0         UG    0      0        0 enp0s25
default         local.gateway   0.0.0.0         UG    200    0        0 wlp2s0
192.168.3.0     0.0.0.0         255.255.255.0   U     0      0        0 enp0s25
192.168.3.0     0.0.0.0         255.255.255.0   U     0      0        0 wlp2s0
192.168.3.0     0.0.0.0         255.255.255.0   U     200    0        0 wlp2s0

The route with metric 0 is created.

192.168.3.0     0.0.0.0         255.255.255.0   U     0      0        0 wlp2s0

I can delete the route manualy.

$sudo ip route del 192.168.3.0/24 via 0.0.0.0 metric 0 dev wlp2s0

But how to prevent the route with metric 0 to be created?

As walkaround,I used networkd-dispatcher hook to delete the route automatically.

$cat /etc/networkd-dispatcher/routable.d/99-wifi.sh

#!/bin/sh
#delete route with metric 0,when wifi is up

if [ "$IFACE" = 'wlp2s0' ]; then
    logger -t networkd-dispatcher "delete route with metric 0 for wifi."
    ip route del 192.168.3.0/24 via 0.0.0.0 metric 0 dev wlp2s0
fi

Your Answer

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

Browse other questions tagged or ask your own question.