Linux系统添加永久静态路由的方法(包含Centos7)

一、使用route命令添加

使用route 命令添加的路由,机器重启或者网卡重启后路由就失效了,方法:
A、添加到主机的路由
# route add –host 192.168.1.10 dev eth0
# route add –host 192.168.1.10 gw 192.168.1.1
B、添加到网络的路由
# route add –net 192.168.1.0  netmask 255.255.255.0 eth0
# route add –net 192.168.1.0  netmask 255.255.255.0 gw 192.168.1.1
# route add –net 192.168.1.0/24 eth1
C、添加默认网关
# route add default gw 192.168.1.1
D、删除路由
# route del –host 192.168.1.10 dev eth0

 

二、Centos7 添加永久路由(需要重启网络) 

ip route 指令对路由的修改不能保存,重启就没了。把 ip route 指令写到 /etc/rc.local 也是徒劳的。

RHEL7官网文档没有提到 /etc/sysconfig/static-routes,经测试此文件已经无效;

/etc/sysconfig/network 配置文件仅仅可以提供全局默认网关,语法同 Centos6 一样: GATEWAY=<ip address> ;

在 /etc/sysconfig/network-scripts/下,新建文件名为route-*interfacename*的文件(interfacename为你的网卡名称)

永久静态路由需要写到 /etc/sysconfig/network-scripts/route-*interfacename* 文件中

比如添加一条静态路由:(网卡的设备名叫eth1)

[root@centos7 ~]# vi /etc/sysconfig/network-scripts/route-eth1
192.168.0.0/16 via 172.16.0.1 dev eth1

wq保存后,service network restart重启网络服务

原文地址:https://www.cnblogs.com/yizhipanghu/p/10529865.html