Linux adds static routing table, Reboot continues to take effect

Source: Internet
Author: User

In the daily use, or in the server, there are two network cards configured two addresses, access to different network segments, this situation is very common phenomenon, but, We need to add additional routing tables to determine which packets are sent through the correct gateways and interface to communicate correctly today to introduce how to add routes in the Linux operating system (mainly for Rhel series, test Platform for rhel5.4)
One, add temporary routes to the host by using the route command # route add-host 192.168.1.123 Dev eth0# routeAdd-host 192.168.1.123 gw192.168.1.1 Add route to the network # route add-net 192.168.1.123 netmask 255.255.255.0eth0# Route add-net 192. 168.1.123 netmask 255.255.255.0 gw192.168.1.1# Route add-net 192.168.1.123 netmask 255.255.255.0 gw192.168.1.1 eth1# rout E 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 D El-net 192.168.1.123 netmask 255.255.255.0
Second, add a permanent route in Linux Method 1, write/etc/rc.loacl can write the above mentioned command/etc/ rc.local file, which automatically joins the relevant routing settings when the system is started but there is one drawback to this approach: if a system service, such as an NFS service, is launched after the network service is started, and if you set up NFS with automatic mount before executing rc.local, then the chain The road is not smooth, will cause the failure of the mount. The other is that if you restart the Web server, then the route will be invalidated, and this time you have to reload the file, but if you are remote operation? So, this method is not very recommended2, add to the end of the file in/etc/sysconfig/network, the format is as follows (note: not verified validity): Gateway=gw-ip or Gateway=gw-dev this way from the appearance can only be used to add the default gateway ... For the multi-channel network environment seems to be not suitable for the look3, write directly to the Ifcfg file when configuring the IP address directly to the GATEWAY configuration to write to the Ifcfg file, form: Gateway=gw-ip suitable for adding default routes4, write to the/etc/sysconfig/static-routes file by default in the/ETC/SYSCONIFG directory is not this file, we need to create manually, the call to this file in the following: /etc/init.d/network:159# ADD noninterface-specific static-routes.160if [-f/etc/sysconfig/static-routes]; Then161grep "^any"/etc/sysconfig/static-routes | While read ignore args; Do162 /sbin/route add-$args163Done164Fi /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 this way, Compared with rc.local, more practical, such as NFS, the effective time of this route is in effect when network service is started, and some other network related services are started after the successful start of network service, so it can ensure the smooth network link. Moreover, if I restart the Network service, this script is called in the Network service startup script, so it will also automatically add the relevant routing settings
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.