3

I've seen multiple questions here and articles elsewhere about this. I get that a subnet can only have one gateway and I get why - devices wouldn't know whether to use the first or second to get to addresses outside the LAN subnet.

But is it possible to have multiple gateways if each goes to a different subnet?

For example: If I use 192.168.1.xxx for my LAN, but have a 2nd LAN in another building, but connected by cable or fiber, and it uses the 172.16.7.xxx range, can I have one gateway to connect to that LAN and another gateway to reach the rest of the internet?

2 Answers 2

4

Yes, that's how routers work in the first place. They (and really not just routers, but most systems that use IP, including even Windows PCs) use a route table to decide where packets should go.

If you run ip route on Linux or netstat -rn on macOS or Get-NetRoute on Windows in PowerShell (or route print in Cmd) you'll see a table of destination networks with their corresponding gateways.

For example, if you want your PC to use two gateways, 192.168.1.1 providing the Internet access and 192.168.1.172 providing access to the other building, you could use:

  • Windows: C:\> route add 172.16.7.0/24 192.168.1.172

  • Linux: # ip route add 172.16.7.0/24 via 192.168.1.172

(Normally, though, it would be the job of your LAN's router to handle such interconnections. Ideally the same router should just act as a gateway between all three networks – it's pretty much the one job of a router. But if there must be two separate gateways, then the 172.16.7.x route should still be configured on the LAN's "default gateway", not on individual devices one-by-one. For example, you won't be able to add new routes to your printer or your smart TV – they get a "default gateway" from DHCP and that's it.)

Even the "default gateway" configuration in your OS is just a shortcut for configuring a route for the 0.0.0.0/0 or ::/0 destination, which matches all possible addresses. (And when you configure a "subnet mask" for your local subnet, that's also just a shortcut for a different route type.)

Routing table entries are ordered by "longest match first" – not top-to-bottom. For example, the same address will match both your 172.16.7.0/24 route and the "default gateway" 0.0.0.0/0 route. The former will have priority because it matches a more specific prefix (24 bits vs 0 bits).


Some systems actually allow multiple gateways to be specified in a single route – it's more commonly done in dedicated routers than PC operating systems (you won't find this feature on Windows or macOS), but Linux in particular lets you create a multi-gateway route easily, and will balance outgoing flows equally between all specified nexthops. This is known as "equal-cost multipath" (ECMP).

ip route add 172.16.0.0/16 nexthop via 192.168.1.7 nexthop via 192.168.1.8

What is more common is the ability to create several separate routes that happen to match exactly the same destination (including same prefix length). In this case, a second parameter – "metric" (aka cost) – is used to decide priority between otherwise equal routes. For example:

ip route add 172.16.0.0/16 via 192.168.1.7 metric 100
ip route add 172.16.0.0/16 via 192.168.1.8 metric 200

Here the route with "metric 100" will be used for everything, and the other one will be ignored. (However, in some cases – such as IPv6, and possibly on Windows with IPv4? – if the first gateway ends up inaccessible, the OS may disable that route and fall back to the 2nd gateway.)

4

Yes.

Your second example is not a good good example because a gateway (practically speaking) need to be in the same network the interface is on.

A better example might be if you have 2 simple routers to different ISPs. The LAN interface on router 1 is 192.168.1.254/24 and router 2 is 192.168.1.253/24.

You can split routes up so that some traffic is routed via one router (one ISP), and some via the other. For example you could set up 0.0.0.0/1 via 192.168.0.253 and 128.0.0.0/1 via 192.168.0.254 to split the traffic to both router's interfaces.

While not common on SOHO routers, large routers with more than 2 interfaces commonly do this.

Note : When you get more complex routing you can have multiple gateways for the same subnet, there are a few ways you can do this :

  • The easier is a "metric" which gives preference to 1 gateway over the ither (if the gateway interface disappears - eg a PPP interface) it will use the higher metric route.
  • It's also possible to do it via multiple routing tables and use policy routing to send different packets to different route tables).

You must log in to answer this question.

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