1. 程式人生 > >用route命令添加永久路由

用route命令添加永久路由

route 添加靜態路由

使用雙網卡,同時使用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 netmask 255.255.255.0 eth0

route add –net 192.168.1.11 netmask 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下設置永久路由的方法:
./etc/sysconfig/static-routes :
any net 192.168.3.0/24 gw 192.168.3.254
any net 10.250.228.128 netmask 255.255.255.192 gw 10.250.228.129
使用static-routes的方法是最好的。無論重啟系統和service network restart 都會生效

static-routes文件又是什麽呢,這個是network腳本執行時調用的一個文件,這個文件的放置在/etc/sysconfig目錄下,在network腳本中的位置是:

Add non interface-specific static-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

用route命令添加永久路由