Display and set static routing tables in Linux.
The route command is used to display and set the network routing table in the Linux kernel. The routes set by the route command are primarily static routes. To achieve communication between two different subnets, a router connecting the two networks or a gateway located in both networks is required.
Setting routes in a Linux system is usually to solve the following problem: if the Linux system is in a local area network (LAN) with a gateway that allows access to the Internet, the IP address of this gateway needs to be set as the default route for the Linux machine. Note that adding a route directly from the command line using the route command will not be saved permanently; the route will be lost after the network card or the machine restarts. You can add the route command to /etc/rc.local to ensure the route setting remains valid permanently.
route(options)(parameters)
-A: Sets the address type.
-C: Prints the routing cache of the Linux kernel.
-v: Verbose mode.
-n: Do not perform DNS reverse lookups; display IP addresses directly in numerical form.
-e: Displays the routing table in netstat format.
-net: Routing table to a network.
-host: Routing table to a host.
add: Adds the specified routing record.
del: Deletes the specified routing record.
target: Destination network or host.
gw: Sets the default gateway.
mss: Sets the TCP Maximum Segment Size (MSS) in bytes.
window: Specifies the TCP window size for TCP connections through the routing table.
dev: The network interface represented by the routing record.
Display the current routing table:
[root@localhost ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
112.124.12.0 * 255.255.252.0 U 0 0 0 eth1
10.160.0.0 * 255.255.240.0 U 0 0 0 eth0
192.168.0.0 10.160.15.247 255.255.0.0 UG 0 0 0 eth0
172.16.0.0 10.160.15.247 255.240.0.0 UG 0 0 0 eth0
10.0.0.0 10.160.15.247 255.0.0.0 UG 0 0 0 eth0
default 112.124.15.247 0.0.0.0 UG 0 0 0 eth1
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
112.124.12.0 0.0.0.0 255.255.252.0 U 0 0 0 eth1
10.160.0.0 0.0.0.0 255.255.240.0 U 0 0 0 eth0
192.168.0.0 10.160.15.247 255.255.0.0 UG 0 0 0 eth0
172.16.0.0 10.160.15.247 255.240.0.0 UG 0 0 0 eth0
10.0.0.0 10.160.15.247 255.0.0.0 UG 0 0 0 eth0
0.0.0.0 112.124.15.247 0.0.0.0 UG 0 0 0 eth1
The Flags column explains the status of the network node:
Add a network route/Set a gateway:
route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0 # Add a route to 224.0.0.0.
Block a route:
route add -net 224.0.0.0 netmask 240.0.0.0 reject # Add a blocked route; destination 224.x.x.x will be rejected.
Delete a routing record:
route del -net 224.0.0.0 netmask 240.0.0.0
route del -net 224.0.0.0 netmask 240.0.0.0 reject
Delete and add a default gateway:
route del default gw 192.168.120.240
route add default gw 192.168.120.240