Linux route command in layman's and actual case explaining
http://blog.51cto.com/oldboy/1119453
-route command in layman's use guide
http://blog.51cto.com/oldboy/974194
Video commentary of this blog post published: http://oldboy.blog.51cto.com/2561410/1119453
Origin: This article for the old boy Linux training seventh pre-class exam questions and reference answers, have friends after reading http://oldboy.blog.51cto.com/2561410/784625 content, hope to supplement this content, therefore, released as follows, I hope you like. Complete questions and answers are as follows:
Exam question one: How to add a route under Linux (Baidu interview question)
The above is the original question, the old boy teacher translated into the following 3 questions.
A. How to add a default gateway to a Linux machine using the command line, assuming the gateway address is 10.0.0.254?
B. 192.168.1.0 network segment, one of the 192.168.1.1 gateway servers want to connect to the 172.16.1.0/24 segment, how to add a route (Qihoo 360)
C. What if I add a host route?
Please answer separately.
Answer: Route -net 172.16.1.0/24 GW 192.168.1.1
How to use the route command:
A. Default gateway route
The default gateway is that the packet does not match any of the set routing rules, and finally flows through the address gates! The gateway literally means the gate of the network, the same as the door of our house, if we go out to go through the door, the packet is the same.
The answer to this question:
Route del default GW 10.0.0.254
Solution Practice:
[[email protected] ~]# route-n #==> View the routing table, Netstat-rn can also.
Kernel IP Routing Table
Destination Gateway genmask Flags Metric Ref use Iface
10.0.0.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 10.0.0.254 0.0.0.0 UG 0 0 0 eth0
#==> here is the default gateway information for the system, which means to go anywhere (0.0.0.0), sent to 10.0.0.254 because it is the default gateway, so put it in the last one. Routes are also ordered, and if they do not conform to any of the rules, they are handed to the default gateway for processing.
[[email protected] ~]# route del default GW 10.0.0.254 #==> This command is to delete the default gateway.
[Email protected] ~]# route-n
Kernel IP Routing Table
Destination Gateway genmask Flags Metric Ref use Iface
10.0.0.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
[[email protected] ~]# route add default GW 10.0.0.254 #==> This command is the answer to the question of adding the defaults gateway.
[Email protected] ~]# Netstat-rn
Kernel IP Routing Table
Destination Gateway genmask Flags MSS Window Irtt Iface
10.0.0.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 10.0.0.254 0.0.0.0 UG 0 0 0 eth0 #==> 's back.
[Email protected] ~]# route-n
Kernel IP Routing Table
Destination Gateway genmask Flags Metric Ref use Iface
10.0.0.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 10.0.0.254 0.0.0.0 UG 0 0 0 eth0 #这里就是添加的默认网关记录.
In particular, the route add default GW 10.0.0.254 is equivalent to the route add-net 0.0.0.0 netmask 0.0.0.0 GW 10.0.0.254
B. Network routing: That is, a route to a network or network segment
Generally, multiple network segments communicate with each other, and you want to establish a priority route, rather than a default gateway, to configure network routing. Or to take the house analogy, you are not going out now, but the bedroom, bathroom, to the bedroom will go through the bedroom door, to the bathroom also to go through the bathroom door, where the bedroom and the bathroom door can be considered to go to a certain segment of the route, rather than the default route (that is, the door of the house. )
Actual work will have demand, two different internal network visits, rather than network access, is the case of the above example.
The answer to this question:
Route add-net 192.168.1.0 netmask 255.255.255.0 GW 192.168.1.1
Solution Practice:
[Email protected] ~]# Route add-net 192.168.1.0 netmask 255.255.255.0 GW 192.168.1.1
SIOCADDRT: Network unreachable #==> Cannot add a route when the address 192.168.1.1 is not connected.
[email protected] ~]# ifconfig eth0:0 192.168.1.1/24 up #==> adds an IP alias for temporary testing, preferably with a dual NIC or write to the configuration file if it is permanently in effect.
[email protected] ~]# ifconfig eth0:0 #==> view the added IP alias (this multi-IP is called sub-interface in the network)
eth0:0 Link encap:ethernet HWaddr 00:0C:29:65:A4:FD
inet addr:192.168.1.1 bcast:192.168.1.255 mask:255.255.255.0
Up broadcast RUNNING multicast mtu:1500 metric:1
Then add the packet to the 192.168.1.0, and give it to 192.168.1.1 processing.
[Email protected] ~]# Route add-net 192.168.1.0 netmask 255.255.255.0 GW 192.168.1.1
[[email protected] ~]# netstat-rn #==> and route-n very much alike.
Kernel IP Routing Table
Destination Gateway genmask Flags MSS Window Irtt Iface
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.1.0 192.168.1.1 255.255.255.0 UG 0 0 0 eth0 #==> This is network routing
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 0 0 0 eth0
0.0.0.0 10.0.0.254 0.0.0.0 UG 0 0 0 eth0
Expand: Other Wording
[[email protected] ~]# Route add-net 192.168.1.0 netmask 255.255.255.0 dev eth0 #==> Specify the device instead of the address.
[Email protected] ~]# route-n
Kernel IP Routing Table
Destination Gateway genmask Flags Metric Ref use Iface
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.1.0 192.168.1.1 255.255.255.0 UG 0 0 0 eth0
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 0 0 0 eth0
0.0.0.0 10.0.0.254 0.0.0.0 UG 0 0 0 eth0
[Email protected] ~]# route del-net 192.168.1.0/24 Dev eth0
[Email protected] ~]# route add-net 192.168.1.0/24 Dev eth0
[Email protected] ~]# route-n
Kernel IP Routing Table
Destination Gateway genmask Flags Metric Ref use Iface
10.0.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
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 0 0 0 eth0
0.0.0.0 10.0.0.254 0.0.0.0 UG 0 0 0 eth0
Summarize:
Route add-net 192.168.1.0 netmask 255.255.255.0 GW 192.168.1.1
Route add-net 192.168.1.0 netmask 255.255.255.0 Dev eth0
Route add-net 192.168.1.0/24 Dev eth0
Route del-net 192.168.1.0/24 Dev eth0
Special emphasis: The above configuration will fail when restarting the network, so how to make it permanent?
If it is permanent, there are several ways:
Method One:
Vi/etc/sysconfig/network-scripts/route-eth0 #默认不存在此文件
Add the following content:
192.168.1.0/24 via 192.168.1.1
Tip: Write to the configuration, restart the network service and restart the system will be effective!
Method Two:
Vi/etc/sysconfig/static-routes #默认不存在此文件
Add the following content:
Any net 192.168.1.0/24 GW 192.168.1.1
Tip: Write to the configuration, restart the network service and restart the system will be effective!
Method Three:
Vi/etc/rc.local
Add the following content:
Route add-net 192.168.1.0/24 GW 192.168.1.1
PS: Method One recommended production environment use
Tip: Method Three writes to the/etc/rc.local only when the power-on load, when manually restart the network will be invalidated, but restart the system will take effect!
If you are configuring a default routing gateway, you can re-configure the NIC:
[[email protected] ~]# grep gateway/etc/sysconfig/network-scripts/ifcfg-eth0
gateway=10.0.0.254
C. Host routing: is to go to a host address how to configure routing
/sbin/route add-host 192.168.2.13 Dev eth2
/sbin/route add-host 202.81.11.91 Dev Lo
For example: Keepalived or heartbeat high-availability server pair between the use of a separate NIC connection to the heartbeat line communication will use the above host routing.
Route command Expansion:
Delete a default route:
Route del default GW 10.0.0.254
To delete a static route:
Route del –net Target network netmask
such as: Route del-net 192.168.1.0/24 or Route del-net 192.168.1.0 netmask 255.225.255.0
To delete a host route:
Route Del-host 192.168.1.10 Dev eth0
More detailed information about the route command requires you to perform a man route view help and summarize it carefully.
About this problem, we talk about the network segment Division and routing solution of multi-segment production environment (1000 Machine dividing network segment scheme). We can feel the different functions of the route command application case.
Please refer to the old boy Linux OPS training-production environment more than 254 machine segment Division and routing solution Case
(turn) Linux route command in layman's and actual case explaining