Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux route command

Linux route command

Linux Command Reference Linux Command Reference


I. Overview of the route command

The route command is a powerful tool in Linux for viewing and manipulating the IP routing table. As a fundamental network configuration command, it allows administrators to:

Basic Syntax

route [选项] [操作] [目标] [命令参数]


II. Basic concepts of the routing table

1. What is a routing table

A routing table is a data structure maintained by the operating system kernel that determines how network packets travel from a source address to a destination address. It can be compared to:

"A city traffic navigation system: when you want to go somewhere, the system tells you which route is the fastest and most efficient"

2. Key fields of the routing table

Pass route -n When you view the routing table, the following important columns are displayed:

Column Name Description Example value
Destination destination network or host 192.168.1.0
Gateway next-hop gateway 192.168.1.1
Genmask network mask 255.255.255.0
Flags routing flags U (route is up)
Metric route cost 0
Iface outgoing interface eth0

III. Common operations of the route command

1. View the routing table

Example

display the full routing table (without resolving hostnames)

route

-n

Example output:

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

2. Add a route

Example

add a route to a specific host

route add

-host

192.168.1.100 dev eth0

add a route to a network

route add

-net

192.168.2.0 netmask 255.255.255.0 gw 192.168.1.1

add a default gateway

route add default gw 192.168.1.1

3. Delete a route

Example

delete a specific route

route del

-net

192.168.2.0 netmask 255.255.255.0

delete the default gateway

route del default gw 192.168.1.1


IV. Detailed command options

The route command supports the following common options:

Option Description
-n Show addresses in numeric form (do not resolve hostnames)
-e Display the routing table in netstat format
-v Display detailed operation information
-A family Specify the address family (inet, inet6, etc.)
-F Display the kernel FIB routing table
-C Show the routing cache

Special command parameters:


V. Practical application scenarios

Scenario 1: Multi-NIC routing configuration

When a server has multiple network interfaces, you need to specify the egress for different networks:

Example

Route traffic destined for 10.0.0.0/24 through the eth1 interface

route add

-net

10.0.0.0 netmask 255.255.255.0 dev eth1

Scenario 2: VPN routing setup

After connecting to a VPN, you need to add specific routes:

Example

Only let specific network segments go through the VPN tunnel

route add

-net

172.16.0.0 netmask 255.240.0.0 dev tun0

Scenario 3: Temporary test routes

Example

Add a route temporarily for testing (lost after reboot)

route add

-host

8.8.8.8 gw 192.168.1.254

ping

8.8.8.8  

Test whether the route takes effect

route del

-host

8.8.8.8  

Delete it after testing is complete


6. Persisting route configuration

Routes added via the route command are lost after a reboot. To make them permanent:

1. Debian/Ubuntu systems

Editing /etc/network/interfaces:

Example

auto eth0

iface eth0 inet static

    address 192.168.1.100

    netmask 255.255.255.0

    gateway 192.168.1.1

    up route add

-net

10.0.0.0 netmask 255.0.0.0 gw 192.168.1.2

2. RHEL/CentOS systems

Create the Router Configuration File /etc/sysconfig/network-scripts/route-eth0:

10.0.0.0/8 via 192.168.1.2


7. Common troubleshooting

1. Steps to check why a route is not taking effect

2. Resolving typical errors

Error 1:"SIOCADDRT: Network is unreachable"

Error 2:"RTNETLINK answers: File exists"


8. Alternative command: ip route

Modern Linux systems recommend usingip routeCommand:

Example

Display the routing table

ip route

show

Add a route

ip route

add 10.0.0.0

/

8

via 192.168.1.2

Delete a route

ip route

del 10.0.0.0

/

8

Althoughip routemore powerful, butroutethe command is still widely used because it is simple and intuitive.


Through this guide, you should have mastered the core usage of the route command. It is recommended to practice these commands in a real environment and observe how routing changes affect network connectivity. When encountering complex routing problems, you can combinetracerouteandpingand other tools for comprehensive diagnosis.

Linux Command Reference Linux Command Reference