码迷,mamicode.com
首页 > 其他好文 > 详细

Centos7新增静态路由

时间:2020-02-05 09:17:25      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:group   val   nbsp   网关   emctl   use   adc   编辑   master   

环境说明:

主机名操作系统版本ip默认网关网卡名目的地址1目的地址2
centos7.6 Centos 7.6.1810 172.27.9.135 172.27.9.254 ens33 172.28.105.0/24 172.28.214.17/32

一、临时方式

1. 查看路由和ip

[root@centos7 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.27.9.254    0.0.0.0         UG    100    0        0 ens33
10.244.0.0      10.244.0.0      255.255.255.0   UG    0      0        0 flannel.1
10.244.1.0      0.0.0.0         255.255.255.0   U     0      0        0 cni0
10.244.2.0      10.244.2.0      255.255.255.0   UG    0      0        0 flannel.1
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
172.27.9.0      0.0.0.0         255.255.255.0   U     100    0        0 ens33
[root@centos7 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:7a:68:80 brd ff:ff:ff:ff:ff:ff
    inet 172.27.9.135/24 brd 172.27.9.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe7a:6880/64 scope link 
       valid_lft forever preferred_lft forever
3: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether 02:42:bc:7f:3b:6b brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 brd 172.17.255.255 scope global docker0
       valid_lft forever preferred_lft forever
4: flannel.1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UNKNOWN group default 
    link/ether 96:fb:38:22:16:63 brd ff:ff:ff:ff:ff:ff
    inet 10.244.1.0/32 scope global flannel.1
       valid_lft forever preferred_lft forever
    inet6 fe80::94fb:38ff:fe22:1663/64 scope link 
       valid_lft forever preferred_lft forever
5: cni0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue state UP group default qlen 1000
    link/ether a6:93:72:c1:bc:6e brd ff:ff:ff:ff:ff:ff
    inet 10.244.1.1/24 scope global cni0
       valid_lft forever preferred_lft forever
    inet6 fe80::a493:72ff:fec1:bc6e/64 scope link 
       valid_lft forever preferred_lft forever
6: vethcc8de5ce@if3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc noqueue master cni0 state UP group default 
    link/ether 1e:70:7e:e4:47:c6 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet6 fe80::1c70:7eff:fee4:47c6/64 scope link 
       valid_lft forever preferred_lft forever

2. 新增静态路由

新增到目的地址1的静态路由

[root@centos7 ~]# ip route add 172.28.105.0/24 via 172.27.9.254 dev ens33

 
新增到目的地址2的静态路由

[root@centos7 ~]# ip route add 172.28.214.17/32 via 172.27.9.254 dev ens33

查看路由信息

[root@centos7 ~]# route -n

技术图片

二、永久方式

实现的形式有两种

1. 实现形式1

新增或编辑配置文件route-interfacename,interfacename为网卡名,本文为ens33

[root@centos7 ~]# view /etc/sysconfig/network-scripts/route-ens33
ADDRESS0=172.28.105.0
NETMASK0=255.255.255.0
GATEWAY0=172.27.9.254
ADDRESS1=172.28.214.17
NETMASK1=255.255.255.255
GATEWAY1=172.27.9.254

注意序号为0和1
 
重启网络

[root@centos7 ~]# systemctl restart network

技术图片

2. 实现形式2

[root@centos7 ~]# view /etc/sysconfig/network-scripts/route-ens33 
172.28.105.0/24 via 172.27.9.254 dev ens33
172.28.214.17/32 via 172.27.9.254 dev ens33

重启网络

[root@centos7 ~]# systemctl restart network

技术图片

三、删除静态路由

1. 删除路由

[root@centos7 ~]# ip route del 172.28.105.0/24
[root@centos7 ~]# ip route del 172.28.214.17/32
[root@centos7 ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.27.9.254    0.0.0.0         UG    100    0        0 ens33
10.244.0.0      10.244.0.0      255.255.255.0   UG    0      0        0 flannel.1
10.244.1.0      0.0.0.0         255.255.255.0   U     0      0        0 cni0
10.244.2.0      10.244.2.0      255.255.255.0   UG    0      0        0 flannel.1
172.17.0.0      0.0.0.0         255.255.0.0     U     0      0        0 docker0
172.27.9.0      0.0.0.0         255.255.255.0   U     100    0        0 ens33

该方式会立即删除静态路由

2. 清除配置

rm route-ens33或者echo > route-ens33或者注释该配置文件的路由条目

#ADDRESS0=172.28.105.0
#NETMASK0=255.255.255.0
#GATEWAY0=172.27.9.254
#ADDRESS1=172.28.214.17
#NETMASK1=255.255.255.255
#GATEWAY1=172.27.9.254

下次重启时,静态路由永久删除

四、注意事项

  • 添加永久静态路由方式一中要注意序号,由0开始依次新增,序号不能跳,若只有一条路由则序号为0;
  • 两种永久方式不可以混用,否则配置的路由条目不会完全生效;
  • 通过编辑配置文件/etc/rc.local,开机自启动方式新增静态路由方式不生效;

Centos7新增静态路由

标签:group   val   nbsp   网关   emctl   use   adc   编辑   master   

原文地址:https://www.cnblogs.com/surplus/p/12262436.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!