The route command for the 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 is connected to two networks, or a gateway to two networks. In Linux systems, routing is usually done to address the problem that the Linux system has a gateway in a local area network that allows the machine to access the Internet, so the IP address of the machine needs to be set to the default route of the Linux machine. Note that the route command is executed directly at the command line to add the route, is not persisted, and is invalidated when the NIC is restarted or the machine restarts, and the route command can be added to the/etc/rc.local to ensure that the 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 for operations based on the kernel IP routing table, and its primary function is to create a static route that lets you 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, and if there are no parameters, the current contents of the routing table are displayed.
3. Command parameters:
-C Show more information
#p # pagination Title #e#-n name not resolved
-V displays detailed processing information
-F Display Send message
-C Show Route cache
-F Clears the routing table for all gateway portals.
-P is used with the add command to make the route permanent.
Add: Adds a new route.
Del: Deletes a route.
-net: The destination address is a network.
-host: The destination address is a host.
Netmask: When you add a network route, you need to use a netmask.
GW: Route packets through the gateway. Note that the gateway you specify must be able to be reached.
#p # page Title #e#metric: Sets the number of route hops.
command to specify the commands you want to run (add/change/delete/print).
DESTINATION Specifies the network destination for this route.
The mask Netmask Specifies the network mask (also known as the subnet mask) that is associated with the network destination.
The Gateway specifies the address set and subnet mask defined by the network destination to reach the forward or next hop IP address.
The metric metric specifies an integer cost value for the route (from 1 to 9999) that can be used when the routing table (which matches the destination address of the forwarded packet) is selected in multiple routes.
The 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. You can use decimal or hexadecimal values for interface indexing.
4. Usage examples:
Example 1: Displaying the current route
#p # Paging Header #e# command:
Route
Route-n
Output:
[Email protected] ~]# 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
[[email protected] ~]# route-n#p# page title #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
Description
The first line indicates that the host network address is 192.168.120.0, if the data transmission target is in the local area network communication, you can forward the packet directly through the eth0;
Row four indicates that the data transfer purpose is to access the Internet, then the interface eth0 to send the packet to the gateway 192.168.120.240
Where flags is the route flag that marks the state of the current network node.
#p # Page Title #e#flags flag Description:
U up indicates that this route is currently in the boot state
H host, which indicates that the gateway is a host
G Gateway, which indicates that this gateway is a router
R reinstate route, re-initialized with dynamic routing
D dynamically, this route is dynamically written to
M Modified, this route is dynamically modified by the route daemon or director
! Indicates that this route is currently in a closed state
Note:
Route-n (-n means not resolving names, listing faster than route)
Example 2: Add Gateway/Set Gateway #p# page Header #e#
Command:
Route add-net 224.0.0.0 netmask 240.0.0.0 Dev eth0
Output:
[Email protected] ~]# Route add-net 224.0.0.0 netmask 240.0.0.0 Dev eth0
[Email protected] ~]# 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 #p # pagination Title #e#
[Email protected] ~]#
Description
Add a route to 244.0.0.0
Example 3: Masking a route
Command:
Route add-net 224.0.0.0 netmask 240.0.0.0 Reject
Output:
[Email protected] ~]# #p # pagination title #e# Route add-net 224.0.0.0 netmask 240.0.0.0 Reject
[Email protected] ~]# 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
Description
Add a blocked route, #p # Pagination header #e# Destination address for 224.x.x.x will be rejected
Example 4: Delete a route 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
Output:
[Email protected] ~]# 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# pagination title #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
[Email protected] ~]# Route del-net 224.0.0.0 netmask 240.0.0.0
[Email protected]alhost ~]# 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
[[email protected] ~]# Route del-net 224.0.0.0 netmask 240.0.0.0 reject#p# page title #e#
[Email protected] ~]# 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
[Email protected] ~]#
Description
Example 5: Delete and add set default gateway
Command:
#p # Pagination title #e#route del default GW 192.168.120.240
Route add default GW 192.168.120.240
Output:
[Email protected] ~]# route del default GW 192.168.120.240
[Email protected] ~]# 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
[[email protected] ~]# route add default GW 192.168.120.240
[Email protected] ~]# route
Kernel IP Routing table#p# paging title #e#
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
[Email protected] ~]#
Reproduced in: http://www.itxuexiwang.com/a/liunxjishu/2016/0304/214.html?1457194241
One Linux command per day: Route command