Skip to main content
added 185 characters in body
Source Link
grawity_u1686
  • 465.3k
  • 66
  • 977
  • 1.1k

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.)

Here the route with "metric 100" will be used for everything, and the other one will be ignored.

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.)

Source Link
grawity_u1686
  • 465.3k
  • 66
  • 977
  • 1.1k

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.