linux下面可以使用route add命令添加静态路由,但是在网卡重启、系统重启后会丢失,添加永久静态路由的方法如下:

/etc/sysconfig/目录下创建静态路由文件 static-routes,编辑内容如下:

vim /etc/sysconfig/static-routes

any net 172.16.1.0 netmask 255.255.255.0 gw 192.168.0.254
any net 172.16.2.0 netmask 255.255.255.0 gw 192.168.1.254

其中172.16.1.0和172.16.2.0分别为目标网络,后面跟掩码,gw后面跟下一跳地址。

其实以上文件也是利用route命令在添加路由,在/etc/init.d/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

至于这几行就不做多余解释了。

最后修改:2020 年 07 月 03 日
如果觉得我的文章对你有用,请随意赞赏