Chapter 7.2 - Routing and Routing Tables
Learning Objectives
By the end of this chapter, you will be able to:
- Understand Linux routing
- View and modify routing tables
- Configure the default route
- Understand static routes
- Troubleshoot routing
What is Routing?
Routing determines how network packets are sent from a source to a destination.
Function:
- Decide where to send packets
- Based on routing tables
- Default route for unknown destinations
Routing Tables
Viewing Routes
# View all routes
ip route show
ip route
route -n # With net-tools
# View specific route
ip route get 8.8.8.8
Structure of a Route
destination via gateway dev interface
Example:
default via 192.168.1.1 dev eth0
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.100
Default Route
Viewing the Default Route
# With ip
ip route show default
ip route | grep default
# With route
route -n | grep "^0.0.0.0"
Adding a Default Route
# With ip
sudo ip route add default via 192.168.1.1 dev eth0
# With route
sudo route add default gw 192.168.1.1 eth0
Removing the Default Route
# With ip
sudo ip route del default
# With route
sudo route del default
Static Routes
Adding a Route
# Route to specific network
sudo ip route add 10.0.0.0/24 via 192.168.1.1 dev eth0
# Route to specific host
sudo ip route add 10.0.0.50 via 192.168.1.1 dev eth0
# Route via local interface
sudo ip route add 172.16.0.0/16 dev eth1
Removing a Route
# Remove route
sudo ip route del 10.0.0.0/24
# Or specify completely
sudo ip route del 10.0.0.0/24 via 192.168.1.1 dev eth0
Examples
# Route to private network via VPN
sudo ip route add 192.168.100.0/24 via 10.8.0.1 dev tun0
# Route to specific server
sudo ip route add 203.0.113.50 via 192.168.1.1 dev eth0
Multiple Routing Tables
Viewing the Tables
# List of tables
ip route show table all
# Specific table
ip route show table main
ip route show table local
Special Tables
- main: Main table (default)
- local: Local routes (127.0.0.0/8, etc.)
- default: Default routes
Adding Route in Specific Table
# Route in custom table
sudo ip route add 10.0.0.0/24 via 192.168.1.1 table 100
# View table
ip route show table 100
Permanent Configuration
Ubuntu/Debian (Netplan)
# /etc/netplan/01-netcfg.yaml
network:
version: 2
ethernets:
eth0:
addresses:
- 192.168.1.100/24
routes:
- to: 10.0.0.0/24
via: 192.168.1.1
- to: 0.0.0.0/0
via: 192.168.1.1
nameservers:
addresses:
- 8.8.8.8
Apply:
sudo netplan apply
Red Hat/CentOS
# File: /etc/sysconfig/network-scripts/route-eth0
10.0.0.0/24 via 192.168.1.1 dev eth0
default via 192.168.1.1 dev eth0
Troubleshooting
Traceroute
# Trace the path
traceroute 8.8.8.8
tracepath 8.8.8.8 # With iputils
# With mtr (install if necessary)
sudo apt install mtr
mtr 8.8.8.8
Check Connection
# Ping
ping -c 4 8.8.8.8
ping -c 4 google.com
# See used route
ip route get 8.8.8.8
Check Default Route
# Default route
ip route show default
# Gateway accessible?
ping -c 2 $(ip route | grep default | awk '{print $3}')
Practical Examples
Routing Configuration Script
#!/bin/bash
# Routing configuration
INTERFACE="eth0"
GATEWAY="192.168.1.1"
# Default route
sudo ip route add default via $GATEWAY dev $INTERFACE
# Route to private network
sudo ip route add 10.0.0.0/24 via $GATEWAY dev $INTERFACE
# Verify
ip route show
Routing Verification
#!/bin/bash
# Verify routing configuration
echo "=== Routes ==="
ip route show
echo "=== Default Route ==="
ip route show default
echo "=== Test Connectivity ==="
ping -c 2 8.8.8.8
Summary
In this chapter, you learned:
Routing: Decision of sending network packets
Tables: main, local, custom tables
Default Route: Gateway for unknown destinations
Static Routes: Specific routes to networks/hosts
Configuration: Netplan, route-* files
Troubleshooting: traceroute, ping, ip route get
Next Steps
Chapter 7.3: DNS and Name Resolution
Chapter 7.4: Network Tools
Chapter created on: December 2024