TrumanWong

route

Display and set the static routing table in Linux

Supplementary instructions

route command is used to display and set the network routing table in the Linux kernel. The routes set by the route command are mainly static routes. To achieve communication between two different subnets, you need a router that connects the two networks, or a gateway that is located on both networks at the same time.

Setting routing in a Linux system is usually to solve the following problems: The Linux system is in a LAN, and there is a gateway in the LAN that allows the machine to access the Internet. Then you need to set the IP address of this machine as the default route of the Linux machine. . It should be noted that executing the route command directly on the command line to add a route will not be saved permanently. When the network card is restarted or the machine is restarted, the route will become invalid; you can add the route command in /etc/rc.local To ensure that the routing settings are permanently valid.

grammar

route(option)(parameter)

Options

-A: Set address type;
-C: Print the routing cache of the Linux core;
-v: detailed information mode;
-n: Do not perform DNS reverse lookup and directly display the IP address in numerical form;
-e: Display routing table in netstat format;
-net: routing table to a network;
-host: Routing table to a host.

Parameters

add: add the specified routing record;
del: Delete the specified routing record;
target: destination network or destination host;
gw: Set the default gateway;
mss: Set the maximum block length (MSS) of TCP, in MB;
window: Specifies the TCP window size for TCP connections through the routing table;
dev: The network interface represented by the routing record.

Example

Show current route:

[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

Among them, Flags is the routing flag, marking the status of the current network node. Flags flag description:

  • U Up indicates that this route is currently in the startup state.

  • H Host, indicating that this gateway is a host.

  • G Gateway, indicating that this gateway is a router.

  • R Reinstate Route, a route reinitialized using dynamic routing.

  • D Dynamically, this route is written dynamically.

  • M Modified, this route is dynamically modified by a routing daemon or director.

  • ! Indicates that this route is currently closed.

    Add gateway/set 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 shielded route. The destination address 224.x.x.x will be rejected.

Delete 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

Remove and add settings default gateway:

route del default gw 192.168.120.240
route add default gw 192.168.120.240