一、ip route显示和设定路由
1、显示路由表
[root@centos7 ~]# ip route show
default via 192.168.150.254 dev enp0s3 proto static metric 1024
192.168.150.0/24 dev enp0s3 proto kernel scope link src 192.168.150.110
太难看了,格式化一下(显示的是默认网关和局域网路由,两行的内容没有共通性):
[root@centos7 tmp]# ip route show|column -t
default via 192.168.150.254 dev enp0s3 proto static metric 1024
192.168.150.0/24 dev enp0s3 proto kernel scope link src 192.168.150.110
2、添加静态路由
[root@centos7 ~]# ip route add 10.15.150.0/24 via 192.168.150.253 dev enp0s3
[root@centos7 ~]#
[root@centos7 ~]# ip route show|column -t
default via 192.168.150.254 dev enp0s3 proto static metric 1024
10.15.150.0/24 via 192.168.150.253 dev enp0s3 proto static metric 1
192.168.150.0/24 dev enp0s3 proto kernel scope link src 192.168.150.110
[root@centos7 ~]#
[root@centos7 ~]# ping 10.15.150.1
PING 10.15.150.1 (10.15.150.1) 56(84) bytes of data.
64 bytes from 10.15.150.1: icmp_seq=1 ttl=63 time=1.77 ms
64 bytes from 10.15.150.1: icmp_seq=1 ttl=63 time=1.08 ms
64 bytes from 10.15.150.1: icmp_seq=1 ttl=63 time=1.57 ms
3、删除静态路由
只需要把 add 替换成 del,或者更简单的只写目标网络
[root@centos7 ~]# ip route del 10.15.150.0/24
二、设置永久的静态路由
1、添加永久静态路由
ip route 指令对路由的修改不能保存,重启就没了。把 ip route 指令写到 /etc/rc.local 也是徒劳的。
RHEL7官网文档没有提到 /etc/sysconfig/static-routes,经测试此文件已经无效;
/etc/sysconfig/network 配置文件仅仅可以提供全局默认网关,语法同 Centos6 一样: GATEWAY=>《ip address》 ;
永久静态路由需要写到 /etc/sysconfig/network-scripts/route-interface 文件中,比如添加两条静态路由:
[root@centos7 ~]# vi /etc/sysconfig/network-scripts/route-enp0s3
10.15.150.0/24 via 192.168.150.253 dev enp0s3
10.25.250.0/24 via 192.168.150.253 dev enp0s3
重启计算机,或者重新启用设备enp0s3才能生效。
[root@centos7 ~]# nmcli dev connect enp0s3
ifdown —关闭网卡设备
例如:
ifdown eth0
ifup —开启网卡设备
例如:
ifup eth0