Linuxroute command details and examples (view and operate IP route tables)

Source: Internet
Author: User
The route command in Linux is used to display and operate the IP route table (show/manipulatetheIProutingtable ). To achieve communication between two different subnets, you need a router that connects two networks or a gateway that is located in both networks.

In Linux, routing is usually set to solve the following problems: the Linux system has a gateway in a LAN that allows machines to access the Internet, then you need to set the IP address of this machine to the default route of the Linux machine. 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.

1. command format:

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

2. command functions:

The Route command is used to operate the kernel-based ip Route table. 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.

3. 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. You can use decimal or hexadecimal values for interface indexing.

4. example:

Instance 1: displays the current route

Command:

Copy codeThe code is as follows:
Route
Route-n

Output:

 
Copy codeThe code is as follows:
[Root @ localhost ~] # 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
[Root @ localhost ~] # 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

Note:

The first line indicates that the network address of the host is 192.168.120.0. if the data transmission target is to communicate within the local area network, the data packet can be forwarded directly through eth0;

The fourth line indicates that the purpose of data transmission is to access the Internet, then the interface eth0 sends the data packet to the Gateway 192.168.120.240

Flags indicates the routing label and the status of the current network node.

Flags:

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, this route is written Dynamically

M Modified. the route is dynamically Modified by the routing daemon or the guide.

! Indicates that the route is currently disabled.

Note:

Route-n (-n indicates that the name is not parsed, and the list speed is faster than route)

Instance 2: Add a gateway or set a Gateway

Command: route add-net 224.0.0.0 netmask 240.0.0.0 dev eth0

Output:


Copy codeThe code is as follows:
[Root @ localhost ~] # Route add-net 224.0.0.0 netmask 240.0.0.0 dev eth0
[Root @ localhost ~] # 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

Note:

Add a route entry to 244.0.0.0

Instance 3: Shield a route

Command: route add-net 224.0.0.0 netmask 240.0.0.0 reject

Output:

 
Copy codeThe code is as follows:
[Root @ localhost ~] # Route add-net 224.0.0.0 netmask 240.0.0.0 reject
[Root @ localhost ~] # 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

Note:

Add a blocked Route. if the destination address is 224. x, the route is denied.

Instance 4: delete route records

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:
[Root @ localhost ~] # 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
[Root @ localhost ~] # Route del-net 224.0.0.0 netmask 240.0.0.0
[Root @ localhost ~] # 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
[Root @ localhost ~] # Route del-net 224.0.0.0 netmask 240.0.0.0 reject
[Root @ localhost ~] # 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
[Root @ localhost ~] #

Note:

Instance 5: delete and add default gateways

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:
[Root @ localhost ~] # Route del default gw 192.168.120.240
[Root @ localhost ~] # 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
[Root @ localhost ~] # Route add default gw 192.168.120.240
[Root @ localhost ~] # 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
[Root @ localhost ~] #

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.