In this lesson, you will learn how to direct network traffic beyond the default routing table by implementing Policy-Based Routing (PBR).
Beyond the Default Table
By default, the Linux kernel relies on the main routing table to decide where packets go. However, as network complexity grows, you often need to steer specific traffic—such as traffic from a particular source IP or a specific protocol—down a different path. This is where Policy-Based Routing (PBR) comes in.
Instead of asking "where does this destination go?", PBR allows the kernel to ask "who is sending this, and which table should I consult for them?"
Managing Multiple Tables
Linux keeps a list of routing tables defined in /etc/iproute2/rt_tables. You can create custom tables (e.g., one for a VPN tunnel or a secondary ISP) and then use rules to map traffic to them.
-
Define a table: Add a custom ID to
/etc/iproute2/rt_tables.echo "200 vpn_table" >> /etc/iproute2/rt_tables -
Add a route to that table: Direct traffic destined for a specific network through a custom gateway.
ip route add 192.168.50.0/24 dev tun0 table vpn_table -
Create a routing rule: Tell the kernel to prioritize your custom table based on criteria, such as the source IP address.
ip rule add from 10.0.0.5 table vpn_table
How the Kernel Decides
When a packet arrives, the kernel looks at the ip rule list from top to bottom. Rules are processed by priority. If a packet matches the criteria in your rule, the kernel jumps to your specified table to find the next hop. If no match is found, it falls through to the standard main table. This allows you to maintain clean, logical separation between different types of traffic without conflicts in your primary routing infrastructure.
Key Takeaway: Policy-based routing decouples traffic steering from destination-based logic, allowing you to route packets based on source, mark, or protocol by consulting user-defined routing tables.