Home > Operation and Maintenance > Linux Operation and Maintenance > Detailed explanation of route command

Detailed explanation of route command

藏色散人
Release: 2020-03-03 09:25:26
Original
12716 people have browsed it

Detailed explanation of route command

Detailed explanation of 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 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. In a Linux system, routing is usually set up 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, so the IP address of the machine needs to be Set as the default route for Linux machines. 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 This routing setting is permanent.

Recommended: "Linux Tutorial"

1. Command format:

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

2. Command function:

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 are no parameters, the current contents of the routing table are displayed.

3. Command parameters:

-c Display more information

-n Do not resolve the name

-v Display detailed processing information

-F Display sending information

-C Show route cache

-f Clear the routing table of 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 target address is a network.

-host: The target address is a host.

netmask: When adding a network route, a network mask needs to be used.

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

metric: Set the routing hop count.

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

Destination specifies the network destination of this route.

mask Netmask Specifies the network mask (also called a 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 for the route (from 1 to 9999), which can be used when selecting 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 obtain a list of interfaces and their corresponding interface indexes, use the display function of the route print command. Interface indexing can be done using decimal or hexadecimal values.

4. Usage example:

Example 1: Display the current route

Command:

route
route -n
Copy after login

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
Copy after login

Description:

First line Indicates that the address of the network where the host is located is 192.168.120.0. If the data transmission target is to communicate within this LAN, the data packet can be forwarded directly through eth0;

The fourth line indicates that the data transmission purpose is to access the Internet, then Interface eth0 sends the data packet to the gateway 192.168.120.240

where Flags is the routing flag, marking the status of the current network node.

Flags flag description:

U Up means that this route is currently in the startup state

H Host, means that this gateway is a host

G Gateway, means This gateway is a router

R Reinstate Route, a route reinitialized using dynamic routing

D Dynamically, this route is dynamically written

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

! Indicates that this route is currently closed

Remarks:

route -n (-n Indicates that the name will not be parsed, and the listing speed will be faster than route)

Example 2: Add gateway/set gateway

Command:

route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
Copy after login

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 ~]#
Copy after login

Instructions:

Add a route to 244.0.0.0

Example 3: Block a route

Command:

route add -net 224.0.0.0 netmask 240.0.0.0 reject
Copy after login

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
Copy after login

Description:

Add a shielded route with the destination address 224.x.x.x and it will be rejected

Example 4: Delete routing record

Command:

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
Copy after login

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 ~]#
Copy after login

Description:

Example 5: Delete and add Set the default gateway

Command:

route del default gw 192.168.120.240
route add default gw 192.168.120.240
Copy after login

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 ~]#
Copy after login

The above is the detailed content of Detailed explanation of route command. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template