Managing Routes

The routing table tells the Linux kernel how to forward packets: which interface to use and which gateway (next hop) to send packets through for any given destination. The default route (0.0.0.0/0) handles all traffic not matched by a more specific route. On servers with multiple network interfaces or complex network topologies, proper route management is essential for predictable traffic flow.

Viewing the routing table

# Show the IPv4 routing table
ip route show
ip route    # Shorthand

ip route show output explained

default via 192.168.1.1 dev ens3 proto dhcp src 192.168.1.10 metric 100
#  ↑                ↑                                                   ↑
# 0.0.0.0/0     gateway IP                                         preference (lower = preferred)
192.168.1.0/24 dev ens3 proto kernel scope link src 192.168.1.10
#    ↑                                                      ↑
# local network                                         source address used
# Show which route would be used for a specific destination
ip route get 8.8.8.8
ip route get 10.0.0.5

ip route get 8.8.8.8

8.8.8.8 via 192.168.1.1 dev ens3 src 192.168.1.10 uid 1000
    cache

Adding and removing routes

# Add a temporary route (lost on reboot or if interface restarts)
# Route to 10.20.0.0/24 via gateway 192.168.1.254
sudo ip route add 10.20.0.0/24 via 192.168.1.254

# Add route via specific interface (for routes without a gateway)
sudo ip route add 10.30.0.0/24 dev ens4

# Remove a route
sudo ip route del 10.20.0.0/24 via 192.168.1.254

# Add a more specific route (takes priority over less specific)
sudo ip route add 10.20.5.0/24 via 192.168.1.100    # Only for this /24

📝 NOTE: Routes added with ip route add are temporary — they disappear when the interface goes down or on reboot. For permanent routes that survive reboots, configure them in Netplan.

Persistent routes in Netplan

# /etc/netplan/00-installer-config.yaml
network:
  version: 2
  ethernets:
    ens3:
      dhcp4: false
      addresses: [192.168.1.10/24]
      routes:
        - to: default
          via: 192.168.1.1          # Default gateway
        - to: 10.20.0.0/24
          via: 192.168.1.254        # Route to internal network via different gateway
        - to: 10.30.0.0/16
          via: 192.168.1.253
          metric: 200               # Lower metric = higher preference for overlapping routes

sudo netplan apply

Changing the default gateway

# Temporary (survives until reboot)
sudo ip route del default
sudo ip route add default via 192.168.1.2

# Permanent: edit Netplan
# Change the 'via' address under routes: - to: default

⚠️ WARNING: Deleting the default route on a remote server immediately drops your SSH connection if your client is not on the local segment. Always have console access available before modifying default route on a live server.

Troubleshooting routing problems

# Systematic approach: work from local to remote

# Step 1: Can you reach the local gateway?
ping -c 3 192.168.1.1

# Step 2: Is the gateway in the ARP table?
ip neigh show | grep 192.168.1.1

# Step 3: Is the route correct?
ip route get 8.8.8.8    # Shows which gateway and interface would be used

# Step 4: Can the gateway reach the destination?
traceroute 8.8.8.8    # Or: tracepath 8.8.8.8

# Step 5: Is the route asymmetric? (packets going out one path, returning another)
# Check return path by looking at routing from the destination's perspective

# Step 6: Multiple default gateways (common misconfiguration)
ip route show | grep default    # Should ideally only show one default

Conclusion

The routing table determines where packets go. Use ip route get DESTINATION to immediately see which gateway and interface Linux will use for any specific destination — this is the fastest way to verify routing is correct. Always configure routes in Netplan (not ip route add) for persistence. When troubleshooting connectivity, ip route get is your first stop: if the wrong gateway is shown, fix the routing table first before chasing other causes.

FAQ

Is Managing Routes important for Ubuntu administrators?+

Yes. It supports practical Ubuntu administration because it connects directly to server reliability, security, troubleshooting, or daily operations.

Should I practice this on a live server?+

Use a lab VM first. After you understand the command output and rollback path, apply the workflow carefully on real systems.

What should I do after reading this article?+

Run the practice commands, write down what each one shows, and continue to the next article in the Ubuntu roadmap.

Need help with Ubuntu administration?

Work directly with Muhammad Irfan Aslam for Ubuntu Server, Linux, cloud, Docker, DevOps, CI/CD, or infrastructure troubleshooting support.

Hire Me for Support