Multi-Homed Linux Boxes: Policy Routing When One Machine Has Two Networks

Why traffic keeps leaving on the wrong interface, and the routing tables that fix it

Contents

I have a box in my rack with two network interfaces: one on the main household VLAN, one on an isolated management network I use for out-of-band access to switches and the hypervisor hosts. The first time I plugged the second NIC in, I expected it to just work — reachable from both networks, replying on whichever interface a request came in on. Instead, replies to management-network clients kept going out the household interface, because that is what a normal Linux routing table does with more than one interface: it picks a single default route and sends everything through it, regardless of which interface a packet arrived on.

This happens because standard routing decisions in Linux are based entirely on destination address, using one routing table. A second NIC on a second subnet gets its own local route to that subnet, but anything destined outside both local subnets — including a reply to a host that just connected to you — falls through to the single default gateway, on whichever interface that gateway happens to live. If your management network’s gateway is not that default, packets addressed back to a management-network client get routed out the wrong interface, arrive with a source address the client’s own gateway does not recognise as local to it, and get silently dropped by that client’s own routing logic before your reply is ever seen. The connection just hangs, source and destination both correctly configured, both technically alive, both looking at their own logs seeing nothing wrong.

Why this looks like a firewall problem and isn’t

The failure mode is deceptive because it looks exactly like a dropped or filtered connection. ping from the management network to the box’s management IP works fine — ICMP echo replies are small and asymmetric routing rarely breaks the outbound leg of the request. TCP connections that stay open longer, or ones where the reply needs to retrace the same path the request took (which is most of them, once a stateful firewall is anywhere in the path), fail in a way that looks identical to a blocked port. I spent longer than I want to admit adding firewall exceptions on both ends before I actually traced the return packets and watched them leave the wrong NIC.

Policy routing: routing tables plural

The fix is policy routing — Linux’s mechanism for having more than one routing table and choosing between them based on rules, rather than always consulting the single main table. ip rule selects which table applies to a packet (by source address, by input interface, by firewall mark, among other criteria); ip route populates each table’s actual routes. The default install has one meaningful table (main) and one rule (use it for everything), which is exactly the setup that produces the asymmetric-routing failure above.

A white railway points lever beside a track, with old rolling stock blurred in the background.
Railway points send each train down one track or another, much as ip rule sends each packet to one routing table. Photo: Points switcher. (10482084506) by Bernard Spragg. NZ from Christchurch, New Zealand, CC0, via Wikimedia Commons.

Suppose eth0 is the household interface (192.168.1.10/24, gateway 192.168.1.1) and eth1 is the management interface (10.10.10.10/24, gateway 10.10.10.1). The goal: traffic sourced from the management IP always routes back out through the management gateway, regardless of what the main table would otherwise pick.

1
2
3
4
5
6
7
8
9
# Create a second routing table (named in /etc/iproute2/rt_tables, or referenced by number)
echo "100 mgmt" | sudo tee -a /etc/iproute2/rt_tables

# Populate it with a default route via the management gateway
sudo ip route add default via 10.10.10.1 dev eth1 table mgmt
sudo ip route add 10.10.10.0/24 dev eth1 src 10.10.10.10 table mgmt

# Rule: any packet SOURCED from the management IP uses the mgmt table
sudo ip rule add from 10.10.10.10 table mgmt priority 100

The from clause is the important part. It does not match on destination, or on which interface the packet is heading toward — it matches on source address, which is exactly the property that was going wrong. Once a packet’s source is the management IP, the kernel now consults the mgmt table first, finds a route back out eth1, and the reply retraces the same path the original request came in on.

Making it survive a reboot

Everything above is runtime-only; a reboot loses it. On distributions using netplan, the equivalent lives in YAML — I’ve covered netplan’s static-IP quirks in more detail elsewhere, but the policy-routing pieces specifically go under routing-policy and routes on the relevant interface:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
network:
  version: 2
  ethernets:
    eth1:
      addresses:
        - 10.10.10.10/24
      routes:
        - to: default
          via: 10.10.10.1
          table: 100
      routing-policy:
        - from: 10.10.10.10/32
          table: 100

For distributions that manage interfaces with plain systemd-networkd .network files instead, the same intent is expressed with [Route] and [RoutingPolicyRule] sections. Either way, the goal is identical: the rule and the table both need to be declared as part of the interface’s normal configuration, not applied by hand after boot, or the very next reboot silently reintroduces the original bug.

Multiple rules, evaluated in order

Rule priority matters once you have more than one. ip rule evaluates rules in ascending priority order and stops at the first match, so a lower number wins. Two common additional rules worth having alongside the source-based one above:

1
2
3
4
5
# Traffic arriving ON eth1 should also leave via eth1's table, even if its source doesn't obviously indicate it
sudo ip rule add iif eth1 table mgmt priority 90

# A route lookup failure should still fall through to the main table rather than blackhole
sudo ip rule add from all lookup main priority 32766

That last rule usually exists by default (ip rule list will show it as one of the built-in rules), which is worth checking before adding a duplicate — Linux ships two implicit rules, one pointing at main and a lower-priority one pointing at a default table, and both need to stay in place unless you have a specific reason to remove them.

The case that breaks people twice: two default gateways

A related, easy mistake is configuring a default gateway on both interfaces in the main table, hoping the kernel will somehow pick the right one per-connection. It will not — only one default route in a table is actually consulted for any given lookup (the others are typically ignored or, worse, chosen inconsistently depending on route metrics you didn’t intend to set). If you find yourself wanting two default gateways, that is precisely the signal that what you actually want is policy routing rather than a second entry in the same table. Give each interface its own table with its own default, and use rules to decide which table a given packet’s source or arriving interface routes it into.

Routing by firewall mark instead of source address

Source-address matching covers most homelab cases, but it breaks down when the thing you want to route differently is not the source IP but some property of the traffic itself — a specific outbound port, a particular process, or connections destined for one specific remote service that should use a different path than everything else leaving the same interface. iptables (or nftables) can tag matching packets with a firewall mark, and ip rule can match on that mark instead of an address:

1
2
3
4
5
# Tag traffic to a specific destination with mark 1
sudo iptables -t mangle -A OUTPUT -d 203.0.113.50 -j MARK --set-mark 1

# Route marked traffic through the mgmt table
sudo ip rule add fwmark 1 table mgmt priority 80

This is the same mechanism a lot of VPN split-tunnel setups use under the hood: mark the traffic that should go over the tunnel, add a rule sending marked packets into a table whose default route is the VPN interface, and leave everything else alone in the main table. It generalises the source-address approach to cases where source address alone cannot express the distinction you need.

Dual-WAN and outbound load distribution

The same mechanism handles a machine with two upstream internet connections rather than two internal networks — a primary fibre line and a 4G failover, say. Each WAN interface gets its own table with its own default route, and a rule sends traffic sourced from each local subnet, or tagged with each interface’s mark, out the matching WAN:

1
2
3
4
sudo ip route add default via 203.0.113.1 dev eth0 table wan1
sudo ip route add default via 198.51.100.1 dev eth2 table wan2
sudo ip rule add from 192.168.1.0/24 table wan1 priority 200
sudo ip rule add from 192.168.2.0/24 table wan2 priority 210

True automatic failover needs more than this — something has to detect a dead WAN and repoint the rule or main-table default, which is a job for a small monitoring script or a tool like mwan3 on OpenWrt-based gear rather than static ip rule entries — but the routing-table half of the problem is identical to the internal multi-homing case above. Anyone who has taken a spare router back from the ISP’s box has almost certainly configured a variant of this exact table-per-uplink pattern, even without realising policy routing was the mechanism underneath it.

Verifying it actually works

ip route get is the single most useful command for confirming policy routing behaves as intended, because it shows you exactly which table and route the kernel would choose for a given source/destination pair without sending any traffic:

1
2
ip route get 10.10.10.50 from 10.10.10.10
# 10.10.10.50 from 10.10.10.10 via 10.10.10.1 dev eth1 table mgmt

If that output still shows the wrong table or the wrong via, the rule priority or the from match is wrong before you go anywhere near a firewall or an application config — this is the check I should have run on day one instead of chasing phantom firewall rules for an evening.

Reverse-path filtering makes this worse if you don’t know about it

Linux has a separate, related feature called reverse-path filtering (rp_filter) that actively drops packets whose source address does not match what the kernel thinks should arrive on that interface, based on the main routing table. In its strict mode (the default on many distributions), it will discard perfectly legitimate return traffic on a multi-homed box if the routing tables and rp_filter’s expectations disagree, producing yet another variant of the same symptom: packets that vanish with no log entry and no error on either side.

1
2
3
4
5
6
# Check current mode per interface (0=off, 1=strict, 2=loose)
sysctl net.ipv4.conf.eth1.rp_filter

