Linux route commands to understand and use examples (view and manipulate IP routing tables)

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

Command:

Copy CodeThe code is as follows:
Route
Route-n

Output:

Copy CodeThe code is as follows:
[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
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.

Flags flags indicate:

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 a gateway/set up a gateway

Command: Route add-net 224.0.0.0 netmask 240.0.0.0 Dev eth0

Output:

Copy CodeThe code is as follows:
[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

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:

Copy CodeThe code is as follows:
[Email protected] ~]# 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 shielded route with the destination address of 224.x.x.x to be rejected

Example 4: Delete a route record

Command:

Copy CodeThe code is as follows:
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:

Copy CodeThe code is as follows:
[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
[Email protected] ~]# Route del-net 224.0.0.0 netmask 240.0.0.0
[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-
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
[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:

Copy CodeThe code is as follows:
Route del default GW 192.168.120.240
Route add default GW 192.168.120.240

Output:

Copy CodeThe code is as follows:
[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
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] ~]#

Linux route commands to understand and use examples (view and manipulate IP routing tables)

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.