A single default route is fine until you need to send traffic from one source IP out one uplink and traffic from another source IP out a second uplink, or route based on a firewall mark, or keep a tenant’s egress on a dedicated WAN. Linux has supported this for two decades through ip rule and multiple routing tables, but the syntax surprises people the first time. This article shows the model, the canonical commands, and the patterns we use for dual ISPs and per-tenant egress in production.
How to verify
Inspect the rule set and per-table routes:
ip rule list
ip route show table main
ip route show table local
ip route show table 100
cat /etc/iproute2/rt_tables
ip route get 1.1.1.1 from 192.0.2.10
ip route get 1.1.1.1 mark 0x10
ip route get with a from or mark argument is the fastest way to check what the kernel actually does for a given packet — far better than guessing from the rule list.
What’s happening
Linux maintains multiple routing tables, identified by an integer in the range 1–252. The kernel keeps three by default: local (255), main (254), default (253). main is what route -n and the bare ip route command show. Tables 0 and 1 are reserved. The rest are yours.
For every packet, the kernel walks the rule list in priority order (lowest number first) and stops at the first rule whose match clause is satisfied. The matched rule names a table; the kernel then looks up the route in that table. If the table has no route for the destination, the kernel does not fall through to the next rule by default — it returns “Network is unreachable” unless the rule was created with suppress_prefixlength or you explicitly chain.
Common match clauses:
from <source>— source address or prefixto <dest>— destination address or prefixfwmark <mark>— iptablesMARKtarget valueiif <interface>— input interface (useful for routed-into traffic)oif <interface>— output interface
The default rule set is 0: from all lookup local, 32766: from all lookup main, 32767: from all lookup default. Add your rules between 0 and 32766 so they take precedence over main but cannot intercept the kernel’s own local-delivery decisions.
The procedure
-
Name your custom tables in
/etc/iproute2/rt_tablesso commands stay readable:100 wan-a 101 wan-b 200 tenant-blue -
Dual-ISP egress by source IP. Two uplinks:
eth0on192.0.2.0/24with gateway192.0.2.1,eth1on198.51.100.0/24with gateway198.51.100.1. Hosts behind each WAN should send replies out the matching WAN.# Per-WAN default route, plus the directly-connected route ip route add default via 192.0.2.1 dev eth0 table wan-a ip route add 192.0.2.0/24 dev eth0 table wan-a ip route add default via 198.51.100.1 dev eth1 table wan-b ip route add 198.51.100.0/24 dev eth1 table wan-b # Rules: pick the table by source ip rule add from 192.0.2.0/24 table wan-a priority 100 ip rule add from 198.51.100.0/24 table wan-b priority 101 -
fwmark-based routing for a tenant VRF. Mark packets in iptables/nftables, then route the marked traffic through a dedicated table:
iptables -t mangle -A PREROUTING -i veth-tenant-blue -j MARK --set-mark 0x10 ip rule add fwmark 0x10 table tenant-blue priority 200 ip route add default via 10.50.0.1 dev wg-tenant-blue table tenant-blue -
Persist with systemd-networkd rather than ad-hoc commands.
[RoutingPolicyRule]andTable=sections in a.networkfile are reapplied on every link event:[Match] Name=eth0 [Network] Address=192.0.2.10/24 [Route] Gateway=192.0.2.1 Table=100 [RoutingPolicyRule] From=192.0.2.10/32 Table=100 Priority=100 -
Verify symmetry. For each source you care about, run
ip route get <internet-ip> from <source>and confirm theoifmatches the intended uplink. Run it again afternetplan applyorsystemctl restart systemd-networkdto catch ordering bugs. -
Watch the reverse-path filter.
net.ipv4.conf.all.rp_filter=1(the strict mode default on many distros) drops packets whose source would not be routed back out the same interface. With policy routing, that fight is usually lost — setrp_filter=2(loose) per-interface, or accept asymmetric routes explicitly.
Operational notes
ip rule addis idempotent only by exact match; running the same command twice creates two identical rules. Useip rule flush priority 100before re-adding when scripting.- The
maintable still wins by default because its rule has priority 32766 — if you want your custom table consulted first for a class of traffic, your rule’s priority must be lower (numerically smaller). - Container runtimes inject their own rules. Docker, Kubernetes CNIs, and Tailscale all add
ip ruleentries; review the full table before assuming yours are the only ones in play. netfilterMARK andconnmarkare different. Mark on the first packet of a flow with iptables, thenCONNMARK --save-markso reply packets carry the same mark via conntrack — otherwise return traffic falls back tomain.- Cloud providers may reset routes on metadata refresh. AWS DHCP, Azure agent, and GCP guest agent rewrite
mainopportunistically; put policy routes in a custom table they leave alone.
In the engagements we run, policy routing is the cleanest fix when a single host has to serve traffic across two uplinks with different SLAs, or when a tenant’s egress must traverse a specific WireGuard tunnel for compliance. The pattern integrates with the BGP routing setup we use on edge nodes, and the same rules live in the Ansible role the managed-operations practice ships with every client lab.