1

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?

1 Answer 1

4

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, and ip 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!

+------------------+------------------------+------------------+
| Command          | route to               |route to same dst |
|                  | same dst exists        | doesn't exists   |
+------------------+------------------------+------------------+
| ip route add     |  error                 |  ok              |
| ip route change  |  ok                    |  error           |
| ip route replace |  ok                    |  ok              |
| ip route del     |  ok                    |  error           |
+------------------+------------------------+------------------+
3
  • Is this documented somewhere? It seems to raise a lot of additional questions (starting with which route is used for what packets).
    – Nikratio
    Jan 2, 2016 at 18:43
  • Not accepting that answer yet, because I think it doesn't answer the last part of the question (why does the route with metric only appear after the one without was deleted?).
    – Nikratio
    Jan 2, 2016 at 18:45
  • @Nikratio the only logical explanation is that the route was being added by a network manager on your system
    – Matthijs
    Jun 13, 2022 at 13:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .