Linux route command Details: View and manipulate IP routing table

Source: Internet
Author: User

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

-N does not parse name

-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. Metric: Sets the route hop count.

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

Route or Route-n

[Email protected]:~# Route-nkernel IP Routing tabledestination Gateway genmask Flags Metric Ref use Iface0.0.0.0         59.188.252.1    0.0.0.0Ug -    0        0br010.22.0.0       0.0.0.0         255.255.255.0U0      0        0br059.188.252.0    0.0.0.0         255.255.255.0U0      0        0br0172.22.0.0      0.0.0.0         255.255.255.0U0      0        0BR1192.168.122.0   0.0.0.0         255.255.255.0U0      0        0Virbr0

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.

Flags flags indicate:

U up indicates that this route is currently a boot state H host, indicating that the gateway is a host

G Gateway, which indicates that this gateway is a router R reinstate route that uses dynamic routing to reinitialize the route

D dynamically, this route is dynamically written to M Modified, which 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 a gateway/set up a gateway
Command:
Route add-net 224.0. 0.0 netmask 240.0. 0.0 Dev eth0


Example 3: Masking a route
Command:
Route add-net 224.0. 0.0 netmask 240.0. 0.0 Reject

[Email protected] ~]# Route add-net224.0.0.0Netmask240.0.0.0Reject[[email protected]~]# routekernel IP routing tabledestination Gateway genmask Flags Metric Ref use Iface192.168.120.0*255.255.255.0U0 0 0eth0192.168.0.0 192.168.120.1 255.255.0.0Ug0 0 0eth010.0.0.0 192.168.120.1 255.0.0.0Ug0 0 0eth0224.0.0.0-240.0.0.0!0-0-224.0.0.0*240.0.0.0U0 0 0Eth0default192.168.120.240 0.0.0.0Ug0 0 0Eth0

Description
Add a shielded route with the destination address of 224.x.x.x to be rejected

Example 4: Delete a route record
Command:

224.0. 0.0 240.0. 0.0  224.0. 0.0 240.0. 0.0 Reject


Example 5: Delete and add set default gateway
Command:

192.168. 120.240  192.168. 120.240

[[email protected] ~]# route del default GW192.168.120.240[[Email protected]~]# routekernel IP routing tabledestination Gateway genmask Flags Metric Ref use Iface192.168.120.0*255.255.255.0U0 0 0eth0192.168.0.0 192.168.120.1 255.255.0.0Ug0 0 0eth010.0.0.0 192.168.120.1 255.0.0.0Ug0 0 0Eth0[[email protected]~]# route Add default GW192.168.120.240[[Email protected]~]# routekernel IP routing tabledestination Gateway genmask Flags Metric Ref use Iface192.168.120.0*255.255.255.0U0 0 0eth0192.168.0.0 192.168.120.1 255.255.0.0Ug0 0 0eth010.0.0.0 192.168.120.1 255.0.0.0Ug0 0 0Eth0default192.168.120.240 0.0.0.0Ug0 0 0Eth0

Linux route command Details: View and manipulate IP routing table

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.