Centos7系统配置上的变化(三)为网络接口添加多IP

原文 Centos7系统配置上的变化(三)为网络接口添加多IP

    • 实验的方法有 nmtui, 编辑ifcfg-*文件,ip addr 指令,子连接配置文件。
      一、nmtui手工添加IP

      Centos7系统配置上的变化(三)为网络接口添加多IP_第1张图片

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

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

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

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

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

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

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

      01. [root@centos7 ~]# nmcli dev connect enp0s3
      02. [root@centos7 ~]#
      03. [root@centos7 ~]#
      04. [root@centos7 ~]# nmcli -t -f IP4.ADDRESS dev show enp0s3
      05. IP4.ADDRESS[1]:ip = 192.168.150.110/24, gw = 192.168.150.254
      06. IP4.ADDRESS[2]:ip = 192.168.150.111/24, gw = 192.168.150.254
      07. [root@centos7 ~]#
      08. [root@centos7 ~]#
      09. [root@centos7 ~]# ip addr show enp0s3 scope global
      10. 2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
      11. link/ether 08:00:27:15:35:d2 brd ff:ff:ff:ff:ff:ff
      12. inet 192.168.150.110/24 brd 192.168.150.255 scope global enp0s3
      13. valid_lft forever preferred_lft forever
      14. inet 192.168.150.111/24 brd 192.168.150.255 scope global secondary enp0s3
      15. valid_lft forever preferred_lft forever
      16. [root@centos7 ~]#
      17. [root@centos7 ~]#
      18. [root@centos7 ~]# ping 192.168.150.111
      19. PING 192.168.150.111 (192.168.150.111) 56(84) bytes of data.
      20. 64 bytes from 192.168.150.111: icmp_seq=1 ttl=64 time=0.039 ms
      21. 64 bytes from 192.168.150.111: icmp_seq=2 ttl=64 time=0.079 ms
      22. ......
      23. [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:

      01. [root@centos7 ~]# ip addr add 192.168.150.113/24 dev enp0s3
      02. [root@centos7 ~]#
      03. [root@centos7 ~]#
      04. [root@centos7 ~]# nmcli -t -f IP4.ADDRESS dev show enp0s3
      05. IP4.ADDRESS[1]:ip = 192.168.150.110/24, gw = 192.168.150.254
      06. IP4.ADDRESS[2]:ip = 192.168.150.111/24, gw = 192.168.150.254
      07. IP4.ADDRESS[3]:ip = 192.168.150.112/24, gw = 192.168.150.254
      08. IP4.ADDRESS[4]:ip = 192.168.150.113/24, gw = 192.168.150.254
      09. [root@centos7 ~]#
      10. [root@centos7 ~]#
      11. [root@centos7 ~]# ip addr show enp0s3 scope global
      12. 2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
      13. link/ether 08:00:27:15:35:d2 brd ff:ff:ff:ff:ff:ff
      14. inet 192.168.150.110/24 brd 192.168.150.255 scope global enp0s3
      15. valid_lft forever preferred_lft forever
      16. inet 192.168.150.111/24 brd 192.168.150.255 scope global secondary enp0s3
      17. valid_lft forever preferred_lft forever
      18. inet 192.168.150.112/24 brd 192.168.150.255 scope global secondary enp0s3
      19. valid_lft forever preferred_lft forever
      20. inet 192.168.150.113/24 scope global secondary enp0s3
      21. valid_lft forever preferred_lft forever
      22. [root@centos7 ~]#
      23. [root@centos7 ~]#
      24. [root@centos7 ~]# ping 192.168.150.113
      25. PING 192.168.150.113 (192.168.150.113) 56(84) bytes of data.
      26. 64 bytes from 192.168.150.113: icmp_seq=1 ttl=64 time=0.098 ms
      27. 64 bytes from 192.168.150.113: icmp_seq=2 ttl=64 time=0.255 ms
      28. ......

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

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

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

      首先注释掉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

      01. [root@centos7 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
      02. TYPE=Ethernet
      03. BOOTPROTO=none
      04. DEFROUTE=yes
      05. IPV4_FAILURE_FATAL=no
      06. IPV6INIT=no
      07. IPV6_AUTOCONF=yes
      08. IPV6_DEFROUTE=yes
      09. IPV6_PEERDNS=yes
      10. IPV6_PEERROUTES=yes
      11. IPV6_FAILURE_FATAL=no
      12. NAME=eth0
      13. UUID=295e1841-0ff3-4db9-8800-4e25fc2944da
      14. ONBOOT=yes
      15. IPADDR0=192.168.150.110
      16. PREFIX0=24
      17. GATEWAY0=192.168.150.254
      18. DNS1=192.168.150.254
      19. HWADDR=08:00:27:15:35:D2
      20. #IPADDR1=192.168.150.111
      21. #PREFIX1=24
      22. #IPADDR2=192.168.150.112
      23. #PREFIX2=24
      24.  
      25. [root@centos7 ~]#
      26. [root@centos7 ~]#
      27. [root@centos7 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0:1
      28. TYPE=Ethernet
      29. BOOTPROTO=none
      30. DEFROUTE=yes
      31. IPV4_FAILURE_FATAL=no
      32. IPV6INIT=no
      33. IPV6_AUTOCONF=yes
      34. IPV6_DEFROUTE=yes
      35. IPV6_PEERDNS=yes
      36. IPV6_PEERROUTES=yes
      37. IPV6_FAILURE_FATAL=no
      38. NAME=eth0:1
      39. DEVICE=eth0:1
      40. UUID=295e1841-0ff3-4db9-8800-4e25fc2944da
      41. ONBOOT=yes
      42. IPADDR0=192.168.150.119
      43. PREFIX0=24
      44. GATEWAY0=192.168.150.254
      45. DNS1=192.168.150.254
      46. HWADDR=08:00:27:15:35:D2
      47. [root@centos7 ~]#
      48. [root@centos7 ~]# nmcli con reload
      49. [root@centos7 ~]# nmcli dev connect enp0s3
      50. [root@centos7 ~]#
      51. [root@centos7 ~]# nmcli -t -f IP4.ADDRESS dev show enp0s3
      52. IP4.ADDRESS[1]:ip = 192.168.150.110/24, gw = 192.168.150.254
      53. [root@centos7 ~]#
      54. [root@centos7 ~]#
      55. [root@centos7 ~]# ip addr show enp0s3 scope global
      56. 2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
      57. link/ether 08:00:27:15:35:d2 brd ff:ff:ff:ff:ff:ff
      58. inet 192.168.150.110/24 brd 192.168.150.255 scope global eth0:1
      59. valid_lft forever preferred_lft forever
      60. [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

你可能感兴趣的:(centos)