Add permanent static routes to CentOS

Source: Internet
Author: User

Today, another friend asked, How do I add permanent static routes to CentOS? Here, we will make a memorandum.
Refer to the official RedHatEnterprise6 documentation:
Http://docs.redhat.com/docs/en-US/Red_Hat_Enterprise_Linux/6/html/Deployment_Guide/s1-networkscripts-static-routes.html

Our server generally enables two NICs, one with the Intranet address and the other with the Internet address, but we generally only configure the default gateway for the NIC. Because if both NICs are configured with the default gateway, you will not know which gateway to go when data packets are sent out.
When the local machine communicates with other machines, data packets are sent to the default gateway, and then the default gateway forwards the data packets to the target host. Because the default gateway of the Internet Nic is configured, it is OK for the local machine to communicate with the Internet IP address of the target host. However, there is no way to communicate with the Intranet IP address, because the default gateway cannot reach the Intranet CIDR block. (Of course, if route forwarding is enabled on this LINUX machine, it should be another thing .)
To communicate with machines in the Intranet CIDR block, we write a static route to specify a path to the Intranet CIDR block. This article describes how to add a permanent static route in CentOS.

To add a permanent static route, write the route table to the/etc/sysconfig/network-scripts/route-interface configuration file. Interface is your Nic, such as eth1 static routing configuration file is route-eth1.
The route-interface configuration file can be written in either the ip command format or the network/netmask format:
Ip command format:
1. defaultviagatewaydevinterface // Replace the gateway with your default gateway, and the interface with your Nic 2.ip/ netmaskviagatewaydevinterface3 .... // Add more static routes as needed. 4 .... Network/netmask command format:
1. ADDRESS0 = CIDR block. This CIDR block is the CIDR block you want to reach. 2. NETMASK0 = mask. 3. GATEWAY0 = gateway address. The Gateway is the egress gateway of the NIC and can reach ADDRESS04 .... // Add more static routes as needed. 5... the first ip command format is used as an example.
According to the official website, the default route in the route table can be omitted because the default gateway is used.
The second route points to the specific CIDR block we want to reach. Ip/netmask is the network segment you want to reach, gateway is the egress gateway of the NIC, and interface specifies the NIC.


Let's take a look at an online example. This configuration adds two static routes for the NIC eth0:
1.10.0.0.0/8via10. 126.40.2542.192.168.188.0/22via10. 126.40.254 the preceding configuration adds two static routes pointing to the 10.0.0.0/8 and 192.168.188.0/22 CIDR blocks respectively. 10.126.40.254 is the egress gateway of eth0.
The above configuration comes from: http://www.m690.com /? P = 346.

In my own server configuration example, two static routes are configured for the network adapter eth1:
1. GATEWAY0 = route = 255.255.255.0.03.address0 = 192.168.0.04.5.GATEWAY1 = route = 255.255.255.0.07.address1 = 10.10.0.0 we have added two static routes pointing to 10.10.0.0/16 and 192.168.0.0/16 respectively, use the egress gateway 10.10.14.254 of eth1.
If multiple static routes exist in this format, each static route must be numbered sequentially, such as ADDRESS0, ADDRESS1, and ADDRESS2.

Note: According to information on the Internet, the gateway in the second route is the gateway of the next hop. I specifically asked my colleague from the network department that he said it was wrong. The gateway is actually the egress gateway of the NIC.
The gateway in the first default route is the default gateway of the Internet Nic.
What is the difference between the gateway (the egress gateway of the NIC) in the second route and the gateway (the default gateway) in the default route? The RedHatEnterprise6 official document says: TheX. X (the egress gateway of the NIC) addressdoesnothavetobethedefagategatewayipaddress. Inmostcases, X. XwillbeanIPaddressinadifferentsubnet, andinterfacewillbetheinterfacethatisconnectedto, orcanreach, thatsubnet.
The differentsubnet mentioned in this document refers to a subnet different from the default gateway. The subnet must be in the same network segment as the IP address of the interface. The subnet here generally refers to the subnet where the Intranet Nic is located, and the gateway here generally refers to the gateway of the Intranet Nic.


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.