Linux Commands for Modifying Routes

In Linux systems, modifying routes (i.e., configuring the network routing table) is primarily done using the ip route command (recommended, part of the iproute2 toolkit, standard in modern Linux distributions) or the traditional route command (older, still supported by some systems). Here are common operations and examples:

1. View Current Routing Table

Before making changes, it is advisable to check the existing routing rules:

ip route show      # Recommended (iproute2)
# Or
route -n           # Traditional command (displays IP in numeric format, does not resolve hostnames)

Example output:

default via 192.168.1.1 dev eth0 proto dhcp metric 100
192.168.1.0/24 dev eth0 proto kernel scope link src 192.168.1.100

2. Add Routes (Temporary, Lost After Reboot)

1. Add Default Gateway (Default Route)

When the system needs to access other networks (such as the internet), a default gateway must be specified:

sudo ip route add default via <gateway IP> dev <interface name>

Example: Set the default gateway to 192.168.1.1, through the interface eth0:

sudo ip route add default via 192.168.1.1 dev eth0

2. Add Route to Specific Network

To access a specific subnet (e.g., 10.0.0.0/24), specify the gateway or interface:

sudo ip route add <target network>/<mask bits> via <gateway IP> dev <interface name>

Example: Access the 10.0.0.0/24 subnet, through the gateway 192.168.1.2 and interface eth0:

sudo ip route add 10.0.0.0/24 via 192.168.1.2 dev eth0

Simplified Version (if the target network is directly reachable through the current interface, no gateway is needed):

sudo ip route add 10.0.0.0/24 dev eth0

3. Delete Routes (Temporary)

1. Delete Default Route

sudo ip route del default

Or specify to delete a particular default route (e.g., through a specific gateway):

sudo ip route del default via <gateway IP>

2. Delete Route to Specific Network

sudo ip route del <target network>/<mask bits>

Example: Delete the route to 10.0.0.0/24:

sudo ip route del 10.0.0.0/24

4. Make Changes Permanent (Modify Configuration Files)

The routes modified by the above commands will be lost after a reboot. To make them permanent, you need to edit the network configuration files (the paths vary by Linux distribution):

1. Debian/Ubuntu (using netplan or /etc/network/interfaces)

  • Netplan (Ubuntu 18.04+): Edit the configuration file (e.g., /etc/netplan/01-netcfg.yaml), and add the routes field under the corresponding interface:
    network:
      version: 2
      ethernets:
        eth0:
          dhcp4: no
          addresses: [192.168.1.100/24]
          gateway4: 192.168.1.1       # Default gateway
          routes:
            - to: 10.0.0.0/24         # Specific network route
              via: 192.168.1.2

    Apply the configuration:

    sudo netplan apply

  • Traditional /etc/network/interfaces: Add the up route add or gateway directive in the file:
    auto eth0
    iface eth0 inet static
        address 192.168.1.100
        netmask 255.255.255.0
        gateway 192.168.1.1          # Default gateway
        up route add -net 10.0.0.0 netmask 255.255.255.0 gw 192.168.1.2  # Specific route

    Restart the network service:

    sudo systemctl restart networking

2. RHEL/CentOS (using /etc/sysconfig/network-scripts/ or nmcli)

  • Traditional Interface Configuration File (e.g., /etc/sysconfig/network-scripts/ifcfg-eth0): Add GATEWAY (default gateway) and ROUTE directives (some versions support):
    GATEWAY=192.168.1.1
    # Specific route must be through an additional file (e.g., /etc/sysconfig/network-scripts/route-eth0):
    10.0.0.0/24 via 192.168.1.2 dev eth0

    Restart the network:

    sudo systemctl restart network

  • NetworkManager (recommended): Use the nmcli command:
    sudo nmcli connection modify eth0 ipv4.gateway "192.168.1.1"  # Default gateway
    sudo nmcli connection modify eth0 +ipv4.routes "10.0.0.0/24 192.168.1.2"  # Specific route
    sudo nmcli connection up eth0

5. Other Common Operations

1. View Detailed Route Information

ip route get <target IP>  # View the specific routing path to a certain IP

Example: Check the route to access 8.8.8.8:

ip route get 8.8.8.8

2. Temporarily Add Policy Routing (Advanced)

Implement multiple routing tables (e.g., source IP-based routing) using ip rule and ip route:

# Create a custom routing table (e.g., table ID 100)
echo "100 custom_table" >> /etc/iproute2/rt_tables

# Add route to the custom table
ip route add default via 10.0.0.1 dev eth1 table custom_table

# Add rule (e.g., use custom table when source IP is 192.168.1.100)
ip rule add from 192.168.1.100 lookup custom_table

Summary

Operation Temporary Command (iproute2) Permanent Method
Add Default Gateway sudo ip route add default via <gateway IP> dev <interface> Edit /etc/netplan/ (Ubuntu) or /etc/sysconfig/network-scripts/ (RHEL)
Add Specific Network Route sudo ip route add <network>/<mask> via <gateway> dev <interface> Same as above, add the routes field in the configuration file
Delete Route sudo ip route del <network>/<mask> or default No action required (remove the corresponding entry in the permanent configuration)
View Route ip route show or ip route get <IP>

It is recommended to prioritize using the ip route command (more powerful and compatible with future versions than the traditional route command). It is advisable to back up the original files before modifying permanent configurations!

Leave a Comment