⽤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
grep命令有什么用//添加默认⽹关
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