Operations 25 min read

Master Linux Routing in 3 Days: From Basics to Advanced Configuration

This comprehensive guide walks you through Linux routing fundamentals, table structures, static and dynamic configurations, as well as advanced techniques like policy routing and ECMP, as well as troubleshooting, performance tuning, security measures, and best practices, enabling you to become a network expert in just three days.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Linux Routing in 3 Days: From Basics to Advanced Configuration

Linux Routing Configuration from Beginner to Expert: 10 Years of Ops Secrets to Become a Network Pro in 3 Days

1. Routing Basics

1.1 What is Routing

Routing is the core mechanism of network communication that determines the path a packet takes from source to destination. In Linux, routing is provided by the kernel and managed through routing tables.

1.2 How Routing Works

When a packet needs to be sent, the Linux kernel makes routing decisions in the following steps:

Destination address check : first check whether the destination address is the local address.

Routing table lookup : search for a matching entry in the routing table.

Longest prefix match : select the entry with the longest subnet mask.

Default route : if no entry matches, use the default route.

Packet forwarding : forward the packet to the specified gateway or interface.

1.3 Routing Table Structure

The Linux routing table contains the following key fields:

Destination : target network or host

Gateway : gateway address

Netmask : subnet mask

Flags : route flags

Metric : route priority

Interface : outgoing interface

2. Routing Types Classification

2.1 Classification by Scope

Local Routes

Directly connected networks

Communication between local interfaces

Automatically generated by the system

Network Routes

Routes to specific network segments

Require forwarding through a gateway

Can be static or dynamic

Host Routes

Routes to a specific host

Subnet mask 255.255.255.255

Used for precise control of the path to a host

2.2 Classification by Configuration Method

Static Routing

Manually configured route entries

Simple configuration, low performance overhead

Suitable for relatively fixed network topologies

Dynamic Routing

Learned automatically via routing protocols

Adaptable to network changes

Requires a routing daemon

3. Routing Inspection Commands

3.1 route command

Basic syntax route [-n] [-v] [-A family] Common options -n: display addresses numerically, no DNS lookup -v: verbose output -A inet: specify address family (IPv4)

Example output interpretation

$ route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG    0      0      0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U     0      0      0 eth0

Flag meanings U: route is up G: route uses a gateway H: target is a host R: route restored by dynamic routing D: installed by routing daemon

3.2 ip route command

Basic syntax ip route [list|show] [SELECTOR] Show all routes ip route show Show routes for a specific network ip route show 192.168.1.0/24 Show default route

ip route show default

3.3 netstat command

Show routing table netstat -rn Show IPv6 routes

netstat -rn -A inet6

4. Static Route Configuration

4.1 Adding Route Entries

Using route command

Add network route:

route add -net 192.168.2.0/24 gw 192.168.1.1 dev eth0

Add host route:

route add -host 192.168.2.100 gw 192.168.1.1 dev eth0

Add default route: route add default gw 192.168.1.1 dev eth0 Using ip command

Add network route:

ip route add 192.168.2.0/24 via 192.168.1.1 dev eth0

Add host route:

ip route add 192.168.2.100/32 via 192.168.1.1 dev eth0

Add default route:

ip route add default via 192.168.1.1 dev eth0

4.2 Deleting Route Entries

Using route command

route del -net 192.168.2.0/24 gw 192.168.1.1
route del -host 192.168.2.100
route del default gw 192.168.1.1

Using ip command

ip route del 192.168.2.0/24 via 192.168.1.1
ip route del 192.168.2.100/32 via 192.168.1.1
ip route del default via 192.168.1.1

4.3 Modifying Route Entries

Replace route

ip route replace 192.168.2.0/24 via 192.168.1.2 dev eth0

Change route attributes

ip route change 192.168.2.0/24 via 192.168.1.2 dev eth0 metric 100

5. Permanent Route Configuration

5.1 Using configuration files

Red Hat family (CentOS/RHEL/Fedora)

