Route
Function Description: The route command in linux can be used to display and operate IP route tables. It is mainly used to create a static route for a specified host or network to pass through a network interface, such as eth0. When the "add" or "del" parameter is used, the route table is modified. If no parameter exists, the current content of the route table is displayed. In a network, a vro is required to forward data between different broadcast domains or data between the lan and the internet. Sometimes we need to set this router as the default route for the linux system, then we can use the route command to operate. We can even use our linux system as a router.
Note that you can directly execute the route command on the command line to add a route. The route will not be permanently saved. When the NIC is restarted or the machine is restarted, the route will become invalid; you can go to/etc/rc. add the route command to local to ensure that the route settings are permanently valid. Of course, if the-p parameter is added, it will take effect permanently.
Command syntax: route [-f] [-p] [Command [Destination] [mask Netmask] [Gateway] [metric Metric] [if Interface]
Command parameters:
-C show more information
-N does not parse the name
-V: displays detailed processing information.
-F: display the sending information
-C: Display route Cache
-F clears the route tables of all gateway entries.
-P makes the route permanent when used together with the add command.
Add: add a new ingress.
Del: delete a route.
-Net: the destination address is a network.
-Host: The target address is a host.
Netmask: when adding a network route, you need to use the network mask.
Gw: Route data packets through the gateway. Note that the gateway you specified must be accessible.
Metric: set the number of Route hops.
Command specifies the Command you want to run (Add/Change/Delete/Print ).
Destination specifies the network Destination of the route.
Mask Netmask specifies the network mask (also known as the subnet mask) related to the network target ).
Gateway specifies the forward or next hop IP addresses that the network target defines and the subnet mask can reach.
Metric Metric specifies an integer cost value mark (from 1 to 9999) for the route, when the destination address of the forwarded packet is the most matched in the route table) you can use it when selecting multiple routes.
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. It can be in decimal or hexadecimal notation.
(1) display route information
[Root @ LiWenTong ~] # Route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.40.0*255.255.252.0 U 0 0 0 eth0
169.254.0.0*255.255.0.0 U 0 0 0 eth0
Default 192.168.40.1 0.0.0.0 UG 0 0 0 eth0
FlagsDescription:
U Up indicates that the route is currently in the starting status.
H Host, indicating that the gateway is a Host
G Gateway indicates that this Gateway is a router.
R Reinstate Route: The Route reinitialized using dynamic routes
D Dynamically, when will dynamic routing information be generated when this route is written Dynamically?
M Modified. The route is dynamically Modified by the routing daemon or the Guide.
(2) Add a route entry pointing to a network
[Root @ LiWenTong ~] # Route add-net 10.0.0.0 netmask route 255.255.0 dev eth0-This specifies the route egress location. Green: the IP address, address range, and subnet mask are used to determine the network range.
[Root @ LiWenTong ~] # Route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.0.0.0*255.255.255.0 U 0 0 0 eth0
192.168.40.0*255.255.252.0 U 0 0 0 eth0
169.254.0.0*255.255.0.0 U 0 0 0 eth0
Default 192.168.40.1 0.0.0.0 UG 0 0 0 eth0
Self-Summary: to add a route, you must specify the target network and the route entry parameters. Remember to add the-p option permanently.
(3) add another route entry to an ip address.
[Root @ LiWenTong ~] # Route add-host 192.168.40.1dev eth0
[Root @ LiWenTong ~] # Route ---- if you can find that the host is added, by default, we will add a full 255 subnet mask, indicating that there is only one subnet range, that is, this host.
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.40.1*255.255.255.255 UH 0 0 0 eth0
(4) shielding a route provider
When we do not allow the system to reach a certain Subnet or a host, we can manually block it.
[Root @ LiWenTong ~] # Route add-net 10.10.10.128 netmask contains route 0000128 reject-the previous part is the same, because we manually add a route. The difference is that at the end of the command, the specified exit is rejected, that is, the exit is rejected. The blocking effect is achieved. Also, check that flags will display one!
[Root @ LiWenTong ~] # Route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.40.1*255.255.255.255 UH 0 0 0 eth0
10.10.128-255.255.255.255.128! 0-0-
10.0.0.0*255.255.255.0 U 0 0 0 eth0
192.168.40.0*255.255.252.0 U 0 0 0 eth0
169.254.0.0*255.255.0.0 U 0 0 0 eth0
Default 192.168.40.1 0.0.0.0 UG 0 0 0 eth0
(5) delete a route
[Root @ LiWenTong ~] # Route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.40.1 * 255.255.255.255UH 0 0 0 eth0
10.10.128-255.255.255.255.128! 0-0-
10.0.0.0*255.255.255.0 U 0 0 0 eth0
192.168.40.0*255.255.252.0 U 0 0 0 eth0
169.254.0.0*255.255.0.0 U 0 0 0 eth0
Default 192.168.40.1 0.0.0.0 UG 0 0 0 eth0
[Root @ LiWenTong ~] # Route del-net 10.10.10.128netmask implements 255.255.128 reject
[Root @ LiWenTong ~] # Route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.40.1 * 255.255.255.255UH 0 0 0 eth0
10.0.0.0*255.255.255.0 U 0 0 0 eth0
192.168.40.0*255.255.252.0 U 0 0 0 eth0
169.254.0.0*255.255.0.0 U 0 0 0 eth0
Default 192.168.40.1 0.0.0.0 UG 0 0 0 eth0
Note: No, I have deleted the route. When deleting a route, it is best to look at the route table as usual, so that no error will be deleted.
(6) add and delete the Default Gateway
[Root @ LiWenTong ~] # Route add default gw 192.168.40.2
[Root @ LiWenTong ~] # Route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.40.1 * 255.255.255.255UH 0 0 0 eth0
10.0.0.0*255.255.255.0 U 0 0 0 eth0
192.168.40.0*255.255.252.0 U 0 0 0 eth0
169.254.0.0*255.255.0.0 U 0 0 0 eth0
Default 192.168.40.2 0.0.0.0 UG 0 0 0 eth0
Default 192.168.40.1 0.0.0.0 UG 0 0 0 eth0
-- We can see that there are two default gateways here. Which one will the route take?
[Root @ LiWenTong ~] # Route del default gw192.168.40.2 --- here we should delete it first.
[Root @ LiWenTong ~] # Route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.40.1 * 255.255.255.255UH 0 0 0 eth0
10.0.0.0*255.255.255.0 U 0 0 0 eth0
192.168.40.0*255.255.252.0 U 0 0 0 eth0
169.254.0.0*255.255.0.0 U 0 0 0 eth0
Default 192.168.40.1 0.0.0.0 UG 0 0 0 eth0
--------------------------- Subsequent self-Summary -------------------------
This route command is similar to the windows route command. It is mainly to view the route table and add a route. Add the default route and delete the route. There is also a blocked route reject.
Route add-net 192.168.40.0 netmask 255.255.255.0 dev eth0: 1
Route add-host 192.168.40.111 dev eth0 reject
Route del-net 192.168.40.0 netmask 255.255.255.0 dev eth0: 1
Route add default gw 192.169.1.1
In the future, I will check whether others have other commonly used environments on the Internet. Everyone knows how to help me stay here!