When a Linux server has two network interfaces connected to two different ISPs, the kernel defaults to recognizing only a single default gateway. The consequence is all too familiar: incoming packets arrive on the second interface, but the response packets (SYN-ACK) get routed out through the first interface and vanish into thin air. To solve this, you need Policy-Based Routing (PBR) using the ip route and ip rule commands.
1. Quick Start: Configure PBR in 5 Minutes
Suppose an Ubuntu 22.04 server has two network interfaces with the following setup:
- eth0 (ISP 1 – VNPT 300 Mbps): IP
192.168.1.100/24, Gateway192.168.1.1 - eth1 (ISP 2 – Viettel 150 Mbps): IP
10.0.0.100/24, Gateway10.0.0.1
Goal: Ensure packets are routed back through the gateway of the interface they arrived on.
Step 1: Name the Routing Tables
Open the routing table configuration file and define two new tables:
echo "200 isp1" | sudo tee -a /etc/iproute2/rt_tables
echo "201 isp2" | sudo tee -a /etc/iproute2/rt_tables
Each table is assigned a numerical ID from 1 to 252 (avoid reserved system IDs like 0, 253, 254, and 255).
Step 2: Create Dedicated Routes for Each Table
# Dedicated routing for ISP 1
sudo ip route add 192.168.1.0/24 dev eth0 src 192.168.1.100 table isp1
sudo ip route add default via 192.168.1.1 dev eth0 table isp1
# Dedicated routing for ISP 2
sudo ip route add 10.0.0.0/24 dev eth1 src 10.0.0.100 table isp2
sudo ip route add default via 10.0.0.1 dev eth1 table isp2
Step 3: Create Rules to Route by Source IP
# Look up the corresponding routing table based on the source IP of the interface
sudo ip rule add from 192.168.1.100 table isp1 priority 1000
sudo ip rule add from 10.0.0.100 table isp2 priority 1001
# Flush the cache so the kernel applies the new rules immediately
sudo ip route flush cache
Verify the setup using curl bound to a specific interface:
curl --interface eth1 https://api.ipify.org
# Returns the public IP of the Viettel line (ISP 2)
Now, SSH sessions or web servers listening on 10.0.0.100 will respond seamlessly without being forced out through eth0.
2. Under the Hood: Why Default Routing Fails
The Pitfall of Traditional Routing Tables
By default, Linux inspects only the Destination IP in the main table to determine the path. When two gateways exist, the system retains only a single default route with the lowest metric. All outbound traffic gets funneled there.
The issue arises when an incoming request arrives via eth1. Once the server finishes processing and prepares to send a response, the kernel checks the main table, sees that the default gateway points to eth0, and pushes the packet out through eth0 while keeping the source IP as 10.0.0.100. The router on the VNPT side (eth0) drops the packet immediately upon seeing an unfamiliar source IP, and the TCP connection breaks down within seconds.
# Capture packets to verify asymmetric routing:
sudo tcpdump -nn -i eth1 port 22
If you see incoming SYN packets but no corresponding SYN-ACK packets leaving eth1, the server is almost certainly sending replies out via eth0.
How the Routing Policy Database (RPDB) Works
PBR intercepts traffic before the kernel performs standard routing lookups. The kernel traverses the ip rule list in ascending order of priority value (0 being the highest priority):
ip rule show
# 0: from all lookup local
# 1000: from 192.168.1.100 lookup isp1
# 1001: from 10.0.0.100 lookup isp2
# 32766: from all lookup main
# 32767: from all lookup default
Because priorities 1000 and 1001 precede 32766 (the main table), packets with a specific source IP are routed to the appropriate isp1 or isp2 table before ever hitting the global default gateway.
3. Advanced Techniques: Load Balancing and Policy Routing by Service
Outbound Load Balancing with Multipath Nexthop
When the server initiates outbound connections (such as downloading files or calling external APIs), you can distribute traffic based on bandwidth ratios (for example, splitting 2:1 between VNPT 300 Mbps and Viettel 150 Mbps):
sudo ip route replace default scope global \
nexthop via 192.168.1.1 dev eth0 weight 2 \
nexthop via 10.0.0.1 dev eth1 weight 1
The Linux kernel hashes connections on a per-flow basis. A single TCP session between the server and a destination remains pinned to one route, preventing unexpected mid-session IP changes that would break connections.
Pro tip: Enable L4 hashing to factor in source and destination ports, distributing traffic more evenly across multiple connections to the same host:
sudo sysctl -w net.ipv4.fib_multipath_hash_policy=1
Routing Specific Application Traffic via Secondary ISP Using iptables (fwmark)
If you want ports 80 and 443 to go through ISP 2 while all other traffic routes through ISP 1, use firewall marks (fwmark):
# 1. Mark HTTP/HTTPS packets originating from the server with mark 2
sudo iptables -t mangle -A OUTPUT -p tcp -m multiport --dports 80,443 -j MARK --set-mark 2
# 2. Route packets with mark 2 to the isp2 table
sudo ip rule add fwmark 2 table isp2 priority 500
# 3. Flush the routing cache
sudo ip route flush cache
4. Critical Operational Considerations
Handling Reverse Path Filtering (rp_filter)
The kernel provides the rp_filter security mechanism to prevent IP spoofing. If a packet enters through interface A but the optimal return path points to interface B, the kernel drops it immediately at the network layer.
When running multi-homed networking, you must switch this mechanism from strict mode (1) to loose mode (2):
# Switch to loose mode (2) for all interfaces
sudo sysctl -w net.ipv4.conf.all.rp_filter=2
sudo sysctl -w net.ipv4.conf.default.rp_filter=2
sudo sysctl -w net.ipv4.conf.eth0.rp_filter=2
sudo sysctl -w net.ipv4.conf.eth1.rp_filter=2
# Persist permanently to /etc/sysctl.d/99-pbr.conf
echo "net.ipv4.conf.all.rp_filter = 2" | sudo tee -a /etc/sysctl.d/99-pbr.conf
echo "net.ipv4.conf.default.rp_filter = 2" | sudo tee -a /etc/sysctl.d/99-pbr.conf
Making the Configuration Persistent Across Reboots
Manual commands are lost when the system restarts. You must make the configuration persistent according to your Linux distribution.
On Ubuntu 20.04/22.04/24.04 (Netplan – /etc/netplan/01-netcfg.yaml):
network:
version: 2
ethernets:
eth0:
addresses: [192.168.1.100/24]
routes:
- to: default
via: 192.168.1.1
metric: 100
eth1:
addresses: [10.0.0.100/24]
routes:
- to: default
via: 10.0.0.1
table: 201
routing-policy:
- from: 10.0.0.100
table: 201
priority: 1001
Apply the Netplan configuration using sudo netplan apply.
On RHEL 8/9, Rocky Linux, AlmaLinux (NetworkManager):
Starting with RHEL 8, use nmcli instead of legacy network configuration files:
# Add route to table 201
nmcli connection modify eth1 +ipv4.routes "0.0.0.0/0 10.0.0.1 201"
# Add routing rule for eth1's IP
nmcli connection modify eth1 +ipv4.routing-rules "priority 1001 from 10.0.0.100 table 201"
# Restart the connection to apply configuration
nmcli connection up eth1