# Loose mode accepts a packet if it's routable via ANY interface, not just the one it arrived on —
# the right setting for a policy-routed multi-homed box
sudo sysctl -w net.ipv4.conf.eth1.rp_filter=2

Make this persistent in /etc/sysctl.d/ rather than setting it at runtime, since it resets on reboot along with everything else in this piece. Strict mode is a genuinely useful anti-spoofing control on a single-homed box with one plausible path in and out; on a multi-homed one running policy routing, it actively fights the routing tables you just built, and loose mode is almost always the correct choice once asymmetric-but-intentional routing is in play.

A worked example: the management-network case end to end

Putting the pieces together for the original two-NIC scenario, in the order I’d actually apply them on a fresh box:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# 1. Interface addressing (persistent config, not shown — see netplan example above)
# eth0: 192.168.1.10/24, gw 192.168.1.1
# eth1: 10.10.10.10/24, gw 10.10.10.1

# 2. Second table
echo "100 mgmt" | sudo tee -a /etc/iproute2/rt_tables

# 3. Routes inside it
sudo ip route add default via 10.10.10.1 dev eth1 table mgmt
sudo ip route add 10.10.10.0/24 dev eth1 src 10.10.10.10 table mgmt

# 4. Rules directing the right traffic into it
sudo ip rule add from 10.10.10.10 table mgmt priority 100
sudo ip rule add iif eth1 table mgmt priority 90

# 5. Loosen rp_filter on the second interface
sudo sysctl -w net.ipv4.conf.eth1.rp_filter=2

# 6. Verify
ip route get 10.10.10.50 from 10.10.10.10

Every step here is idempotent and safe to re-run, which matters when you are iterating on a live box over SSH and want to be able to reapply the whole sequence after a typo without first tearing anything down.

Troubleshooting

Rules exist but traffic still leaves the wrong interface. Check priority ordering with ip rule list — a lower-priority-number rule pointing at main placed above your custom rule wins and shadows it entirely.

Works over ping, breaks for TCP services. Classic asymmetric-routing signature, as described above. Confirm with ip route get <dest> from <mgmt-ip> rather than trusting ICMP results.

Rules and routes vanish after reboot. Runtime ip rule/ip route commands are not persistent. Move the same configuration into netplan or your distribution’s persistent network config, as shown above.

A container or VM bridged onto the management interface can’t reach the internet. That is very likely correct behaviour rather than a bug — a policy-routed management network usually should not have a path to the wider internet, and a container inheriting that interface inherits the same routing table.

ip rule add succeeds but the route lookup still picks main. Confirm the destination in mgmt table actually covers the address you’re testing — a table with only a 10.10.10.0/24 route and no default entry falls through for anything outside that subnet, and Linux’s rule-matching moves on to the next rule rather than failing outright, which quietly lands you back in main for exactly the traffic you meant to redirect.

Two default routes in the same table, and it seems to work sometimes. This is the two-default-gateway trap from earlier resurfacing with a twist — if both defaults have different route metrics, the kernel consistently prefers the lower metric, which can make the setup look correct under casual testing while actually being one metric change away from routing everything out the wrong interface. Split the interfaces into separate tables instead of relying on metric ordering within one table; it removes the ambiguity entirely rather than making it depend on a number nobody remembers setting.

VLAN sub-interfaces work the same way

Everything above assumes two physical NICs, but the identical logic applies to a single physical interface carrying multiple VLANs as sub-interfaces (eth0.10, eth0.20, and so on) — from the routing table’s perspective, a VLAN sub-interface is just another interface with its own address and its own place in the rules, indistinguishable from a second physical card. If you’ve segmented a home network into VLANs and then put a single Linux box with legs on more than one of those VLANs — a router, a monitoring host, anything that legitimately needs presence on two segments — the same source-based and interface-based rules apply without modification. The physical-versus-virtual distinction at Layer 2 does not matter to ip rule; only the interface name and the addresses bound to it do.

Multi-homing is one of those Linux features that works exactly as documented and still trips people up constantly, because the failure mode looks like everything except what it actually is. Once you know to reach for ip rule instead of a firewall rule, it stops being mysterious.

Smarc
Written by Smarc

Founder and editor of Aside House. A lifelong tinkerer who self-hosts far more than is sensible, hardens Linux boxes for fun, and prods the latest AI tools to see what they can really do. The how-to guides here are the notes Smarc wishes had existed the first time round.

Smarc is the editor of Aside House. The homelab, the hardware and the opinions are real; the guides are drafted with AI assistance. Infrastructure details are genericised on purpose.