# /etc/sysconfig/network-scripts/route-eth0
192.168.2.0/24 via 192.168.1.1 dev eth0
10.0.0.0/8 via 192.168.1.2 dev eth0

Debian/Ubuntu

auto eth0
iface eth0 inet static
    address 192.168.1.10
    netmask 255.255.255.0
    gateway 192.168.1.1
    up route add -net 192.168.2.0/24 gw 192.168.1.1 dev eth0
    down route del -net 192.168.2.0/24 gw 192.168.1.1 dev eth0

5.2 Using NetworkManager

nmcli command

# Add static route
nmcli con mod eth0 +ipv4.routes "192.168.2.0/24 192.168.1.1"

# Activate configuration
nmcli con up eth0

5.3 Using systemd-networkd

Create network configuration file:

# /etc/systemd/network/eth0.network
[Match]
Name=eth0

[Network]
DHCP=no
Address=192.168.1.10/24
Gateway=192.168.1.1

[Route]
Destination=192.168.2.0/24
Gateway=192.168.1.1

6. Advanced Routing Configuration

6.1 Multipath Routing

ECMP configuration

ip route add 192.168.2.0/24 \
    nexthop via 192.168.1.1 dev eth0 weight 1 \
    nexthop via 192.168.1.2 dev eth1 weight 1

View multipath route

ip route show 192.168.2.0/24

6.2 Policy Routing

Create routing table

echo "100 custom_table" >> /etc/iproute2/rt_tables

Add policy routes

# Source‑based policy routing
ip rule add from 192.168.1.0/24 table custom_table

# Destination‑based policy routing
ip rule add to 10.0.0.0/8 table custom_table

# Interface‑based policy routing
ip rule add iif eth0 table custom_table

Add route to custom table

ip route add default via 192.168.2.1 table custom_table

6.3 Route priority and metric

Set route priority

ip route add 192.168.2.0/24 via 192.168.1.1 dev eth0 metric 100
ip route add 192.168.2.0/24 via 192.168.1.2 dev eth1 metric 200

View route priority

ip route show 192.168.2.0/24

7. Routing Table Management

7.1 Multiple routing tables

List routing tables cat /etc/iproute2/rt_tables Show routes in a specific table

ip route show table main
ip route show table local
ip route show table 100

7.2 Routing cache

View routing cache ip route show cache Flush routing cache

ip route flush cache

7.3 Routing rule management

Show all rules ip rule show Delete a rule

ip rule del from 192.168.1.0/24 table custom_table

8. Dynamic Routing Configuration

8.1 RIP

Install quagga:

# CentOS/RHEL
yum install quagga

# Debian/Ubuntu
apt-get install quagga

Configure RIP (/etc/quagga/ripd.conf):

router rip
 version 2
 network 192.168.1.0/24
 network 192.168.2.0/24
 redistribute connected

8.2 OSPF

Configure OSPF (/etc/quagga/ospfd.conf):

router ospf
 network 192.168.1.0/24 area 0
 network 192.168.2.0/24 area 0

8.3 BGP

Configure BGP (/etc/quagga/bgpd.conf):

router bgp 65001
 bgp router-id 192.168.1.1
 network 192.168.1.0/24
 neighbor 192.168.2.1 remote-as 65002

9. Routing Troubleshooting

9.1 Common routing problems

Route unreachable

Check if the routing table contains the corresponding entry

Verify that the gateway is reachable

Confirm that the interface status is normal

Routing loops

Check for circular references in the routing table

Verify route priority settings

Inspect dynamic routing protocol configuration

9.2 Diagnostic tools

ping ping -c 4 192.168.2.100 traceroute traceroute 192.168.2.100 mtr mtr 192.168.2.100 tcpdump

tcpdump -i eth0 host 192.168.2.100

9.3 Log analysis

System logs

journalctl -u network
tail -f /var/log/messages

Routing daemon logs

journalctl -u quagga

10. Performance Optimization

10.1 Routing table optimization

Reduce routing table size

Use route aggregation

Delete unnecessary route entries

Optimize routing table structure

Route cache optimization

