Routing — How Linux Sends Network Traffic¶
Routing is the process of determining the best path for network packets to travel from a source to a destination. Every Linux server, cloud instance, Kubernetes node, and network device relies on routing tables to decide where packets should be sent. Understanding routing is essential for Linux administrators, DevOps engineers, Cloud Architects, Network Engineers, and Site Reliability Engineers (SREs).
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand routing
- Learn how routing tables work
- Understand default gateways
- Configure static routes
- View routing information
- Troubleshoot routing issues
- Apply routing concepts in production
Prerequisites¶
Complete:
- Module 1 – Linux Fundamentals
- Module 2 – Linux Command Line Essentials
- Module 3 – Text Processing
- Module 4 – File Management
- Module 5 – Users and Groups
- Module 6 – Process Management
- Module 7 – Package Management
- Module 8 Lessons 1–3
Why Learn Routing?¶
Imagine:
- A Linux server cannot reach another network.
- A Kubernetes node cannot communicate with another node.
- SSH works locally but fails across subnets.
- A cloud VM has Internet access but cannot reach a private network.
In many cases, the cause is an incorrect routing configuration.
What is Routing?¶
Routing is the process of forwarding network packets from one network to another.
A router or a Linux system examines the destination IP address and determines the next hop based on its routing table.
How Routing Works¶
What is a Routing Table?¶
A routing table is a list of routes that tells the operating system how to reach different networks.
Each route typically contains:
- Destination network
- Gateway (next hop)
- Network interface
- Metric (priority)
View the Routing Table¶
Display all routes.
Example:
Understanding a Route¶
Example:
Meaning:
- Destination network:
- Next hop:
- Interface:
Default Route¶
The default route is used when no more specific route matches the destination.
Example:
Without a default route, a system typically cannot communicate with external networks such as the Internet.
Routing Decision Process¶
Linux searches the routing table using the longest prefix match.
Example:
The most specific matching route is selected.
Add a Temporary Route¶
Add a route to another network.
Verify:
This route is temporary and will not survive a reboot.
Delete a Route¶
Replace the Default Route¶
Persistent Routes¶
Routes added with the ip command are temporary.
Persistent routes are configured using the operating system's networking tools.
Examples:
- Ubuntu: Netplan
- RHEL / Rocky / AlmaLinux: NetworkManager (
nmcli) or network configuration files
Route Lookup¶
Check which route Linux will use.
Example:
This command is extremely useful when troubleshooting routing decisions.
Enable IP Forwarding¶
Linux can act as a router by forwarding packets.
Temporarily enable IP forwarding.
Verify:
For permanent configuration, update the appropriate sysctl configuration file.
Common Commands¶
Display routing table.
View a specific route.
Add route.
Delete route.
Enable IP forwarding.
Real Production Examples¶
Verify default gateway.
Check Internet route.
Add a route to a private subnet.
Enable packet forwarding on a gateway server.
Production Perspective¶
Routing is essential for:
- Cloud virtual machines
- Kubernetes clusters
- VPN gateways
- Firewalls
- Load balancers
- Multi-subnet environments
- Hybrid cloud networks
- Enterprise data centers
Incorrect routing is one of the most common causes of network connectivity problems.
Hands-on Lab¶
Task 1¶
Display the routing table.
Task 2¶
Identify the default route.
Task 3¶
Determine the route to Google's DNS server.
Task 4¶
Add a temporary static route.
Replace the gateway with one appropriate for your network.
Task 5¶
Verify the new route.
Task 6¶
Delete the temporary route.
Task 7¶
Check whether IP forwarding is enabled.
Task 8¶
Enable IP forwarding temporarily.
Command Deep Dive¶
| Command | Purpose | Production Example |
|---|---|---|
ip route | Display routing table | Verify routes |
ip route get | Show route to a destination | Troubleshooting |
ip route add | Add static route | Multi-network connectivity |
ip route del | Delete route | Cleanup |
ip route replace | Replace default route | Gateway updates |
sysctl net.ipv4.ip_forward | Check IP forwarding | Router verification |
Static vs Dynamic Routing¶
| Feature | Static Routing | Dynamic Routing |
|---|---|---|
| Configuration | Manual | Automatic |
| Scalability | Small networks | Large networks |
| Maintenance | Higher | Lower |
| Complexity | Simple | More complex |
| Common Use | Small LANs, cloud VMs | Enterprise networks |
Dynamic routing commonly uses protocols such as:
- OSPF
- BGP
- RIP
- EIGRP (Cisco)
Linux servers typically use static routes unless participating in a routing infrastructure.
Production Troubleshooting Scenario¶
Scenario
A Linux application server cannot communicate with a database located in another subnet.
Investigation:
Check the routing table.
Verify the route.
Output:
Add the required route.
Verify:
The route now exists, and the application can communicate with the database.
A permanent route is then configured using the operating system's network management tools.
Best Practices¶
- Use the
ipcommand instead of the deprecatedroutecommand. - Verify the routing table before troubleshooting applications.
- Configure persistent routes through supported network management tools.
- Use static routes only when necessary.
- Document custom routes in production environments.
- Verify routing after network configuration changes.
Common Mistakes¶
❌ Forgetting that routes added with ip route add are temporary.
✅ Remember to that routes added with ip route add are temporary.
❌ Configuring an incorrect default gateway.
✅ Avoid this mistake: configuring an incorrect default gateway.
❌ Adding overlapping or conflicting routes.
✅ Avoid this mistake: adding overlapping or conflicting routes.
❌ Ignoring routing when troubleshooting connectivity issues.
✅ Always review routing when troubleshooting connectivity issues.
Interview Questions¶
Beginner¶
- What is routing?
- What is a routing table?
- What is the purpose of a default gateway?
- Which command displays the routing table?
Intermediate¶
- What is the difference between a static route and a dynamic route?
- How do you add a temporary route?
- What does
ip route getdo? - What is IP forwarding?
Architect Level¶
- How would you design routing for a multi-subnet cloud environment?
- How would you troubleshoot communication between Kubernetes worker nodes?
- Why is the longest prefix match important in routing decisions?
Summary¶
In this lesson, you learned:
- Routing fundamentals
- Routing tables
- Default gateways
- Static routes
- Route lookup
- IP forwarding
- Route troubleshooting
- Production networking best practices
Routing determines how Linux systems send packets across networks. A solid understanding of routing tables, gateways, and static routes enables administrators to build reliable, scalable, and well-connected infrastructure.
Key Takeaways¶
- Routing determines the path packets take to reach their destination.
- The routing table contains information about reachable networks.
- The default gateway handles traffic for unknown networks.
- Use
ip routeto view and manage routes. - Use
ip route getto understand routing decisions. - Routes added with the
ipcommand are temporary unless configured persistently.
What's Next?¶
ping — Testing Network Connectivity in Linux
You'll explore:
- How ICMP works
- Testing network connectivity
- Measuring latency
- Understanding packet loss
- Common
pingoptions - Troubleshooting network issues
- Production best practices
The ping command is one of the most widely used tools for verifying network connectivity and diagnosing communication problems.