How to Configure Source-Based Routing on a Linux VPS

Source-based routing directs traffic based on its originating address rather than only its destination — essential for servers with multiple network interfaces or IP addresses that need consistent, deliberate path selection. This guide covers configuration.

Why Standard Routing Isn't Always Sufficient

Default Linux routing decisions are based purely on destination address — for a server with multiple interfaces/IPs where you need traffic from a specific source IP to always exit via a specific corresponding interface/gateway, standard destination-based routing alone can't express this requirement.

Common Use Case: Multiple IPs, Multiple Gateways

If your VPS has multiple IP addresses (see How to Configure Multiple IP Addresses on One VPS) each associated with a different logical network/gateway, source-based routing ensures traffic from a specific IP consistently uses its corresponding gateway, rather than potentially using the wrong one and causing asymmetric routing issues (see How to Diagnose Asymmetric Routing Issues).

Step 1 — Create Custom Routing Tables

echo "100 table1" | sudo tee -a /etc/iproute2/rt_tables
echo "200 table2" | sudo tee -a /etc/iproute2/rt_tables

Named routing tables beyond the default main table, each holding routes specific to a particular source/interface combination.

Step 2 — Add Routes to Each Custom Table

sudo ip route add default via 192.168.1.1 dev eth0 table table1
sudo ip route add default via 192.168.2.1 dev eth1 table table2

Step 3 — Add Rules Directing Traffic to the Correct Table Based on Source

sudo ip rule add from 192.168.1.10 table table1
sudo ip rule add from 192.168.2.10 table table2

These rules are the actual source-based routing logic — traffic originating from a specific source IP is directed to consult the corresponding custom table, ensuring the correct gateway is used for that specific source.

Step 4 — Verify Rule Priority

ip rule show

Rules are evaluated in priority order — ensure your custom rules have appropriate priority relative to the default rules, so they're actually consulted before falling through to default behavior.

Step 5 — Make Configuration Persistent

Add the above commands to a network startup script or your distribution's persistent network configuration mechanism (Netplan, NetworkManager dispatcher scripts) — manual ip rule/ip route commands don't survive a reboot without explicit persistence configuration.

Testing Source-Based Routing

ping -I 192.168.1.10 destination.com
traceroute -s 192.168.1.10 destination.com

Verify traffic from each specific source genuinely takes its expected path, confirming the rules are working as intended.

Combining with the Asymmetric Routing Troubleshooting Context

See How to Diagnose Asymmetric Routing Issues — source-based routing is often the actual fix for asymmetric routing problems on multi-interface servers, addressing the root cause by ensuring consistent, deliberate path selection rather than relying on potentially inconsistent default routing behavior.

Common Errors

Rules configured but traffic still uses the wrong path — verify rule priority ordering, and confirm the source IP in your rule exactly matches the actual source address of the traffic you're trying to route (a common mismatch if the traffic is NAT'd or the source IP differs from what you expect).

Continue Reading

Browse more articles in Advanced Networking & VPN.

  • source based routing linux, ip rule ip route tutorial, multiple gateway routing, linux policy routing
  • 0 Korisnici koji smatraju članak korisnim
Je li Vam ovaj odgovor pomogao?

Vezani članci

How to Set Up a VPN Server with WireGuard

WireGuard is a modern, fast, and simple VPN protocol — significantly easier to configure...

How to Set Up an OpenVPN Server on a VPS

OpenVPN is a mature, widely-supported VPN protocol — a solid choice when you need broad...

How to Configure a VPS as a Forward Proxy with Squid

A forward proxy routes outbound requests through your VPS, useful for accessing geo-restricted...

How to Set Up IPv6 on Your VPS

IPv6 adoption continues to grow, and many VPS providers now offer IPv6 addresses alongside IPv4....

How to Bond Multiple Network Interfaces for Redundancy

Network interface bonding combines multiple physical/virtual network interfaces into a single...