Configuring Policy-Based Routing (PBR) on CentOS Stream 9 with iproute2 for Multi-Homed Servers

CentOS tutorial - IT technology blog
CentOS tutorial - IT technology blog

The Real-World Problem with Multi-Homed Servers

Last week, I spun up a CentOS Stream 9 server running both internal applications and public-facing Internet services. To keep traffic separated, the server was configured with two network interfaces (multi-homed):

  • ens192: Internal IP 10.10.1.50/24, gateway 10.10.1.1 (used for administrative SSH and database connections).
  • ens224: Public IP 203.0.113.10/24, gateway 203.0.113.1 (handling incoming traffic from external users).

Once the IPs were configured, SSHing into 10.10.1.50 worked smoothly. However, when I tried pinging 203.0.113.10 from an external 4G connection, 100% of the packets returned Request timed out.

At first, I suspected the firewall was blocking the traffic. Running tcpdump -i ens224 icmp showed ICMP echo request packets arriving on ens224 as expected, but not a single echo reply packet was leaving the interface. This is a classic Linux issue when running multiple network interfaces in parallel.

Root Cause: The Default Routing Table and Asymmetric Routing

By default, the Linux kernel routes packets based on the Destination IP (Destination-Based Routing). Every routing decision passes through a single routing table called main.

If you check with ip route show, you will see that the system prioritizes the gateway with the lowest metric (in this case, 10.10.1.1 with a metric of 100):

ip route show
# default via 10.10.1.1 dev ens192 proto static metric 100
# default via 203.0.113.1 dev ens224 proto static metric 101
# 10.10.1.0/24 dev ens192 proto kernel scope link src 10.10.1.50 metric 100
# 203.0.113.0/24 dev ens224 proto kernel scope link src 203.0.113.10 metric 101

The failure happens in these exact 4 steps:

  1. An external Internet client sends a packet to the public IP 203.0.113.10 via ens224.
  2. The server processes the request and prepares to send the reply back to the client’s IP.
  3. The kernel looks up the main table to find an outbound path. Without a specific route, it immediately picks the highest-priority default gateway, which is 10.10.1.1 (via ens192).
  4. The reply packet—carrying the source IP 203.0.113.10—is pushed out through the internal interface ens192. The internal router sees an unexpected source IP and immediately drops the packet.

This in-one-interface, out-another behavior is called Asymmetric Routing. On top of that, CentOS Stream 9 typically enables rp_filter = 1 (Strict mode) by default. When the kernel sees a packet arriving on ens224 while the return path points out of ens192, it drops the packet right at the gate.

Three Common Approaches (and Why Only One Truly Fixes It)

When faced with this issue, sysadmins usually try one of the three approaches below.

Option 1: Add Destination-Based Static Routes

This keeps a single primary gateway and uses ip route add to direct a few specific subnets out of the second interface. If you only need to reach 2–3 fixed VPN or branch office subnets, this works cleanly. However, for a public service receiving traffic from millions of random Internet IPs, you cannot possibly list every destination IP as a static route.

Option 2: Disable rp_filter in sysctl

Many online guides suggest setting net.ipv4.conf.all.rp_filter = 0 or 2 (Loose mode). While this stops the kernel from dropping the packet during its ingress check, it does not fix the root cause: reply packets are still sent out the wrong interface (ens192) and continue to be dropped by the upstream router.

Option 3: Use Policy-Based Routing (PBR) with iproute2

Instead of looking only at the destination IP, Policy-Based Routing (PBR) lets you route traffic based on the Source IP. The rule is straightforward: packets originating from an interface’s IP must leave through that same interface’s gateway. This is the definitive solution for multi-homed servers.

Properly Configuring Policy-Based Routing on CentOS Stream 9

On CentOS Stream 9, iproute2 handles dynamic routing while NetworkManager (nmcli) persists the configuration across reboots. Note that legacy ifcfg-* files have been deprecated by default on RHEL/CentOS 9. We will test the setup live with iproute2 first, and then save it permanently in NetworkManager.

Step 1: Create a Custom Routing Table in iproute2

By default, Linux includes three system tables: local (255), main (254), and default (253). You can create custom tables with IDs from 1 to 252—for example, ID 200 named isp2 dedicated to ens224.

On minimal installations, /etc/iproute2/rt_tables might not exist yet (the default template lives at /usr/share/iproute2/rt_tables). Create the directory if needed and append the new table:

sudo mkdir -p /etc/iproute2
echo "200 isp2" | sudo tee -a /etc/iproute2/rt_tables

Step 2: Configure Temporary Routes and Rules with iproute2

Next, add the local subnet route and default gateway specifically to the isp2 table:

sudo ip route add 203.0.113.0/24 dev ens224 src 203.0.113.10 table isp2
sudo ip route add default via 203.0.113.1 dev ens224 table isp2

With the table populated, create a rule telling the kernel that any packet originating from 203.0.113.10 must look up the isp2 table instead of main:

sudo ip rule add from 203.0.113.10/32 table isp2 priority 100

Verify the newly created rule and routing table:

ip rule show
ip route show table isp2

To immediately verify the outbound path to the Internet (for example, to 8.8.8.8) from source IP 203.0.113.10, run:

ip route get 8.8.8.8 from 203.0.113.10

Getting via 203.0.113.1 dev ens224 src 203.0.113.10 in the output confirms it is working properly. Try pinging or SSHing into the public IP over 4G right now—the connection should work immediately.

Step 3: Persist the Configuration with NetworkManager (nmcli)

The commands in Step 2 only live in RAM and will be wiped after a reboot. On CentOS Stream 9, the cleanest way to make PBR persistent is with nmcli.

First, check the connection name associated with ens224:

nmcli connection show

Assuming the connection name is also ens224, assign the routes to table 200 and add the corresponding routing rule:

sudo nmcli connection modify ens224 +ipv4.routes "203.0.113.0/24 0.0.0.0 table=200"
sudo nmcli connection modify ens224 +ipv4.routes "0.0.0.0/0 203.0.113.1 table=200"
sudo nmcli connection modify ens224 +ipv4.routing-rules "priority 100 from 203.0.113.10/32 table 200"

Finally, reactivate the connection so NetworkManager applies the changes:

sudo nmcli connection up ens224

Quick tip: If you want to keep ens192 as the default path for outbound traffic initiated by the server itself (such as dnf update or internal API calls), set ipv4.never-default yes on ens224 or give it a higher metric than ens192. This keeps the main table clean, while incoming traffic on ens224 still replies through the right interface thanks to table 200.

Step 4: Verify After Completion

Use curl with the --interface flag to test outbound connectivity independently on each interface:

curl --interface ens192 -s https://ifconfig.me
curl --interface ens224 -s https://ifconfig.me

If each command returns the expected public IP for its respective link, you are all set. Finally, reboot the server once and run ip rule show again to make sure the configuration persists cleanly.

Share: