I am very confused by the semantics of ip rules
when it comes to the default route.
Sometimes, I can have multiple default routes using different gateways:
# ip route
default via 10.0.0.4 dev eth0 proto static metric 1024
10.0.0.0/16 dev eth0 proto kernel scope link src 10.0.0.129
45.79.69.51 via 10.0.0.4 dev eth0
169.254.0.0/16 dev rath scope link metric 1000
192.168.12.0/24 dev rath proto kernel scope link src 192.168.12.2
# ip route replace default via 192.168.12.1
# ip route
default via 192.168.12.1 dev rath
default via 10.0.0.4 dev eth0 proto static metric 1024
10.0.0.0/16 dev eth0 proto kernel scope link src 10.0.0.129
45.79.69.51 via 10.0.0.4 dev eth0
169.254.0.0/16 dev rath scope link metric 1000
192.168.12.0/24 dev rath proto kernel scope link src 192.168.12.2
At other times, I cannot:
# ip route del default via 10.0.0.4 dev eth0
# ip route
default via 192.168.12.1 dev rath
10.0.0.0/16 dev eth0 proto kernel scope link src 10.0.0.129
45.79.69.51 via 10.0.0.4 dev eth0
169.254.0.0/16 dev rath scope link metric 1000
192.168.12.0/24 dev rath proto kernel scope link src 192.168.12.2
# ip route add default via 10.0.0.4
RTNETLINK answers: File exists
# ip route
default via 192.168.12.1 dev rath
10.0.0.0/16 dev eth0 proto kernel scope link src 10.0.0.129
45.79.69.51 via 10.0.0.4 dev eth0
169.254.0.0/16 dev rath scope link metric 1000
192.168.12.0/24 dev rath proto kernel scope link src 192.168.12.2
But if I delete the default route, it is changed instead (or, more likely, the other default rule becomes visible):
# ip route del default via 192.168.12.1
# ip route
default via 10.0.0.4 dev eth0 proto static metric 1024
10.0.0.0/16 dev eth0 proto kernel scope link src 10.0.0.129
45.79.69.51 via 10.0.0.4 dev eth0
169.254.0.0/16 dev rath scope link metric 1000
192.168.12.0/24 dev rath proto kernel scope link src 192.168.12.2
What is happening here? Why isn't ip route replace default
changing the default route? Why isn't ip route show
always showing all default routes?
In your setup you have two different default routes (with and without metric).
You can have multiple route to same destination target with different metrics.
ip route replace
adds route, if this route didn't exist, and replaces it otherwise. In your case you haven't had the default route without metric, andip route replace default via 192.168.12.1
has added the default route without metric.One more time: the routes to same destination with metric and without metric are different routes!