A remote network can be reachable through more than one gateway. On Linux, iproute2 can add one IPv4 multipath route that makes both next hops available for the same destination prefix.
Linux hashes packet fields to select a next hop. The weights control how much of the hash space goes to each path, so repeated traffic between the same endpoints can keep using one gateway.
Both gateways must already be reachable on their respective links and able to forward traffic to the destination. The route takes effect in the running kernel immediately and needs to be recreated after a reboot.
Steps to add a multipath route with iproute2:
- Confirm that each gateway address falls within an on-link subnet for its intended interface in the main routing table.
$ ip -4 route show table main scope link 192.0.2.0/24 dev eth0 proto kernel src 192.0.2.10 198.51.100.0/24 dev eth1 proto kernel src 198.51.100.10
Here 192.0.2.2 falls within the subnet on eth0 and 198.51.100.2 within the subnet on eth1. A missing on-link route must be fixed before continuing.
- Check the main table for an exact match to your destination prefix in place of 203.0.113.0/24.
$ ip -4 route show table main exact 203.0.113.0/24
A successful query with no output means the table has no entry for that exact prefix. If it lists a route, stop and review the existing entry before changing how traffic reaches that network.
- Add the route as root with your destination prefix, gateway addresses, interface names, and weights in place of the example values.
The new route can redirect traffic immediately, including your remote session if it uses the destination prefix. If it does, make the change from a local or out-of-band console.
ip -4 route add 203.0.113.0/24 table main proto static \ nexthop via 192.0.2.2 dev eth0 weight 1 \ nexthop via 198.51.100.2 dev eth1 weight 2
via names the gateway and dev names its interface. Weight 2 gets twice the hash share of weight 1. Equal weights split the hash space evenly.
If traffic breaks after a successful add, delete the new route from the console using the same prefix, gateways, interfaces, and weights.
ip -4 route del 203.0.113.0/24 table main proto static \ nexthop via 192.0.2.2 dev eth0 weight 1 \ nexthop via 198.51.100.2 dev eth1 weight 2
- Verify both next hops and their weights in the main table for your destination prefix in place of 203.0.113.0/24.
$ ip -4 route show table main exact 203.0.113.0/24 203.0.113.0/24 proto static nexthop via 192.0.2.2 dev eth0 weight 1 nexthop via 198.51.100.2 dev eth1 weight 2
The listing should show one route with a nexthop line per gateway. The example shows weight 1 on eth0 and weight 2 on eth1.