CentOS新增永久靜態路由
在使用雙網絡卡,同時使用
2
個閘道器的時候就需要加一條靜態路由了。當然還有很多時候會需要
加路由。
一:使用
route
命令新增
使用
route
命令新增的路由,機器重啟或者網絡卡重啟後路由就失效了,方法:
//
新增到主機的路由
# route add
–
host 192.168.1.11 dev eth0
# route add
–
host 192.168.1.12 gw 192.168.1.1
//
新增到網路的路由
# route add
–
net 192.168.1.11 net
m
ask 255.255.255.0 eth0
# route add
–
net 192.168.1.11 net
m
ask 255.255.255.0 gw 192.168.1.1
# route add
–
net 192.168.1.0/24 eth1
//
新增預設閘道器
# route add default gw 192.168.2.1
//
刪除路由
# route del
–
host 192.168.1.11 dev eth0
二:在
linux
下設定永久路由的方法:
1.
在
/etc/rc.local
裡新增
方法:
route add -net 192.168.3.0/24 dev eth0
route add -net 192.168.2.0/24 gw 192.168.2.254
2.
在
/etc/sysconfig/network
裡新增到末尾
方法:
GATEWAY
=gw-ip
或者
GATEWAY
=gw-dev
3./etc/sysconfig/static-routes :
any net 192.168.3.0/24 gw 192.168.3.254
any net 10.250.228.128 net
mask 255.255.255.192 gw 10.250.228.129
如果在
rc.local
中新增路由會造成
NFS
無法自動掛載問題,所以使用
static-routes
的方法是
最好的。無論重啟系統和
service network restart
都會生效
以下是蚊子解決
NFS
問題的描述:
按照
linux
啟動的順序,
rc.local
裡面的內容是在
linux
所有服務都啟動完畢,
最後才被執行的,
也就是說,
這裡面的內容是在
netfs
之後才被執行的,
那也就是說在
netfs
啟動的時候,
伺服器
上的靜態路由是沒有被新增的,所以
netfs
掛載不能成功。
static-routes
檔案又是什麼呢,這個是
network
指令碼執行時呼叫的一個檔案,這個檔案的放
置在
/etc/sysconfig
目錄下,在
network
指令碼中的位置是:
# Add non interface-specific stati
c-routes.
if [ -f /etc/sysconfig/static-routes ]; then
grep "^any" /etc/sysconfig/static-routes | while read ignore args do
/sbin/route add -$args
done
fi
從這段指令碼可以看到,這個就是新增靜態路由的方法,
static-routes
的寫法是
any net 192.168.0.0/16 gw
閘道器
ip
這樣的話,在啟動
network
指令碼的時候路由就自動新增上了,又因為
network
是在
netfs
前
面啟動的,自然在掛載
nfs
的時候就正常了。
這樣看來,
如果需要新增靜態路由,
使用
static-routes
檔案要比使用
rc.local
好,而且當改變
了網路配置,需要重啟
network
指令碼的時候,相應的靜態路由是可以自動新增上的,但這時如
果使用
rc.local
的話,在重啟
network
服務的時候,原本新增好的靜態路由就消失了。