A linux command per day (53): route command

The route command of the 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 display more information
#p#page title #e#-n do not resolve names
-v display detailed processing information
-F display sending information
-C display routing cache
-f clear all gateway entry routing table.
-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.
#p#Page title#e#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 obtain 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
#p#Pagination title#e#Command:
route
route -n
Output:

Copy code
[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#p#分页标题#e#
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 code


Note :
The first line indicates that the address of the network where the host is located is 192.168.120.0. If the communication is 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
where Flags is the routing flag, marking the current network The state of the node.
#p#Page title#e#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 that this gateway is a router
R Reinstate Route, a route re-initialized using dynamic routing
D Dynamically, this route is dynamically written to
M Modified, this route is dynamically modified by the routing daemon or director
! Indicates that this route is currently closed

Remarks :
route -n (-n means no name resolution, list speed will be faster than route)

Example 2: Add gateway/set gateway#p#page title#e#
command:
route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
output:
copy code
[root@localhost ~]# route add -net 224.0.0.0 netmask 240.0.0.0 dev eth0
[root@localhost ~]# route
Kernel IP routing table
Destination Flags Metric Ref Genmask the Use Gateway 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 255.0.0.0 192.168.120.1 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 #p#page title#e#
copy code
[root@localhost ~]#
Description:
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
Output:

Copy code
[root@localhost ~]##p #Pagination title#e# 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 code


Description :
Add a shielded route, #p#page title#e#The destination address is 224.xxx will be rejected

Example 4: Delete routing records
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
Output:

Copy code
[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#p#分页标题#e#
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#p#分页标题#e#
[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 code


Description :

Example 5: Delete and add setting default gateway
Command :
#p#Page title#e#route del default gw 192.168.120.240
route add default gw 192.168.120.240
output:

copy code
[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#p#Pagination title#e#
Destination Gateway Genmask Ref Iface the Use Metric Flags
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 255.0.0.0 192.168.120.1 UG 0 0 0 eth0
default 192.168.120.240 0.0 .0.0 UG 0 0 0 eth0
[root@localhost ~] #Copy
code


Reproduced in: http://www.itxuexiwang.com/a/liunxjishu/2016/0304/214.html?1457194241

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326771635&siteId=291194637