Change order of routing table/move entry up or down in linux
I have a couple of interfaces in my routing table and the first one stopped working. I want to temporarily move it down to make one below it default. Do I have to delete and add it again or is there an ip/route/ifconfig command, which can just move the entries up or down?
Solution 1:
A routing table isn't used in top-down order. Instead, more-specific routes (with longer prefix length) always take priority over less-specific ones – e.g. a /24 route wins over a /16 route, which wins over the 'default' /0 route.
If you have two routes for the exact same destination (e.g. both of them are for 192.168.1.0/24), they are compared according to their 'metric' parameter – which is basically the route's cost, so the lowest metric value (lowest cost) wins.
So if you have multiple 'default' routes, you can prioritize one by removing and re-adding it with a lower metric, for example:
$ ip route
default via 192.168.7.1 dev eth0 proto dhcp metric 100
default via 192.168.1.1 dev wlan0 proto dhcp metric 600
$ ip route del default dev wlan0
$ ip route add default via 192.168.1.1 dev wlan0 metric 50
(You don't actually need to delete the unwanted high-metric route, especially if the change is going to be very temporary – it's enough to add the new one.)
Note that you probably cannot use ip route change
or ip route replace
in this situation, because they treat the specified metric as a lookup key and the device as the parameter to update, while you probably want to do the exact opposite.