1. 程式人生 > >Linux雙網卡靜態路由修改

Linux雙網卡靜態路由修改

ref 正在 生產 vim 原來 oca logs 如果 then

本人正在學習Linux運維,在做架構時需要虛擬多臺服務器且配置雙網卡 , 當設置好雙網卡IP的配置時發現 , 系統會自動選擇一個網卡作為默認路由 . 而在我的環境中系統選擇的網卡並非我所想要的 , 而且將默認路由寫入rc.local開機可以執行 , 但用/etc/init.d/network重啟網卡配置就會出現路由恢復的問題 , 如果這是在生產環境就需要去現場處理 . 所以在網上找到一個修改靜態路由帖子(原文地址會在最後貼出) , 可以在 /etc/init.d/network腳本下執行的方法 . 但在這裏要說的是一點這個腳本對默認路由的改進


1. 在/etc/init.d/network查找到我們要修改的循環的位置               
[root@template ~]# grep -n ‘# Add non interface-specific static-routes.‘ /etc/init.d/network 
138:    # Add non interface-specific static-routes.
[root@template ~]# 
2. vim打開/etc/init.d/network文件找到剛才查找到行
[root@template ~]# vi /etc/init.d/network
打開之後
137 
138         # Add non interface-specific static-routes.
139         if [ -f /etc/sysconfig/static-routes ]; then
140            grep "^any" /etc/sysconfig/static-routes | while read ignore args ; do
141               /sbin/route add -$args
142            done
143         fi
144         # Add non interface-specific static arp entries.
:set nu                                                                                                            

將第140和141行改為

#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
  done
fi

這樣/sbin/route命令後面就可以接任何參數 , 不用只接add

  1. 在 /etc/sysconfig/目錄下建立static-routes,並添加命令
[root@backup ~]# vi /etc/sysconfig/static-routes 
寫入
1 add default gw 10.0.0.254
1 del default gw 172.16.1.254

[root@backup ~]# cat /etc/sysconfig/static-routes
1 add default gw 10.0.0.254
1 del default gw 172.16.1.254
[root@backup ~]# 

刪除172的默認路由 同時建立10網段默認路由
這樣在我們重啟網卡時 最後會運行這個循環 從而改寫默認路由
寫法就是正常route修改路由命令 , 將route替代為1 , 在腳本中1會被替換掉

4. 驗證
我在執行/sbin/route後面加了一條
          do
       /sbin/route $args
        echo 1111111111111##############
  done

讓每次執行route命令時輸出一個標識符 好通知已經執行

5. 最後執行驗證
[root@backup ~]# /etc/init.d/network restart
Shutting 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  ]
1111111111111##############
1111111111111##############
[root@backup ~]# 

查看路由表

[root@backup ~]# 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
172.16.1.0      0.0.0.0         255.255.255.0   U     0      0        0 eth1
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1003   0        0 eth1
0.0.0.0         10.0.0.254      0.0.0.0         UG    0      0        0 eth0
[root@backup ~]# 

靜態路由成功~

由於將循環中執行的命令參數刪除 ,方便添加其他參數 , 可刪除或添加其他路由 , 所以應該沒有損失這個腳本原來的使命 , 靜態路由還是可以設置的 , 註意一下文件中寫法就好了 .

文檔中介紹的其他方法或其他文檔的方法都試過 , 應該就是這個好用 , 在此給大家分享一下.

參考的文件地址 #https://www.cnblogs.com/mengfanrong/p/4816695.html

Linux雙網卡靜態路由修改