Detail the route command in CentOS

  • 2020-05-14 05:52:37
  • OfStack

introduce

The route command on the Linux system can be used to display and manipulate the 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" parameters are used, the routing table is modified, and if there are no parameters, the current contents of the routing table are displayed. In a network, one router is required to forward data between different broadcast domains or between lan and internet. Sometimes we need to set this router as the default route for the linux system, so we can use the route command. We can even use our linux system as a router.

It is important to note that the route command is executed directly from the command line to add routes. It will not be saved permanently. When the network card is restarted or the machine is restarted, the route will be invalid. You can add the route command to /etc/ rc.local to make this routing setting permanent. Of course, if the -p parameter is added, it will be permanently effective.

The command format


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

The command parameter

-c shows more information

-n does not resolve names

-v displays detailed processing information

-F display send message

-C shows the route cache

-f clears the routing table for all gateway entries.

-p makes routing permanent when used with add command 1.

add: add a new route.

del: delete 1 route.

-net: the destination address is 1 network.

-host: the target address is 1 host.

netmask: the network mask is used when adding a network route.

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

metric: sets the number of routing hops.

1, Command specify you want to run the command (Add/Change/Delete/Print).

2. Destination specifies the network target for this route.

3. mask Netmask specifies the network mask (also known as a subnet mask) associated with the network target.

4. Gateway specifies the address set defined by the network target and the address of IP at the forward or down 1 hop that can be reached by the subnet mask.

5. metric Metric specifies an integer cost value label (from 1 to 9999) for routing, which can be used when selecting among multiple routes in the routing table (which most closely matches the destination address of the packet being forwarded).

6. if Interface specifies the interface index for the interface to which the target can be accessed. To get a list of interfaces and their corresponding interface indexes, use the display capability of the route print command. You can use base 10 or 106

The instance

Display routing information


[root@localhost~]# 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

Flags flag description

U Up means that the path is currently enabled H Host, indicating that this gateway is the 1 host G Gateway, indicating that this gateway is a 1 router R Reinstate Route, re-initialized routing using dynamic routing D Dynamically, this path is dynamically written to the copyright. When will there be dynamic routing information? M Modified, which is dynamically modified by a routing daemon or guide

2 add a route to a network


[root@localhost~]# route add -net 10.0.0.0 netmask 255.255.255.0 dev eth0

Here is the exit that specifies this route. -net 10.0.0.0 netmask 255.255.255.0 as a parameter of the specified target network, the ip address or address range and subnet mask are required to determine the network range.


[root@localhost~]# 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

route adds routes by specifying two parameters: the target network and the route exit. Remember to add the -p option permanently.

3 route added to a certain ip


[root@localhost~]# route add -host 192.168.40.1dev eth0
[root@localhost ~]# route

It can be found that if the host is added, the default will help us to add a full 255 subnet mask, which means that the subnet scope is only 1, that is the 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

Block a route

When we do not allow the system to reach a certain subnet range or a certain host is can be manually shielded.


[root@localhost~]# route add -net 10.10.10.128 netmask 255.255.255.128 reject

The previous part is like 1, because we manually add 1 route. Only at the end of the command, instead of 1, we specify the exit to reject (reject), which means reject the exit. To achieve the shielding effect. And if you look at flags it will show 1!


[root@localhost~]# route

[root@localhost~]# route
0

5 delete route


[root@localhost~]# route
1

[root@localhost~]# route
2

[root@localhost~]# route del -net 10.10.10.128netmask 255.255.255.128 reject

[root@localhost~]# route
4

When deleting the route, it is best to look at the route table on the same type in, so the comparison will not delete the wrong.

Add and remove default gateways


[root@localhost~]# route
5

You can see there are two default gateways, so which one will the route go to?


[root@localhost~]# route
6

[root@localhost~]# route
7

conclusion

The above is the whole content of this article, I hope the content of this article can help you in your study or work, if you have any questions, you can leave a message to communicate.


Related articles: