Detailed route command in CentOS _linux

Source: Internet
Author: User

Introduced

The route command in the Linux system can be used for the display and operation of IP routing tables. Its primary role is to create a static route that specifies a host or network through a network interface, such as eth0. When you use the "add" or "del" parameters, the routing table is modified, and if there are no parameters, the current contents of the routing table are displayed. In a network, a router is needed to forward data between different broadcast domains, or to forward data between the LAN and the Internet. Sometimes we need to set this router as the default route for Linux, so we can operate it through the route command. Even we can use our Linux system to act as a router.

It is important to note that the route is added directly to the command line to add the route, does not persist, and is invalidated when the network card is rebooted or the machine is rebooted, and the route command can be added to the/etc/rc.local to ensure that the routing is permanently valid. \ n \ Route Of course, if you add the-p parameter, it will be permanently effective.

Command format

Route [F] [P] [Command [Destination] [mask Netmask] [gateway][metric metric]] [if Interface]]

Command Arguments

-C Display more information

-N does not resolve names

-V Display detailed processing information

-F Display Send message

-C Display Routing cache

-F Clears the routing table for all gateway portals.

-P makes the route permanent when used with the add command.

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: Routing packets through gateways. Note that the gateway you specify must be able to reach.

Metric: Sets the route hop count.

1. command specifies the commands you want to run (add/change/delete/print).

2, destination specifies the network destination for this route.

3. Mask Netmask Specifies the netmask (also known as the subnet mask) associated with the network destination.

4. Gateway specifies the forward or next-hop IP address that the address set and subnet mask of the network target definition can reach.

5. Metric metric Specifies an integer cost value (from 1 to 9999) for the route, which is available when you select multiple routes in the routing table (which matches the destination address of the forwarding packet).

6, if interface specifies an interface index for an interface that can access the target. To get a list of interfaces and their corresponding interface indexes, use the display feature of the route Print command. You can use decimal or 16 in

Instance

1 Displaying 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 Flags Description

    1. U up indicates that this route is currently in the starting state
    2. H host, which indicates that this gateway is a host
    3. G Gateway, which indicates that this gateway is a router
    4. R reinstate Route, using dynamic routing to reinitialize a route
    5. D dynamically, this route is dynamically written-"When will there be dynamic routing information?"
    6. M Modified, this route is dynamically modified by the routing daemon or the guide

2 Add a route to a network

[root@localhost~]# Route add-net 10.0.0.0 netmask 255.255.255.0 Dev eth0

This is where the exit for this route is specified. -net 10.0.0.0 netmask 255.255.255.0 A parameter for the specified destination network, an IP address or address range, subnet mask is required to determine the network scope.

[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 to add a route is to specify the target network, and route the export of these two parameters. Remember that adding the-p option can be added permanently.

3 routes added to an IP

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

Can be found that the addition of the host, the default is to help us add a full 255 subnet mask, indicating subnet range is only one, 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

4 Masking a route

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

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

The previous part is the same, because we are all manually to add a route. Just at the end of the order, our designated export goes to reject (refusal), which means refusing to export. To achieve shielding effect. And look at the flags will show one!

[root@localhost~]# 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.10.128-  255.255.255.128! 0-0-10.0.0.0
*  255.255.255.0 U 0 0 0 et H0
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.1 68.40.1 0.0.0.0  UG 0 0 0 eth0

5 removing routes

[root@localhost~]# 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.10.128-  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 19 2.168.40.1 0.0.0.0  UG 0 0 0 eth0
[root@localhost~]# Route del-net 10.10.10.128netmask 255.255.255.128 Reject
[root@localhost~]# 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: When you delete a route, it is best to look at the same route in the routing table, so that the comparison will not be deleted incorrectly.

Add Delete default gateway

[root@localhost ~]# route add default GW 192.168.40.2

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

You can see that there are two default gateways, so which way does the route go?

[root@localhost~]# Route del default gw192.168.40.2
[root@localhost~]# 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
   

Summarize

The above is the entire content of this article, I hope the content of this article for everyone's study or work can help, if there is doubt you can message exchange.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.