The route command is used to display and manipulate IP routing tables. To achieve communication between two different subnets, you need a router that connects two networks, or a gateway that is located in two networks. In a Linux system, routing is often set up to address the following issues: The Linux system has a gateway in a local area network that allows the machine to access the Internet, so it needs to set the IP address of the machine to the default route of the Linux machine.
Role
The route command is used to view and configure the Linux kernel routing table, which is the static routing table used to view and configure Linux.
Describe
The route command operation is based on the Linux kernel routing table, and its primary role is to create a static route to allow a specified host or network to pass through a network interface, such as a 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.
Command format
The route command format is as follows:
Route [-nee]
Route add [-net|-host] [network or host] netmask [mask] [Gw|dev]
Route del [-net|-host] [network or host] netmask [mask] [Gw|dev]
It is important to note that the route is added directly under the command line to add a route that is not permanently saved and is invalidated when the network card is restarted or the machine is restarted; to be permanently saved, there are the following methods:
1. Add in/etc/rc.local
2. Add to end in/etc/sysconfig/network
3./etc/sysconfig/static-router:
Any net x.x.x.x/24 GW Y.y.y.y
Format: Route
Format:/sbin/route
Used to print the routing table (display the current routing table).
You need to use the full path to execute the route command when not using the root user.
Command Arguments
[root@linux ~]# Route [-nee]
[Root@linux ~]# route add [-net|-host] [network domain or host] netmask [mask] [Gw|dev]
[Root@linux ~]# Route del [-net|-host] [network domain or host] netmask [mask] [Gw|dev]
Parameters to observe:
-N: Do not use the communication protocol or host name directly using IP or port number;
-ee: Use more detailed information to display
Increase (add) and delete (DEL) routing related parameters:
-net: Indicates that the route followed is a network domain;
-host: Represents a route followed by a connection to a single host;
Netmask: Related to the domain, you can set the netmask to determine the size of the network domain;
Gw:gateway, followed by the number of IP oh, and Dev is different;
Dev: If you just want to specify that a network card is connected, use this setting, followed by eth0, etc.
Format: Route-n
Format:/sbin/route-n
used to print the routing table, plus the-n parameter is to print the IP address directly in the output information without printing the host name.
Format: Route add default GW {ip-address} {Interface-name}
is used to set the default route, where
parameter {ip-address): Used to specify the IP address of the router (gateway);
Parameter Number {Interface-name}: Used to specify interface name, such as Eth0. Use/SBIN/IFCONFIG-A to display all interface information.
Example: route add default GW Mango
Format: route add-net {network-address} netmask {netmask} dev {interface-name}
Add to Specifies the routing rule for the network, where
parameter {network-address}: is used to specify the network address
parameter {NETMASK}: Used to specify the subnet mask
parameter {interface-name}: For specifying an interface name, such as Eth0.
Example 1:route add-net 192.56.76.0 netmask 255.255.255.0 dev eth0
Example 2:route add-net 224.0.0.0 netmask 240.0.0.0 Dev E Th0
Format: Route add-net {network-address} netmask {netmask} reject
set to the specified network is unreachable, avoiding the program's lengthy wait when connected to the address of this network, Know that the network is not up to the right.
Example: Route add-net 10.0.0.0 netmask 255.0.0.0 reject
Format: route del-net {network-address} netmask {netmask} dev {in Terface-name}
Format: Route del-net {network-address} netmask {netmask} reject
is used to remove routing settings. The parameter is specified in the same way as route Add.
Output detailed
the route command outputs a routed table field meaning as follows:
Destination target
The destination network or destination host. Target network or target host.
Gateway Gateways
The gateway address or ' * ' if none set. The gateway address, and if not, an asterisk is displayed.
Genmask Network Mask
The netmask for the destination net; ' 255.255.255.255 ' for a
Host destination and ' 0.0.0.0 ' for the default route.
Flags: A total of several flags, the meaning of the Representative is as follows:
o U (Route is up): the route is started;
o H (target is a host): The Target is a host (IP) rather than a domain;
o G (use gateway): need to transmit the packet through external host (gateway);
o R (reinstate route for dynamic routing): Restores the flag of routing information when using dynamic routing;
o D (dynamically installed by daemon or redirect): The service or port function has been set to dynamic routing
o M (modified from routing daemon or redirect): routing has been modified;
O! (Reject route): This route will not be accepted (to withstand insecure domains!). )
o A (installed by addrconf)
o C (cache entry)
Metric distance, hop number. Temporarily useless.
The ' distance ' to the target (usually counted in hops). It is
Not used by recent kernels, but may is needed by routing dae-
Mons.
Ref no tube, constant is 0.
Number of references to this route. (not used in the Linux Ker-nel.)
Use the number of times the 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'll be either route cache misses (-f) or hits (-c).
Iface interface, that is, Eth0,eth0 and other network interface names
Interface to which packets the for this route would be sent.
Example One
Simply observing the routing state
[Root@linux ~]# route-n
Kernel IP routing table
destination gateway genmask Flags Metric R ef use iface
192.168.10.0 0.0.0.0 255.255.255.0 u 0 0 0 eth0
169.254.0 .0 0.0.0.0 255.255.0.0 u 0 0 0 eth0
0.0.0.0 192.168.10.30 0.0.0.0&nb Sp ug 0 0 0 eth0
[root@linux ~]# route
Kernel IP routing table
destination Gateway&nbs P genmask Flags Metric ref use iface
192.168.10.0 * 255.255.255.0 u 0 0 0 eth0
169.254.0.0 * 255.255.0.0 u 0 0 0 eth0
default server.cluster& nbsp 0.0.0.0 ug 0 0 0 eth0
In addition, look at the routing order above, in sequence by the small domain (192.168.10.0/24 is class C), gradually to the large domain (169.254.0.0/16 class B) and finally the default route (0.0.0.0/0.0.0.0). Then when we want to determine how a network packet should be routed, the packet will be judged by the process of this route! For example, I have only three routes above, if I have a 192.168.10.20 packet to pass, it will first find 192.168.10.0/24 the domain of the route, found! So it is transmitted directly from the eth0, if it is transmitted to the Yahoo host? Yahoo's host IP is 202.43.195.52, I pass judgment
1) is not 192.168.10.0/24,
2) Not 169.254.0.0/16 results arrive
3) at 0/0, ok! Spread out, through the eth0 will be packets to 192.168.10.30 that Gateway host Ah! So, there is a sequence of routes. So what happens when you repeatedly set multiple identical routes, such as when the two nets on your host network are set to the same IP domain? The following conditions will appear:
Kernel IP Routing Table
Destination Gateway genmask Flags Metric Ref use Iface
192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
That is, because the route is arranged and transmitted in order, so whether the packet is received by that interface (Eth0, eth1), will be transmitted by the above eth0, so, in a host set up two of the same domain IP itself has no meaning! It's a little superfluous. This is necessary unless you are hosting multiple hosts like a virtual host (Xen, VMware, etc.)
Example Two
[Root@linux ~]# Route del-net 169.254.0.0 netmask 255.255.0.0 Dev eth0
# above this action can delete 169.254.0.0/16 this domain!
# Note that when you delete, you need to write the information that appears above the routing table
# including netmask, Dev and so on parameters Oh! Pay attention.
[Root@linux ~]# Route add-net 192.168.100.0 netmask 255.255.255.0 Dev eth0
# Add a route through route add! Please note that this route must be interoperable with you.
# For example, if I give the instructions below, the error will be displayed:
Route add-net 192.168.200.0 netmask 255.255.255.0 GW 192.168.200.254
# because my environment only has 192.168.10.100 this IP, therefore cannot with 192.168.200.254 this network segment direct use MAC interworking! That's understandable.
[Root@linux ~]# route add default GW 192.168.10.30
# Increase the default routing Method! Please note that a preset route is enough.
# In this place if you set it up, remember to reset your network with the instructions below
/etc/init.d/network restart
If the route is to be deleted and added, then you have to refer to the above example, in fact, the use of man route inside the information is very rich! Check it out? Cherry ∧ 阒 Gray cut squid frame? Iocaddrt:network is unreachable "This error is definitely due to the fact that the IP next to GW cannot communicate directly with your domain (the Gateway is not in your domain).
We illustrate how to use the route command with several examples:
routeadd-net127.0.0.0
This command adds a route to the routing table that specifies the address or network. Note that at this point the network is a Class A address, the mask is set to 255.0.0.0, and the newly added entry is connected to the LO device.
Routeadd-netxxx.xxx.xxx.xxxnetmask255.255.255.0deveth0
This command adds a route to the host with the IP address xxx.xxx.xxx.xxx, and its netmask is set to 255.255.255.0.
Routedel-netxxx.xxx.xxx.xxx
This command deletes the route to xxx.xxx.xxx.xxx this network.
The route command also makes it easy to manage routing information for the entire network, and its output is the routing table for the network. As shown below:
[Root@lee/root] #route
Kerneliproutingtable
Destinationgatewaygenmaskflagsmetricrefuseiface
10.10.8.224*255.255.255.255uh000eth0
10.10.8.0*255.255.255.0u000eth0
127.0.0.0*255.0.0.0u000lo
Defaultdgc8.njupt.edu0.0.0.0ug000eth0
Defaultdgc8.njupt.edu0.0.0.0ug100eth0
[root@lee/root]#
The meanings of each field in the output result are:
· Destination represents the destination IP address of the route.
· The gateway represents the host name or IP address used by the gateways. The output "*" above indicates no gateway.
· Genmask represents the network mask for the route.
· Flags are flags that represent routes. The available flags and their meanings are: U indicates that the route is started, h indicates that target is a host, G indicates that the gateway is used, and r represents the reset setting for dynamic routing; d indicates the dynamic installation of the route, m means to modify the route,!
· Metric indicates the unit sales for the route.
· Ref indicates the number of other routes that depend on the current routing status.
· Use indicates the number of routing table entries to be used.
· Iface represents the destination network for the packets sent by the route.
By looking at these output information, we can easily manage the routing table of the network.
Route table field Meaning
[Root@www ~]# Route-n
Kernel IP Routing Table
Destination Gateway genmask Flags Metric Ref use Iface
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
0.0.0.0 192.168.1.254 0.0.0.0 UG 0 0 0 eth0
[Root@www ~]# Route
Kernel IP Routing Table
Destination Gateway genmask Flags Metric Ref use Iface
192.168.1.0 * 255.255.255.0 U 0 0 0 eth0
link-local * 255.255.0.0 U 1002 0 0 eth0
Default 192.168.1.254 0.0.0.0 UG 0 0 0 eth0
Destination target
The destination network or destination host. The destination network or target host (with the-n parameter displays only the IP address, otherwise the host name, such as the above default) is displayed.
Gateway Gateways
The gateway address or ' * ' if none set. The gateway address, and if not, an asterisk is displayed.
Genmask Network Mask
The netmask for the destination net; ' 255.255.255.255′for a host destination and ' 0.0.0.0′for the default route. That is, "255.255.255.255″ represents a host." "0.0.0.0″ represents a gateway.
Flags Possible flags include flags, which are commonly used in u and G.
U (route is up) routing enabled
H (target is a host) destination is host
G (use gateway) using gateways
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, hop number. Temporarily useless.
The ' distance ' to the target (usually counted in hops). It is
Not used by recent kernels, but may is needed by routing dae-
Mons.
Ref no tube, constant is 0.
Number of references to this route. (not used in the Linux ker-
Nel.)
Use the number of times the 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'll be either route cache misses (-f) or hits (-c).
Iface interface, that is, Eth0,eth0 and other network interface names
Interface to which packets the for this route would be sent.