CentOS 7系统配置上的变化解析(7)

一、nmtui手工添加IP

CentOS 7系统配置上的变化解析

看一下当前网络设备的IP,为了精简指令输出,指令就要复杂一些了:

[root@CentOS7 ~]# nmcli -t -f IP4.ADDRESS dev show enp0s3
IP4.ADDRESS[1]:ip = 192.168.150.110/24, gw = 192.168.150.254
[root@centos7 ~]#
[root@centos7 ~]# ip addr show enp0s3 scope global
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:15:35:d2 brd ff:ff:ff:ff:ff:ff
    inet 192.168.150.110/24 brd 192.168.150.255 scope global enp0s3
      valid_lft forever preferred_lft forever
[root@centos7 ~]#
[root@centos7 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
......
IPADDR0=192.168.150.110
PREFIX0=24
GATEWAY0=192.168.150.254
DNS1=192.168.150.254
HWADDR=08:00:27:15:35:D2
IPADDR1=192.168.150.111
PREFIX1=24
[root@centos7 ~]#

新IP已经被加入到ifcfg-eth0中,原始地址的关键字是 IPADDR0、PREFIX0,新地址的关键字是 IPADDR1、PREFIX1 。

[root@centos7 ~]# nmcli con load /etc/sysconfig/network-scripts/ifcfg-eth0
[root@centos7 ~]#
[root@centos7 ~]# nmcli -t -f IP4.ADDRESS dev show enp0s3
IP4.ADDRESS[1]:ip = 192.168.150.110/24, gw = 192.168.150.254
[root@centos7 ~]#
[root@centos7 ~]# ip addr show enp0s3 scope global
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:15:35:d2 brd ff:ff:ff:ff:ff:ff
    inet 192.168.150.110/24 brd 192.168.150.255 scope global enp0s3
      valid_lft forever preferred_lft forever
[root@centos7 ~]#

load connection 后,NetworkManager 也没有知晓新增IP,新增IP也没有出现在 ip addr show 中。

[root@centos7 ~]# systemctl restart network
[root@centos7 ~]#
[root@centos7 ~]# nmcli -t -f IP4.ADDRESS dev show enp0s3
IP4.ADDRESS[1]:ip = 192.168.150.110/24, gw = 192.168.150.254
IP4.ADDRESS[2]:ip = 192.168.150.111/24, gw = 192.168.150.254
[root@centos7 ~]#
[root@centos7 ~]# ip addr show enp0s3 scope global
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:15:35:d2 brd ff:ff:ff:ff:ff:ff
    inet 192.168.150.110/24 brd 192.168.150.255 scope global enp0s3
      valid_lft forever preferred_lft forever
[root@centos7 ~]#
[root@centos7 ~]#
[root@centos7 ~]# ping 192.168.150.111
PING 192.168.150.111 (192.168.150.111) 56(84) bytes of data.
From 192.168.150.110 icmp_seq=1 Destination Host Unreachable
From 192.168.150.110 icmp_seq=2 Destination Host Unreachable
......
[root@centos7 ~]#

重启network服务后,NetworkManager 知晓新增IP了,但新增IP还是没有生效。

[root@centos7 ~]# nmcli dev connect enp0s3
[root@centos7 ~]#
[root@centos7 ~]#
[root@centos7 ~]# nmcli -t -f IP4.ADDRESS dev show enp0s3
IP4.ADDRESS[1]:ip = 192.168.150.110/24, gw = 192.168.150.254
IP4.ADDRESS[2]:ip = 192.168.150.111/24, gw = 192.168.150.254
[root@centos7 ~]#
[root@centos7 ~]#
[root@centos7 ~]# ip addr show enp0s3 scope global
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:15:35:d2 brd ff:ff:ff:ff:ff:ff
    inet 192.168.150.110/24 brd 192.168.150.255 scope global enp0s3
      valid_lft forever preferred_lft forever
    inet 192.168.150.111/24 brd 192.168.150.255 scope global secondary enp0s3
      valid_lft forever preferred_lft forever
[root@centos7 ~]#
[root@centos7 ~]#
[root@centos7 ~]# ping 192.168.150.111
PING 192.168.150.111 (192.168.150.111) 56(84) bytes of data.
64 bytes from 192.168.150.111: icmp_seq=1 ttl=64 time=0.039 ms
64 bytes from 192.168.150.111: icmp_seq=2 ttl=64 time=0.079 ms
......
[root@centos7 ~]#

重新连接设备 enp0s3,新IP生效了。注意:不需要先断开设备(nmcli dev disconnect enp0s3),断开设备的话ssh立刻断线。重新连接设备的过程中,ssh是不会断线的。

nmtui添加IP后,需要:
nmcli con load /etc/sysconfig/network-scripts/ifcfg-eth0
nmcli dev connect enp0s3

二、手工添加IP到ifcfg-eth0

在ifcfg-eth0配置文件中手工添加如下两行:
IPADDR2=192.168.150.112
PREFIX2=24

先执行 nmcli con load /etc/sysconfig/network-scripts/ifcfg-eth0 后,在nmtui中可以看到新增IP。(也可以简单的用 nmcli con reload ,reload 和 load的区别自己脑补一下吧。)
再执行 nmcli dev connect enp0s3 后,IP 192.168.150.112 才生效的。

手工添加IP到ifcfg-eth0后,需要:
nmcli con load /etc/sysconfig/network-scripts/ifcfg-eth0 或 nmcli con reload
nmcli dev connect enp0s3

三、用ip addr add指令添加/删除IP,即刻生效,重启不保留。

在线添加IP:

[root@centos7 ~]# ip addr add 192.168.150.113/24 dev enp0s3
[root@centos7 ~]#
[root@centos7 ~]#
[root@centos7 ~]# nmcli -t -f IP4.ADDRESS dev show enp0s3
IP4.ADDRESS[1]:ip = 192.168.150.110/24, gw = 192.168.150.254
IP4.ADDRESS[2]:ip = 192.168.150.111/24, gw = 192.168.150.254
IP4.ADDRESS[3]:ip = 192.168.150.112/24, gw = 192.168.150.254
IP4.ADDRESS[4]:ip = 192.168.150.113/24, gw = 192.168.150.254
[root@centos7 ~]#
[root@centos7 ~]#
[root@centos7 ~]# ip addr show enp0s3 scope global
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:15:35:d2 brd ff:ff:ff:ff:ff:ff
    inet 192.168.150.110/24 brd 192.168.150.255 scope global enp0s3
      valid_lft forever preferred_lft forever
    inet 192.168.150.111/24 brd 192.168.150.255 scope global secondary enp0s3
      valid_lft forever preferred_lft forever
    inet 192.168.150.112/24 brd 192.168.150.255 scope global secondary enp0s3
      valid_lft forever preferred_lft forever
    inet 192.168.150.113/24 scope global secondary enp0s3
      valid_lft forever preferred_lft forever
[root@centos7 ~]#
[root@centos7 ~]#
[root@centos7 ~]# ping 192.168.150.113
PING 192.168.150.113 (192.168.150.113) 56(84) bytes of data.
64 bytes from 192.168.150.113: icmp_seq=1 ttl=64 time=0.098 ms
64 bytes from 192.168.150.113: icmp_seq=2 ttl=64 time=0.255 ms
......

/etc/sysconfig/network-scripts/ifcfg-eth0 配置文件里并没有192.168.150.113,所以重启后该IP不会保留。

在线删除IP:

[root@centos7 ~]# ip addr del 192.168.150.113/24 dev enp0s3
[root@centos7 ~]#
[root@centos7 ~]#
[root@centos7 ~]# nmcli -t -f IP4.ADDRESS dev show enp0s3
IP4.ADDRESS[1]:ip = 192.168.150.110/24, gw = 192.168.150.254
IP4.ADDRESS[2]:ip = 192.168.150.111/24, gw = 192.168.150.254
IP4.ADDRESS[3]:ip = 192.168.150.112/24, gw = 192.168.150.254
[root@centos7 ~]#
[root@centos7 ~]#
[root@centos7 ~]# ip addr show enp0s3 scope global
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:15:35:d2 brd ff:ff:ff:ff:ff:ff
    inet 192.168.150.110/24 brd 192.168.150.255 scope global enp0s3
      valid_lft forever preferred_lft forever
    inet 192.168.150.111/24 brd 192.168.150.255 scope global secondary enp0s3
      valid_lft forever preferred_lft forever
    inet 192.168.150.112/24 brd 192.168.150.255 scope global secondary enp0s3
      valid_lft forever preferred_lft forever

四、使用子连接配置文件 ifcfg-*:n

子连接配置文件是 Centos6 常用的方法,即:如果当前网卡配置文件为 ifcfg-eth0,则子连接配置文件可以为 ifcfg-eth0:1,并且文件中需要写明 DEVICE=eth0:1 , IPADDR=<新IP地址> 。
我们试试在Centos7 中是否也支持。理论上应该是不支持的,因为在Centos7的网络连接配置文件(即 ifcfg-*)中,DEVICE关键字已经具有了新的定义,即我们上篇说的“设备”。参考前面几页。

首先注释掉ifcfg-eth0中的其他地址,复制 ifcfg-eth0 为 ifcfg-eth0:1,编辑ifcfg-eth0:1, DEVICE 设为eth0:1 ,IP设为 192.168.150.119 ,然后 nmcli con reload , nmcli dev con enp0s3


复制代码
[root@centos7 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=no
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eth0
UUID=295e1841-0ff3-4db9-8800-4e25fc2944da
ONBOOT=yes
IPADDR0=192.168.150.110
PREFIX0=24
GATEWAY0=192.168.150.254
DNS1=192.168.150.254
HWADDR=08:00:27:15:35:D2
#IPADDR1=192.168.150.111
#PREFIX1=24
#IPADDR2=192.168.150.112
#PREFIX2=24

[root@centos7 ~]#
[root@centos7 ~]#
[root@centos7 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0:1
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=no
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eth0:1
DEVICE=eth0:1
UUID=295e1841-0ff3-4db9-8800-4e25fc2944da
ONBOOT=yes
IPADDR0=192.168.150.119
PREFIX0=24
GATEWAY0=192.168.150.254
DNS1=192.168.150.254
HWADDR=08:00:27:15:35:D2
[root@centos7 ~]#
[root@centos7 ~]# nmcli con reload
[root@centos7 ~]# nmcli dev connect enp0s3
[root@centos7 ~]#
[root@centos7 ~]# nmcli -t -f IP4.ADDRESS dev show enp0s3
IP4.ADDRESS[1]:ip = 192.168.150.110/24, gw = 192.168.150.254
[root@centos7 ~]#
[root@centos7 ~]#
[root@centos7 ~]# ip addr show enp0s3 scope global
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 08:00:27:15:35:d2 brd ff:ff:ff:ff:ff:ff
    inet 192.168.150.110/24 brd 192.168.150.255 scope global eth0:1
      valid_lft forever preferred_lft forever
[root@centos7 ~]#

可以看到ifcfg-eth0:1里的新IP确实没有生效。

五、总结
•ip addr show 中显示的IP才是有效的。
•ip addr add 能在线添加IP,立即生效,但重启即丢。
•ip addr del 能在线删除IP,立即生效,但重启即丢。
•子连接(ifcfg-*:n)已经无效了。
•nmtui和编辑ifcfg-eth0 文件效果基本相同,��不能立即生效,必须 load 连接再重新connect网络设备,即:
nmcli con load /etc/sysconfig/network-scripts/ifcfg-eth0 或 nmcli reload
nmcli dev connect enp0s3

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/17456.html