# Adjust route cache parameters
echo 1024 > /proc/sys/net/ipv4/route/max_size
echo 300 > /proc/sys/net/ipv4/route/gc_timeout

10.2 Kernel parameter tuning

IP forwarding

echo 1 > /proc/sys/net/ipv4/ip_forward
echo 1 > /proc/sys/net/ipv4/conf/all/forwarding

Routing performance parameters

# Adjust route refresh interval
echo 1 > /proc/sys/net/ipv4/route/gc_interval

# Adjust route table size limit
echo 32768 > /proc/sys/net/ipv4/route/max_size

11. Security Configuration

11.1 Routing security

Prevent route spoofing

# Enable reverse path filtering
echo 1 > /proc/sys/net/ipv4/conf/all/rp_filter

Restrict route updates

# Configure route authentication (add authentication in routing protocol configuration)

11.2 Access control

Control routing with iptables

# Block routing from a specific subnet
iptables -I FORWARD -s 192.168.3.0/24 -j DROP

12. Monitoring and Maintenance

12.1 Routing monitoring

Real‑time monitoring of route changes ip monitor route Monitor routing table size

#!/bin/bash
while true; do
    route_count=$(ip route show | wc -l)
    echo "$(date): Route count: $route_count"
    sleep 60
done

12.2 Automated maintenance

Route backup script

#!/bin/bash
backup_dir="/backup/network"
timestamp=$(date +%Y%m%d_%H%M%S)

# Backup routing table
ip route show > "${backup_dir}/routes_${timestamp}.txt"

# Backup routing rules
ip rule show > "${backup_dir}/rules_${timestamp}.txt"

Route restore script

#!/bin/bash
backup_file="/backup/network/routes_20240101_120000.txt"

# Flush existing routes
ip route flush table main

# Restore routes
while read line; do
    ip route add $line
done < $backup_file

13. Best Practices

13.1 Configuration standards

Naming conventions

Use meaningful routing table names

Assign fixed IDs to custom tables

Keep configuration file comments complete

Version control

Put network configuration files under version control

Record reasons for each change

Establish rollback mechanisms

13.2 Operational recommendations

Regular checks

Periodically verify routing table correctness

Monitor routing performance metrics

Clean up invalid routes promptly

Documentation

Maintain network topology diagrams

Record routing policies and reasons

Establish incident handling procedures

13.3 Disaster recovery design

Redundant routes

Configure multiple backup routes

Set appropriate route priorities

Implement automatic failover

Fast recovery

Prepare routing configuration templates

Build automated deployment processes

Conduct regular failover drills

14. Case Studies

14.1 Enterprise network routing design

Requirements

Interconnect multiple subnets

Access to external networks

High availability

Solution

# Primary routes
ip route add 10.1.0.0/16 via 192.168.1.1 dev eth0 metric 10
ip route add 10.2.0.0/16 via 192.168.1.2 dev eth1 metric 10

# Backup routes
ip route add 10.1.0.0/16 via 192.168.1.3 dev eth2 metric 20
ip route add 10.2.0.0/16 via 192.168.1.4 dev eth3 metric 20

# Default routes
ip route add default via 192.168.1.1 dev eth0 metric 10
ip route add default via 192.168.1.2 dev eth1 metric 20

14.2 Load‑balancing routing

Multi‑link load balancing

# Configure multipath route
ip route add default scope global \
    nexthop via 192.168.1.1 dev eth0 weight 1 \
    nexthop via 192.168.1.2 dev eth1 weight 1

Policy‑based load balancing

# Create load‑balance table
echo "200 load_balance" >> /etc/iproute2/rt_tables

# Policy routing
ip rule add from 192.168.1.0/24 table load_balance
ip route add default via 192.168.1.1 table load_balance

15. Summary

Linux routing configuration is a core skill for network management. Mastering routing principles, configuration methods, and troubleshooting techniques is essential for operations engineers. Apply the concepts, tools, and best practices presented here to build reliable, secure, and high‑performance network infrastructures.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

routingSysadminip
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.