The use of the Linux route command is detailed

Source: Internet
Author: User

The use of the Linux route command is detailed

--http://www.cnblogs.com/snake-hand/p/3143041.html

One Linux command per day: Route command

--http://www.cnblogs.com/peida/archive/2013/03/05/2943698.html

The route command is used to display and 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. It is important to note that the route command is executed directly at the command line to add routes that are not persisted, that the route is invalidated when the network card is restarted or after the machine restarts, and 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).

Use the full path to execute the route command when non-root users are in use.

Command parameters

[Email protected] ~]# route [-nee]
[[email protected] ~]# route add [-net|-host] [domain or host] netmask [mask] [Gw|dev]
[[email protected] ~]# route del [-net|-host] [domain or host] netmask [mask] [Gw|dev]
Parameters of the observation:
-N: Do not use the communication protocol or host name to directly use IP or port number;
-ee: Use more detailed information to display
Add (add) and delete (del) route related parameters:
-net: Indicates that the following route is a domain;
-host: Represents a route that is followed by a connection to a single host;
Netmask: Related to the domain, you can set the netmask to determine the size of the domain;
Gw:gateway, followed by the IP value oh, unlike Dev;
Dev: If you only want to specify that a network card is connected, use this setting, followed by eth0, etc.

Format: Route-n
Format:/sbin/route-n
is used to print the routing table, plus the-n parameter is to print the IP address directly without printing the hostname in the output information.

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 {interface-name}: Used to specify the 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}
parameter {network-address}: Used to specify the network address
parameter {NETMASK}: Used to specify the subnet mask
parameter {interface-name}: Used to specify the 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 eth0

Format: Route add-net {network-address} netmask {netmask} reject
set to the specified network is unreachable, avoid the program too long waiting when connecting to the address of this network, Just know that the network is unreachable.

Example: Route add-net 10.0.0.0 netmask 255.0.0.0 reject

Format: Route del-net {network-address} netmask {netmask} de v {interface-name}
Format: Route del-net {network-address} netmask {netmask} reject
is used to remove routing settings. The parameters are specified in the same way as the route Add.

Output detailed

The route table field of the route command output has the following meanings:
Destination Target
The destination network or destination host. Target network or destination host.

Gateway Gateways
The gateway address or ' * ' if none set. The gateway address, if not, displays an asterisk.

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: There are a total number of flags representing the following meanings:

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): The external host (gateway) is required to transmit the packet;

o R (reinstate route for dynamic routing): Restores the flag of the routing information when using dynamic routing;

o D (dynamically installed by daemon or redirect): Already set to dynamic routing by the Service or port function

o M (modified from routing daemon or redirect): The route 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 count. Temporarily useless.
The ' distance ' to the target (usually counted in hops). It is
Not used by recent kernels, but could be needed by routing dae-
Mons.

Ref no tube, constant is 0.
Number of references to the this route. (not used in the Linux Ker-nel.)

Using the number of times the route is used, you can 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, i.e., Eth0,eth0 network interface name
Interface to which packets for the this route would be is sent.

Example One

Simply observing the routing state


[Email protected] ~]# route-n
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
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 UG 0 0 0 eth0
[Email protected] ~]# route
Kernel IP Routing Table
Destination Gateway 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 0.0.0.0 UG 0 0 0 eth0

Also, take a look at the routing order above, which is ordered by the small domain (192.168.10.0/24 is class C), and gradually to the large domain (169.254.0.0/16 class B) and finally to the default route (0.0.0.0/0.0.0.0). Then, when we want to determine how a network packet should be transmitted, the packet will be judged by the process of this route! For example, I have only three routes above, if I have a packet destined for 192.168.10.20 to pass, then first will find 192.168.10.0/24 this domain routing, found! So it is transmitted directly from the eth0, if it is sent to Yahoo's host? Yahoo's host IP was 202.43.195.52, and I judged by

1) Not 192.168.10.0/24,
2) Not 169.254.0.0/16 results arrive

3) 0/0, OK! Out of the way, through the eth0 to the 192.168.10.30 of the gateway host Ah! So, the routes are sequential. So what happens when you repeatedly set multiple identical routes, such as when the two network card on your console is set to the same domain IP? 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 in accordance with the order and transmission, so whether the packet is received by the interface (Eth0, eth1), will be sent out by the above eth0, so, in a host set two of the same domain IP itself is meaningless! Just a little superfluous. This is necessary unless it is a multi-host, such as a virtual host (Xen, VMware, and other software).

Example Two

[Email protected] ~]# 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 on the routing table
# includes netmask, dev, etc. parameters Oh! Note that
[Email protected] ~]# Route add-net 192.168.100.0 netmask 255.255.255.0 Dev eth0
# Add a route via route add! Please note that this route must be interoperable with you.


# For example, if I release the instructions below, I'll show an error:
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, so cannot with 192.168.200.254 this network segment direct use MAC interworking! It's understandable, you know?
[[email protected] ~]# route add default GW 192.168.10.30
# Add a way to preset routes! Please note that there is only one preset route that will suffice!
# In this place if you want to set up, remember to use the instructions below to reset your network
/etc/init.d/network restart
If you are going to remove and increase the route, then you have to refer to the above example, in fact, the use of the man route inside the material is very rich! Check it out! As long as you remember, when the "Siocaddrt:network is unreachable" error occurs, it must be that the IP behind the GW is not able to communicate directly with your domain (gateway is not within your domain).

The route command under Windows

The simple operation is as follows,

View routing Status: Routeprint

View only IPv4 (IPv6) Routing Status: Route print-4 (-6)

Add route: Route add destination netmask Subnet mask gateway-restart machine or NIC fails

Route Add 192.168.20.0 Mask 255.255.255.0192.168.10.1

Add permanent: route-p Add destination netmask Subnet mask Gateway

Route-p Add 192.168.20.0 Mask 255.255.255.0192.168.10.1

Delete route: Route Delete Destination network mask netmask

Route Delete 192.168.20.0 mask255.255.255.0

The use of the Linux route command is detailed

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.