Skip to content

Routing Tables

Linux routing tables are the foundation of network traffic management, directing packets between interfaces based on destination addresses and policy rules. The ip route command provides granular control over static and dynamic routing configurations, while routing policies and policy-based routing (PBR) enable advanced traffic shaping. This section covers core concepts and practical techniques for managing routing tables effectively.


Static Routing Table Configuration

Static routes are manually defined using the ip route command. They are ideal for small networks or when precise control is needed.

Basic Syntax:

ip route [add|replace|del] <destination> via <gateway> dev <interface> [metric <number>]

Example: Add a static route

ip route add 192.168.2.0/24 via 192.168.1.2 dev eth0 metric 100
This routes traffic destined for 192.168.2.0/24 through the gateway 192.168.1.2 on eth0 with a metric of 100.

Viewing the routing table:

ip route show
Output includes entries like:
192.168.2.0/24 via 192.168.1.2 dev eth0

Advanced Options:
- Use table <table_id> to associate routes with specific routing tables (e.g., table 100).
- Metrics determine route preference; lower values take precedence.


Dynamic Routing Table Management

Dynamic routing protocols (e.g., BGP, OSPF, RIP) automatically update routing tables. Linux integrates with external daemons like bird or quagga for these protocols.

Example: Monitor dynamic routes

ip route show table main
Dynamic routes appear alongside static entries, often marked with via <gateway> or metric <value>.

Note: Dynamic routing requires configuration of the daemon and proper network interface setup.


Routing Policies and Policy-Based Routing (PBR)

Routing policies define rules for packet forwarding, overriding default behavior. PBR allows traffic shaping based on packet attributes (e.g., source IP, protocol).

Configuring Routing Policies:
Use ip rule to define policies:

ip rule add from 192.168.1.0/24 table 100 priority 1000
This routes traffic from 192.168.1.0/24 to routing table 100 with priority 1000.

Associating Routes with Policies:

ip route add 10.0.0.0/24 via 10.1.0.1 dev tun0 table 100
This binds the route to the policy-defined table 100.

Example: PBR for Traffic Shaping
1. Mark packets using iptables:

iptables -t mangle -A PREROUTING -s 192.168.1.0/24 -j MARK --set-mark 1
2. Define a routing rule:
ip rule add mark 1 table 200
3. Add a route for the custom table:
ip route add default via 192.168.2.1 dev eth1 table 200


Troubleshooting and Validation

  • Check routing table:
    ip route show
    
  • Verify routing rules:
    ip rule show
    
  • Monitor traffic:
    Use tcpdump or Wireshark to validate route behavior.
  • Common issues:
  • Conflicting routes with incorrect metrics.
  • Missing rules in policy tables.
  • Misconfigured gateways or interfaces.

Key takeaways

  • Use ip route for static routing, and external daemons for dynamic routing.
  • Routing policies (ip rule) and PBR enable advanced traffic control based on packet attributes.
  • Always validate configurations with ip route show and ip rule show.
  • Troubleshoot with packet capture tools and ensure metrics/tables align with intended behavior.
  • PBR requires careful integration with iptables or nftables for marking traffic.