3

I have a pretty simple standard routing table on my linux box:

default via 192.168.1.1 dev eth0  proto static
192.168.1.0/24 dev eth0  proto kernel  scope link  src 192.168.1.162  metric 1

Now I need to route a specific subnet to another gateway. No problem yet, just add

10.0.0.0/8 via 192.168.1.2 dev eth0

However, there's a subset of that which should NOT go there. This works:

10.0.2.0/24 via 192.168.1.1 dev eth0

But I'd rather not have to repeat the default gateway in there; I'd like to be able to just say, "for 10.0.2.0/24, do what you'd do if there was no 10.0.0.0/8 route defined"

Any way of doing this? (Other than replacing the 10.0.0.0/8 with a bunch of more specific routes to carefully avoid the 10.0.2/24 one)

2
  • and what is default gw on the host?
    – ALex_hha
    Jul 24, 2013 at 7:40
  • As you can see in the first line of the routing table, 192.168.1.1
    – Jochen
    Jul 24, 2013 at 17:59

2 Answers 2

4

Routing is matched as a most specific wins.

This:

10.0.2.0/24 via 192.168.1.1 dev eth0

Is more specific than this:

10.0.0.0/8 via 192.168.1.2 dev eth0

/24 vs /8

So just add both routes, the traffic for 10.0.2.0/24 will match that route, anything else will match the less-specific /8 route.

Remember that your default route is 0.0.0.0/0 which is the least-specific CIDR that exists, which is why all other routes match before your default route. (192.168.1.0/24 is part of 0.0.0.0/0 remember)

2
  • I know that adding both works, but it means I must repeat the default gateway in the /24 route; I'd looking for a way to avoid that duplication.
    – Jochen
    Jul 24, 2013 at 17:59
  • Oh I understand now; sorry, I don't know a way around that. You could do multiple routing tables and something with the 'throw' route but that's making it wayyyy more complex than just adding the same gateway twice.
    – fukawi2
    Jul 25, 2013 at 23:33
0

Why not add the route to 10.0.0.0/8 on 192.168.1.1? That way new hosts on your network don't need to know about that static route and you can leave 192.168.1.1 as the default route and have it responsible for finding one-off networks like 10.0.2.0/24 and more generally 10.0.0.0/8.

1
  • Nice idea. Unfortunately that won't work in this case since 192.168.1.1 doesn't know how to route to the alternate (I oversimplified the example; the 10/8 actually goes to a different network, not just a different host on 192.168.1/24. And it would still mean having to specify the default gatway twice on the machine you put those routes on
    – Jochen
    Jul 24, 2013 at 18:07

You must log in to answer this question.

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