Add a static route table in linux and restart it to take effect.

Source: Internet
Author: User

Add a static route table in linux and restart it to take effect.
In daily use, or on the server, two NICs are configured with two addresses to access different network segments. This is a very common phenomenon, however, we need to add a route table to determine the correct gateway and interface for the sent packets to communicate correctly. Today, we will introduce how to add routes in the linux operating system (mainly for the rhel series, the test platform is rhel5.4)
1. Use the route command to add a route entry to the host # route add-host 192.168.1.123 dev eth0 # routeAdd-host 192.168.1.123 gw192.168.1.1 route entry to the network # route add-net 192.168.1.123 netmask route # route add-net 192.168.1.123 netmask route gw192.168.1.1 # route add-net 192.168.1.123 netmask route 255.255.0 gw192.168.1.1 eth1 # route add -net 192.168.1.0/24 eth1 add default Gateway # route add default gw 192.168.1.1 Delete route # route del-host 192.168.1.11 dev eth0 # route del-net 192.168.1.123 netmask 255.255.0
2. Add permanent routes in linux. 1. Write/etc/rc. loacl can write the commands mentioned above to/etc/rc. in the local file, the related routing settings will be automatically added when the system starts. However, this method has one disadvantage: if a system service, such as the NFS service, this service is executed after the network service is started. if you set nfs to be automatically mounted before local, the link is not smooth and the mounting will fail. The other one is that if you restart the network server, the route will become invalid and you have to re-load the file. But what if you operate remotely? Therefore, this method is not very recommendable.2. Add it to the end of the file in/etc/sysconfig/network in the following format (Note: unverified validity ): GATEWAY = gw-ip or GATEWAY = gw-dev can only be used to add the default GATEWAY... It seems unsuitable for multi-channel network environments3. directly write the ifcfg file and directly write the GATEWAY configuration to the ifcfg file When configuring the ip address. Format: GATEWAY = gw-ip is suitable for adding default routes.4. Write the/etc/sysconfig/static-routes file. This file is not found in the/etc/sysconifg directory by default. You need to create it manually and call this file below:/Etc/init. d/network: 159 # Add noninterface-specific static-routes.160 If [-f/etc/sysconfig/static-routes]; then161  Grep "^ any"/etc/sysconfig/static-routes | while read ignore args; do162  /Sbin/route add-$ args163  Done164 Fi/Etc/sysconfig/static-routes:Any net 192.168.1.0/24 gw 192.168.1.24any net 192.168.2.0 netmask 255.255.255.0 gw 192.168.2.1 and rc. local is more practical, such as nfs. The effective time of this route takes effect when the network service network is started, other network-related services are started after the network service is successfully started, so the network link can be smooth. In addition, if I restart the network service, this script is called in the network service startup script, so the relevant route settings will be automatically added.
How to add a static route table in linux

You can add the default gateway to the three configuration files.

1./etc/sysconfig/network-srcipts/ifcfg-ethx, x = 0, 1, 2, 3...
Ii./etc/sysconfig/network
The added content is GATEWAY = 168.200.200.226.
Iii./etc/sysconfig/network-srcipts/route-ethx, x = 0, 1, 2, 3...
0.0.0.0/0 via 168.200.200.226

When both the three configuration files contain the default gateway, the network configuration takes effect.

Or, write the command you entered into/etc/rc. local, which is similar to autoexec. bat in DOS.
 
In linux, how to add a default route can also take effect after the machine is restarted

Route add-net 172.24.130.0 netmask 255.255.255.0 gw 172.24.130.1
Or
Route add default gw 172.24.130.1 bond0
If not, ll. Check if your rc. local has any problems.
 

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.