//添加到主机的路由
[root@localhost ~]# route add -host 8.8.8.8 dev ens3 [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 172.25.8.250 0.0.0.0 UG 100 0 0 ens3 8.8.8.8 0.0.0.0 255.255.255.255 UH 0 0 0 ens3 172.25.0.0 0.0.0.0 255.255.0.0 U 100 0 0 ens3 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0 [root@localhost ~]# route add -host 8.8.8.8 gw 8.8.8.8 [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 172.25.8.250 0.0.0.0 UG 100 0 0 ens3 8.8.8.8 8.8.8.8 255.255.255.255 UGH 0 0 0 ens3 8.8.8.8 0.0.0.0 255.255.255.255 UH 0 0 0 ens3 172.25.0.0 0.0.0.0 255.255.0.0 U 100 0 0 ens3 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
//添加到网络的路由 (why)
route add –net 11.11.1.11 netmask 255.255.255.0 dev eth0 route add –net 11.11.1.11 netmask 255.255.255.0 gw 11.11.1.1 route add –net 11.11.1.0/24 dev eth1
//添加默认网关
[root@localhost ~]# route add default gw 172.25.8.250 [root@localhost ~]# route add default gw 172.25.8.251 [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 172.25.8.251 0.0.0.0 UG 0 0 0 ens3 0.0.0.0 172.25.8.250 0.0.0.0 UG 0 0 0 ens3 0.0.0.0 172.25.8.250 0.0.0.0 UG 100 0 0 ens3 8.8.8.8 8.8.8.8 255.255.255.255 UGH 0 0 0 ens3 8.8.8.8 0.0.0.0 255.255.255.255 UH 0 0 0 ens3 172.25.0.0 0.0.0.0 255.255.0.0 U 100 0 0 ens3 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
//删除路由
[root@localhost ~]# route del -host 8.8.8.8 dev ens3 [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 172.25.8.251 0.0.0.0 UG 0 0 0 ens3 0.0.0.0 172.25.8.250 0.0.0.0 UG 0 0 0 ens3 0.0.0.0 172.25.8.250 0.0.0.0 UG 100 0 0 ens3 172.25.0.0 0.0.0.0 255.255.0.0 U 100 0 0 ens3 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
//机器重启或者网卡重启后路由就失效了
[root@localhost ~]# systemctl restart network [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 172.25.8.250 0.0.0.0 UG 100 0 0 ens3 172.25.0.0 0.0.0.0 255.255.0.0 U 100 0 0 ens3 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[root@localhost ~]# ip route add default via 172.25.8.251 dev ens3 [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 172.25.8.251 0.0.0.0 UG 0 0 0 ens3 0.0.0.0 172.25.8.250 0.0.0.0 UG 100 0 0 ens3 172.25.0.0 0.0.0.0 255.255.0.0 U 100 0 0 ens3 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0 [root@localhost ~]# ip route add 172.16.1.0/24 via 172.25.8.251 dev ens3 [root@localhost ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 172.25.8.251 0.0.0.0 UG 0 0 0 ens3 0.0.0.0 172.25.8.250 0.0.0.0 UG 100 0 0 ens3 172.16.1.0 172.25.8.251 255.255.255.0 UG 0 0 0 ens3 172.25.0.0 0.0.0.0 255.255.0.0 U 100 0 0 ens3 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
1)在/etc/sysconfig/network里添加到末尾,是全局生效的。(有效的方法)
GATEWAY=gw-ip或者GATEWAY=gw-dev
2)在/etc/sysconfig/network-script/route-interface下添加路由(每个接口一个文件,如果没有就创建一个,只能添加针对该接口的路由)
例如给eth0添加一个默认网关:
vim /etc/sysconfig/network-scripts/ifcfg-eth0 #添加如下内容(可以省略dev eth0) GATEWAY=X.X.X.X #保存退出后 service network restart或systemctl restart network。