Centos7添加静态路由

一、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
  • 0
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值