Linux common commands: route command


  The route command of Linux system is used to display and manipulate the IP routing table (show / manipulate the IP routing table). To implement communication between two different subnets, a router connecting the two networks, or a gateway located in both networks is required. In a Linux system, routing is usually set to solve the following problems: the Linux system is in a local area network, and there is a gateway in the local area network, which can allow the machine to access the Internet, then you need to set the IP address of the machine as the default of the Linux machine. routing. It should be noted that executing the route command directly on the command line to add a route will not be permanently saved. When the network card is restarted or the machine is restarted, the route will be invalid; you can add the route command in /etc/rc.local to ensure This routing setting is permanent.

1. Command format:

  route [-f] [-p] [Command [Destination] [mask Netmask] [Gateway] [metric Metric]] [if Interface]] 

2. Command function:

  The Route command is used to operate the kernel-based ip routing table. Its main function is to create a static route to specify a host or a network through a network interface, such as eth0. When the "add" or "del" parameter is used, the routing table is modified. If there is no parameter, the current content of the routing table is displayed.

3. Command parameters:

-c show more information

-n do not resolve names

-v display detailed processing information

-F show send information

-C show route cache

-f Clears the routing table for all gateway entries. 

-p Makes the route permanent when used with the add command.

 

add: Add a new route.

del: delete a route.

-net: The destination address is a network.

-host: The target address is a host.

netmask: When adding a network route, the netmask is required.

gw: Routing packets through a gateway. Note that the gateway you specify must be reachable.

metric: Set the number of route hops.

Command Specifies the command you want to run (Add/Change/Delete/Print). 

Destination Specifies the network destination for this route. 

mask Netmask Specifies the netmask (also known as the subnet mask) associated with the network target. 

Gateway Specifies the forward or next hop IP address that can be reached by the address set and subnet mask defined by the network destination. 

metric Metric specifies an integer cost value metric (from 1 to 9999) for the route, which can be used when choosing among multiple routes in the routing table (that best matches the destination address of the forwarded packet). 

if Interface specifies the interface index for the interface that can access the target. To get a list of interfaces and their corresponding interface indices, use the display function of the route print command. Interface indexing can be done using decimal or hexadecimal values.

4. Example of use:

Example 1: Display the current route

Order:

route

route -n

output:

[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
e192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
0.0.0.0 192.168.120.240 0.0.0.0 UG 0 0 0 eth0

illustrate:

The first line indicates that the address of the network where the host is located is 192.168.120.0. If the data transfer target is to communicate within the local area network, the data packet can be forwarded directly through eth0;

The fourth line indicates that the purpose of data transmission is to access the Internet, then the interface eth0 will send the data packet to the gateway 192.168.120.240

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

Description of Flags:

U Up indicates that the route is currently up

H Host, indicating that this gateway is a host

G Gateway, indicating that this gateway is a router

R Reinstate Route, a route reinitialized with dynamic routing

D Dynamically, this route is written dynamically

M Modified, this route is dynamically modified by the routing daemon or director

! Indicates that this route is currently closed

Remark:

route -n (-n means no name resolution, listing speed will be faster than route)

 

Example 2: Add gateway/set gateway

Order:

  route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0

output:

[root@localhost ~]# route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
224.0.0.0       *               240.0.0.0       U     0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]#   

illustrate:

  Add a route to 244.0.0.0

 

Example 3: Block a route

Order:

  route add -net 224.0.0.0 netmask 240.0.0.0 reject

output:

[root@localhost ~]# route add -net 224.0.0.0 netmask 240.0.0.0 reject
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
224.0.0.0       -               240.0.0.0       !     0      -        0 -
224.0.0.0       *               240.0.0.0       U     0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0

illustrate:

  Add a shielded route, the destination address is 224.xxx will be rejected

Example 4: Delete routing records

Order:

  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

output:

[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
224.0.0.0       -               240.0.0.0       !     0      -        0 -
224.0.0.0       *               240.0.0.0       U     0      0        0 eth0
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]# route del -net 224.0.0.0 netmask 240.0.0.0
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
224.0.0.0       -               240.0.0.0       !     0      -        0 -
default         192.168.120.240 0.0.0.0         UG    0      0        0 eth0
[root@localhost ~]# route del -net 224.0.0.0 netmask 240.0.0.0 reject
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0 * 255.255.255.0 U 0 0 0 eth0
192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0
[root@localhost ~]# 

illustrate:

 

Example 5: Delete and add setting default gateway

Order:

route del default gw 192.168.120.240

route add default gw 192.168.120.240

output:

[root@localhost ~]# route del default gw 192.168.120.240
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0   *               255.255.255.0   U     0      0        0 eth0
192.168.0.0     192.168.120.1   255.255.0.0     UG    0      0        0 eth0
10.0.0.0        192.168.120.1   255.0.0.0       UG    0      0        0 eth0
[root@localhost ~]# route add default gw 192.168.120.240
[root@localhost ~]# route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.120.0 * 255.255.255.0 U 0 0 0 eth0
192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
10.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0
[root@localhost ~]#

illustrate:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325194632&siteId=291194637