Linux dual-nic static route modification

Source: Internet
Author: User

I am learning Linux operations, in the framework of the need to virtual multiple servers and configure a dual network adapter, when the configuration of the dual-NIC IP is found, the system will automatically select a network card as the default route. In my environment, the system chooses the network card is not what I want, and the default route write rc.local boot can be executed, but with/etc/init.d/network restart the network card configuration will be the problem of routing recovery, if this is in production environment need to go to the scene processing. So on the Internet to find a modified static routing post (the original address will be posted at the end), can be executed under the/etc/init.d/network script method. But here's one thing to say. Improvements to the default route for this script

1. 在/etc/init.d/network查找到我们要修改的循环的位置               
[[email protected] ~]# grep -n ‘# Add non interface-specific static-routes.‘ /etc/init.d/network 138:    
2. vim打开/etc/init.d/network文件找到刚才查找到行
[[email protected] ~]# vi /etc/init.d/network打开之后137 138         # Add non interface-specific static-routes.139         if [ -f /etc/sysconfig/static-routes ]; then140            grep "^any" /etc/sysconfig/static-routes | while read ignore args ; do141               /sbin/route add -$args142            done143         fi144         # Add non interface-specific static arp entries.:set nu                                                                                                            

Change lines 140th and 141 to

#Add noninterface-specific static-routes.if [ -f/etc/sysconfig/static-routes ]; then  egrep "^1" /etc/sysconfig/static-routes | while read ignore args ;   do       /sbin/route $args  donefi

This allows the/sbin/route command to be followed by any parameters, not just the add

    1. Build static-routes in the/etc/sysconfig/directory and Add commands

Delete the default route of 172 and establish a 10 segment default route at the same time
This will eventually run this loop to overwrite the default route when we restart the NIC
A normal route modifies the route command, replaces the route with 1, and 1 is replaced in the script.

4. 验证我在执行/sbin/route后面加了一条
          do       /sbin/route $args        echo 1111111111111##############  done

To output an identifier every time the route command is executed good notification has been executed

5. 最后执行验证
[[email protected] ~]# /etc/init.d/network restartShutting down interface eth0:                              [  OK  ]Shutting down interface eth1:                              [  OK  ]Shutting down loopback interface:                          [  OK  ]Bringing up loopback interface:                            [  OK  ]Bringing up interface eth0:  Determining if ip address 10.0.0.41 is already in use for device eth0...                                                           [  OK  ]Bringing up interface eth1:  Determining if ip address 172.16.1.41 is already in use for device eth1...                                                           [  OK  

View the routing table

[[email protected] ~]# route -nKernel IP routing tableDestination     Gateway         Genmask         Flags Metric Ref    Use Iface10.0.0.0        0.0.0.0         255.255.255.0   U     0      0        0 eth0172.16.1.0      0.0.0.0         255.255.255.0   U     0      0        0 eth1169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth10.0.0.0         10.0.0.254      0.0.0.0         UG    0      0        

Static routing successful ~

Because the command parameters executed in the loop are deleted, it is convenient to add other parameters, can delete or add other routes, so there should be no loss of the original mission of the script, static routes can be set, note the file in the wording.

Other methods described in the document or other methods of documentation have tried, should be this easy to use, here to share.

Reference file address #https://www.cnblogs.com/mengfanrong/p/4816695.html

Linux dual-nic static route modification

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.