Linux uses the route command to display and operate IP Route tables

Source: Internet
Author: User
ArticleDirectory
    • Example 1 print the current route table (Root User)
    • Example 2 print the current route table (non-Root User)

The route command is used to display and operate the IP route table (show/manipulate the IP routing
Table ). 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
In order to solve the following problem: the Linux system has a gateway in a LAN to allow machines to access the Internet, you need to set the IP address of this machine
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. After the NIC is restarted or the machine is restarted, the route will become invalid.
Add the route command to/etc/rc. Local to ensure that the route settings are permanently valid. This is verified in the example in this article.

Common Parameters

Format: Route

Format:/sbin/route

Displays the current routing table ).

When using a non-root user, you must use the full path to execute the route command. TP-link g install NIC Driver on fedora Guide

Format: route-n

Format:/sbin/route-n

Used to print the route table. The-N parameter is used to print the IP address directly without printing the host name in the output information. The netstat command also has this parameter.

Format: Route add default GW {IP-ADDRESS} {interface-name}

Used to set the default route (adds a default route, which will be used if no other route matches), where,

Parameter {IP-ADDRESS): Specifies the IP address of the router (GATEWAY) (specify router IP address );
Parameter {interface-name}: Specifies the interface name, for example, eth0 (specify interface name such as eth0 ). Use/sbin/ifconfig-a to display all interface information.

Man route writes route add default GW mango-GW
Adds a default route (which will be used if no other route matches). All packets using this route will
Be gatewayed through "mango-GW". The device which will be actually be used for that route depends on how we
Can reach "mango-GW"-the static route to "mango-GW" will have to be set up before.

 

Format: Route add-net {network-address} netmask {netmask} Dev {interface-name}

The routing rules added to the specified network.

Parameter {network-address}: used to specify the network address

Parameter {netmask}: used to specify the subnet mask

Parameter {interface-name}: Specifies the interface name, for example, eth0.

Man route writes route add-net 192.56.76.0 netmask limit 255.0 Dev eth0
Adds a route to the network 192.56.76.x via "eth0". The class C netmask modifier is not really necessary
Here because 192. * is a class c ip address. The word "Dev" can be omitted here.

Route add-net 224.0.0.0 netmask 240.0.0.0 Dev eth0
This is an obscure one defined ented so people know how to do it. This sets all of the Class D (Multicast)
IP routes to go via "eth0". This is the correct normal configuration line with a multicasting kernel.

 

Format: Route add-net {network-address} netmask {netmask} reject

Set to inaccessible to the specified network to avoid connection to the network addressProgramAfter a long wait, you can see that the network is inaccessible.

Man route writes route add-net 10.0.0.0 netmask route 0.0.0 reject
This installa rejecting route for the private network "10. x ."

Format: Route del-net {network-address} netmask {netmask} Dev {interface-name}

Format: Route del-net {network-address} netmask {netmask} reject

Used to delete route settings. The method specified by the parameter is similar to route add.

The meanings of the route table fields output by the route command are as follows:

Destination target
The destination network or destination host. Target network or target host.

Gateway
The gateway address or '*' If none set. gateway address, if not, an asterisk is displayed.

Genmask network mask
The netmask for the destination net; '2017. 255.255 'for
Host destination and '0. 0.0.0 'for the default route.

Flags possible flags include flag, commonly used U and G.
U (route is up) Route Enabled

H (target is a host) the target is a host
G (use gateway) Use Gateway

R (reinstate route for Dynamic Routing)
D (dynamically installed by daemon or redirect)
M (Modified from routing daemon or redirect)
A (installed by addrconf)
C (cache entry)
! (Reject route)

Metric distance and number of hops. Useless.

The 'distance 'to the target (usually counted in hops). It is
Not used by recent kernels, but may be needed by routing Dae-
Mons.

Ref is not required. The constant value is 0.

Number of references to this route. (not used in the Linux Ker-
Nel .)

Use the number of times this route is used to roughly estimate the network traffic to the specified network address.

