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:
Example: Add a static route
192.168.2.0/24 through the gateway 192.168.1.2 on eth0 with a metric of 100.
Viewing the routing table:
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
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:
192.168.1.0/24 to routing table 100 with priority 1000.
Associating Routes with Policies:
100.
Example: PBR for Traffic Shaping
1. Mark packets using iptables:
3. Add a route for the custom table:
Troubleshooting and Validation¶
- Check routing table:
- Verify routing rules:
- Monitor traffic:
UsetcpdumporWiresharkto validate route behavior. - Common issues:
- Conflicting routes with incorrect metrics.
- Missing rules in policy tables.
- Misconfigured gateways or interfaces.
Key takeaways¶
- Use
ip routefor 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 showandip rule show. - Troubleshoot with packet capture tools and ensure metrics/tables align with intended behavior.
- PBR requires careful integration with
iptablesornftablesfor marking traffic.