21

Does the routing order matter:

> route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
123.x.x.151     0.0.0.0         255.255.255.255 UH    0      0        0 vmbr0
123.x.x.154     0.0.0.0         255.255.255.255 UH    0      0        0 vmbr0
123.x.x.128     0.0.0.0         255.255.255.224 U     0      0        0 vmbr0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 vmbr1
0.0.0.0         123.x.x.129     0.0.0.0         UG    0      0        0 vmbr0

is it the same as:

> route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
123.x.x.128     0.0.0.0         255.255.255.224 U     0      0        0 vmbr0
123.x.x.151     0.0.0.0         255.255.255.255 UH    0      0        0 vmbr0
123.x.x.154     0.0.0.0         255.255.255.255 UH    0      0        0 vmbr0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 vmbr1
0.0.0.0         123.x.x.129     0.0.0.0         UG    0      0        0 vmbr0

?

where the difference is, that

123.x.x.128     123.x.x.129         255.255.255.224 U     0      0        0 vmbr0

is higher order than

123.x.x.151     0.0.0.0         255.255.255.255 UH    0      0        0 vmbr0

so if I send to 123.x.x.151 where will it go:

- the routed way over `123.x.x.129`, because it matches the `123.x.x.128` rule, or
- the direct way using the arp table, because it matches the `123.x.x.151` rule

?

26

The order in the table doesn't matter; routes with a longer prefix always take priority. If you stop clinging to netmasks and consider the prefix lengths instead (which ip route shows), you have 123.x.x.128/27 and 123.x.x.151/32, and the latter – more specific – route will take priority over the former (more generic one).

  • 2
    What happens if there are eqal sized masks? E.g. 10.0.0.0/24 and 192.168.0.0/24. – ManuelSchneid3r Feb 4 '16 at 21:57
  • 3
    @ManuelSchneid3r: Nothing happens. Routes are only considered if they actually match the destination, and an IP address obviously cannot start with 10. and 192. at the same time, so it will only match one of those masks in the first place. – grawity Feb 5 '16 at 5:37
  • 1
    what if you have two interfaces on the same IP space? – MikeSchem Jul 26 '18 at 20:25
  • @MikeSchem: Then the 'metric' parameter of both routes is used. (The OS will usually refuse to add two routes with identical prefix, identical prefixlen, and identical metric, or possibly merge them into an ECMP load-balanced route.) – grawity Jul 26 '18 at 20:34
5

Order only matters if you have duplicate routes. Don't do that.

For example, if two interfaces have the same destination, netmask, and gateway of 0.0.0.0, the first one will grab all the traffic for that network.

What's more, I've found some systems will randomly reorder on boot which one comes first. If only one works, or only one has a router gateway set, this can result in losing access to that network on reboot.

  • If you do have duplicate routes, then you should add a higher Administrative Distance (AD) for the less preferred route. The route used will always be the one with the lower AD. If that path goes down, then the one with the higher AD will automatically replace it in the routing table. – kojow7 Jan 11 at 23:31

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.