A detailed description of the route command under Linux

Source: Internet
Author: User

In order for the device to access another subnet, you need to increase the routing to the sub-network in the device, here are some information. The basic operations are as follows:

In general, to be able to access other subnets to set up the route, for example, your host is in 192.168.10.0/24, and you want to access the 192.168.20.0/24 network host, of course, you know a gateway IP, For example 192.168.10.1 (must be on the same subnet as your host), you can configure the route as such.

Add route

Route add-net 192.168.20.0 netmask 255.255.255.0 GW 192.168.10.1

View route Status

Route-n

Delete route

Route del-net 192.168.20.0 netmask 255.255.255.0

Route modification Route
We talked about the problem of routing on the basis of the network, there must be a route between the two hosts to be able to communicate TCP/IP protocol, otherwise you will not be able to connect Ah!
In general, as long as there is a network interface, the interface will produce a route, for example, in the bird's brother lab inside the host has a eth0 and lo, so:
[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.
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.0255.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
By carefully observing the output of the route and route-n in the above example, you can see that the main point of the Add-n parameter is to display the IP, and as for the use of the route, the host name is displayed. That is, in the case of a preset, the route will go to find the host name of the IP, if not found? It will show blunt (a little bit slow), so I usually use route-n directly! From above, we also know the default = 0.0.0.0/0.0.0.0, and what do you have to know about the above information?

· Destination, Genmask: These two gadgets are the network and netmask! So the two are combined into a complete domain!

· Gateway: Is this domain connected through the gateway? If the display 0.0.0.0 indicates that the route is delivered directly from the local computer, that is, it can be directly routed through the MAC of the regional network, and if there is a display IP, it means that the route needs to be routed through the router (gateway).

· 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!) )

· Iface: This route passes the interface of the packet.

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) at 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: increase and deletion of routes
[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 to include netmask, Dev, and so on! Note that

[Email protected] ~]# Route add-net 192.168.100.0 netmask 255.255.255.0 Dev eth0

Add a route via the route add! Please note that this route must be interoperable with you. For example, if I give the following instructions, I'll show the error: Route add-net 192.168.200.0 netmask 255.255.255.0 GW 192.168.200.254 because there's only 192.168.10.100 in my environment IP, so can not be connected with 192.168.200.254 this network directly using MAC! It's understandable, you know?

[[email protected] ~]# route add default GW 192.168.10.30

How to add a preset route! 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
  如果是要进行路由的删除与增加,那就得要参考上面的例子了,其实,使用 man route 里面的资料就很丰富了!仔细查阅一下啰!你只要记得,当出现『SIOCADDRT: Network is unreachable』这个错误时,肯定是由于 gw 后面接的 IP 无法直接与您的网域沟通 (Gateway 并不在你的网域内),所以,赶紧检查一下是否输入错误啊!加油吧!
Routes added by the route command, the machine restarts, or the network card restarts, the method of setting the permanent route under Linux:

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

============================

The route command under Windows:

The simple operation is as follows,

View route Status: Route print

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.0 192.168.10.1

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

Route-p Add 192.168.20.0 Mask 255.255.255.0 192.168.10.1

Delete route: Route Delete Destination network mask netmask

Route Delete 192.168.20.0 Mask 255.255.255.0

A detailed description of the route command under Linux

Related Article

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.