CentOS 7 单网卡多IP配置
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
CentOS 7 下的网络配置工具
之前在CentOS 6下编辑网卡,直接使用setup工具就可以了。
但在新版的CentOS 7里,setuptool已经没有网络编辑组件了,取而代之的是NetworkManager Text User Interface,即nmtui。
nmtui默认随系统标准安装,如果你使用最小化安装,可能没有该软件,使用下面的命令手动安装:
再次运行nmtui,即可看到网络编辑界面了,这里可以编辑连接、激活新网络连接和设置主机名字,根据实际需要选择即可:
一、nmtui手工添加IP
看一下当前网络设备的IP,为了精简指令输出,指令就要复杂一些了:
view sourceprint?
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 ~]#ipaddr show enp0s3 scope global
05.2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP>mtu 1500 qdiscpfifo_fast state UP qlen 1000
06.link/ether 08:00:27:15:35:d2 brdff: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 。
view sourceprint?
1.[root@centos7 ~]#nmcli con load /etc/sysconfig/network-scripts/ifcfg-eth0
2.[root@centos7 ~]#
3.[root@centos7 ~]#nmcli -t -f IP
4.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 ~]# ipaddr show enp0s3 scope global<br>2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP>mtu 1500 qdiscpfifo_fast state UP qlen 1000<br> link/ether 08:00:27:15:35:d2
brdff: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也没有出现在ipaddr show 中。
view sourceprint?
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 ~]#ipaddr show enp0s3 scope global
08.2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP>mtu 1500 qdiscpfifo_fast state UP qlen 1000
09.link/ether 08:00:27:15:35:d2 brdff: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还是没有生效。
view sourceprint?
01.[root@centos7 ~]#nmclidev 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 ~]#ipaddr show enp0s3 scope global
10.2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP>mtu 1500 qdiscpfifo_fast state UP qlen 1000
11.link/ether 08:00:27:15:35:d2 brdff: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生效了。
注意:不需要先断开设备(nmclidev disconnect enp0s3),断开设备的话ssh立刻断线。
重新连接设备的过程中,ssh是不会断线的。
nmtui添加IP后,需要:
nmcli con load /etc/sysconfig/network-scripts/ifcfg-eth0
nmclidev 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的区别自己脑补一下吧。
)再执行nmclidev connect enp0s3 后,IP 192.168.150.112 才生效的。
手工添加IP到ifcfg-eth0后,需要:
nmcli con load /etc/sysconfig/network-scripts/ifcfg-eth0 或nmcli con reload nmclidev connect enp0s3
三、用ipaddr add指令添加/删除IP,即刻生效,重启不保留。
在线添加IP:
view sourceprint?
01.[root@centos7 ~]#ipaddr 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 ~]#ipaddr show enp0s3 scope global
12.2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP>mtu 1500 qdiscpfifo_fast state UP qlen 1000
13.link/ether 08:00:27:15:35:d2 brdff: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:
view sourceprint?
01.[root@centos7 ~]#ipaddr 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 ~]#ipaddr show enp0s3 scope global
11.2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP>mtu 1500 qdiscpfifo_fast state UP qlen 1000
12.link/ether 08:00:27:15:35:d2 brdff: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关键字已经具有了新的定义,即我们上篇说的“设备”。
参考上篇/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 ,nmclidev con enp0s3
view sourceprint?
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
=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
=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 ~]#nmclidev 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 ~]#ipaddr show enp0s3 scope global
56.2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP>mtu 1500 qdiscpfifo_fast state UP qlen 1000
57.link/ether 08:00:27:15:35:d2 brdff: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确实没有生效。
五、总结
ipaddr show 中显示的IP才是有效的。
ipaddr add 能在线添加IP,立即生效,但重启即丢。
ipaddr del 能在线删除IP,立即生效,但重启即丢。
子连接(ifcfg-*:n)已经无效了。
nmtui和编辑ifcfg-eth0 文件效果基本相同,都不能立即生效,必须load 连接再重新connect网络设备,即:
nmcli con load /etc/sysconfig/network-scripts/ifcfg-eth0 或nmcli reload
nmclidev connect enp0s3。