Count of lookups for the route. Depending on the use of-F and
-C this will be either route cache misses (-F) or hits (-C ).

Iface interface, that is, network interface names such as eth0 and eth0

Interface to which packets for this route will be sent.

Example 1: print the current route table (Root User)

[Root @ jfht ~] # Route

Kernel IP routing table
Destination gateway genmask flags metric ref use iface
211.103.28.0 * too many bytes u 0 0 0 eth0
192.168.1.0*255.255.255.0 u 0 0 0 eth1
169.254.0.0*255.255.0.0 u 0 0 0 eth1
Default 211.103.28.1 0.0.0.0 ug 0 0 0 eth0
[Root @ jfht ~] # /Sbin/route

Kernel IP routing table
Destination gateway genmask flags metric ref use iface
211.103.28.0 * too many bytes u 0 0 0 eth0
192.168.1.0*255.255.255.0 u 0 0 0 eth1
169.254.0.0*255.255.0.0 u 0 0 0 eth1
Default 211.103.28.1 0.0.0.0 ug 0 0 0 eth0
[Root @ jfht ~] # Route-n

Kernel IP routing table
Destination gateway genmask flags metric ref use iface
211.103.28.0 0.0.0.0 255.255.255.255.u 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.0 u 0 0 0 eth1
169.254.0.0 0.0.0.0 255.255.0.0 u 0 0 0 eth1
0.0.0.0 211.103.28.1 0.0.0.0 ug 0 0 0 eth0
[Root @ jfht ~] # /Sbin/route-n

Kernel IP routing table
Destination gateway genmask flags metric ref use iface
211.103.28.0 0.0.0.0 255.255.255.255.u 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.0 u 0 0 0 eth1
169.254.0.0 0.0.0.0 255.255.0.0 u 0 0 0 eth1
0.0.0.0 211.103.28.1 0.0.0.0 ug 0 0 0 eth0
[Root @ jfht ~] #

Example 2 print the current route table (non-Root User)

[Web @ hnweb1 ~] $ Route

-Bash: Route: Command not found
[Web @ hnweb1 ~] $ /Sbin/route

Kernel IP routing table
Destination gateway genmask flags metric ref use iface
10.66.10.0*255.255.255.255.128 u 0 0 0 eth0
192.130.12.0 10.66.10.1 255.255.255.0 ug 0 0 0 eth0
10.0.0.0*255.255.255.0 u 0 0 0 eth1
10.66.0.0 10.66.10.1 255.255.0.0 ug 0 0 0 eth0
134.161.0.0 10.66.10.1 255.255.0.0 ug 0 0 0 eth0
10.255.0.0 10.66.10.1 255.255.0.0 ug 0 0 0 eth0
169.254.0.0*255.255.0.0 u 0 0 0 eth1
172.224.0.0 10.66.10.1 255.255.0.0 ug 0 0 0 eth0
Default 10.66.10.22 0.0.0.0 ug 0 0 0 eth0
[Web @ hnweb1 ~] $ Route-n

-Bash: Route: Command not found
[Web @ hnweb1 ~] $ /Sbin/route-n

Kernel IP routing table
Destination gateway genmask flags metric ref use iface
10.66.10.0 0.0.0.0 255.255.255.128 u 0 0 0 eth0
192.130.12.0 10.66.10.1 255.255.255.0 ug 0 0 0 eth0
10.0.0.0 0.0.0.0 255.255.255.0 u 0 0 0 eth1
10.66.0.0 10.66.10.1 255.255.0.0 ug 0 0 0 eth0
134.161.0.0 10.66.10.1 255.255.0.0 ug 0 0 0 eth0
10.255.0.0 10.66.10.1 255.255.0.0 ug 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 u 0 0 0 eth1
172.224.0.0 10.66.10.1 255.255.0.0 ug 0 0 0 eth0
0.0.0.0 10.66.10.22 0.0.0.0 ug 0 0 0 eth0
[Web @ hnweb1 ~] $

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.