Skip to content

Policy-based routing on Linux: multiple routing tables and ip rule

How Linux routes packets through multiple tables driven by source address, fwmark, and interface, with concrete configs for split egress, dual ISPs, and tenant isolation.

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 prefix
  • to <dest> — destination address or prefix
  • fwmark <mark> — iptables MARK target value
  • iif <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

  1. Name your custom tables in /etc/iproute2/rt_tables so commands stay readable:

    100  wan-a
    101  wan-b
    200  tenant-blue
  2. Dual-ISP egress by source IP. Two uplinks: eth0 on 192.0.2.0/24 with gateway 192.0.2.1, eth1 on 198.51.100.0/24 with gateway 198.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
  3. 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
  4. Persist with systemd-networkd rather than ad-hoc commands. [RoutingPolicyRule] and Table= sections in a .network file 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
  5. Verify symmetry. For each source you care about, run ip route get <internet-ip> from <source> and confirm the oif matches the intended uplink. Run it again after netplan apply or systemctl restart systemd-networkd to catch ordering bugs.

  6. 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 — set rp_filter=2 (loose) per-interface, or accept asymmetric routes explicitly.

Operational notes

  • ip rule add is idempotent only by exact match; running the same command twice creates two identical rules. Use ip rule flush priority 100 before re-adding when scripting.
  • The main table 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 rule entries; review the full table before assuming yours are the only ones in play.
  • netfilter MARK and connmark are different. Mark on the first packet of a flow with iptables, then CONNMARK --save-mark so reply packets carry the same mark via conntrack — otherwise return traffic falls back to main.
  • Cloud providers may reset routes on metadata refresh. AWS DHCP, Azure agent, and GCP guest agent rewrite main opportunistically; 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.