解决Vmware中为CentOS添加网卡后丢失配置文件

故障描述

最近使用 VMware 为已经创建的 CentOS7 添加多张网卡时,发现虽然成功添加了 eth1 网卡并通过 DHCP 获取了IP地址,但是在 /etc/sysconfig/network-scripts/ 目录下却看不到对应的网卡文件 ifcfg-eth1 。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
[root@localhost network-scripts]# 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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:80:69:1f brd ff:ff:ff:ff:ff:ff
inet 192.168.245.129/24 brd 192.168.245.255 scope global noprefixroute dynamic eth0
valid_lft 1491sec preferred_lft 1491sec
inet6 fe80::1c28:54a4:1513:c8ee/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:80:69:29 brd ff:ff:ff:ff:ff:ff
inet 192.168.245.131/24 brd 192.168.245.255 scope global noprefixroute eth1
valid_lft forever preferred_lft forever
inet6 fe80::cae2:5268:7768:d61e/64 scope link noprefixroute
valid_lft forever preferred_lft forever

[root@localhost network-scripts]# ll
total 261
-rw-r--r--. 1 root root 324 Aug 14 2019 ifcfg-eth0 # eth0网卡的配置文件
-rw-r--r--. 1 root root 254 Aug 24 2018 ifcfg-lo
lrwxrwxrwx. 1 root root 24 Aug 14 2019 ifdown -> ../../../usr/sbin/ifdown
-rwxr-xr-x. 1 root root 654 Aug 24 2018 ifdown-bnep
-rwxr-xr-x. 1 root root 6532 Aug 24 2018 ifdown-eth
-rwxr-xr-x. 1 root root 6190 Oct 31 2018 ifdown-ib
-rwxr-xr-x. 1 root root 781 Aug 24 2018 ifdown-ippp
-rwxr-xr-x. 1 root root 4540 Aug 24 2018 ifdown-ipv6
lrwxrwxrwx. 1 root root 11 Aug 14 2019 ifdown-isdn -> ifdown-ippp
-rwxr-xr-x. 1 root root 2130 Aug 24 2018 ifdown-post
-rwxr-xr-x. 1 root root 1068 Aug 24 2018 ifdown-ppp
-rwxr-xr-x. 1 root root 870 Aug 24 2018 ifdown-routes
-rwxr-xr-x. 1 root root 1456 Aug 24 2018 ifdown-sit
-rwxr-xr-x. 1 root root 1621 Mar 18 2017 ifdown-Team
-rwxr-xr-x. 1 root root 1556 Mar 18 2017 ifdown-TeamPort
-rwxr-xr-x. 1 root root 1462 Aug 24 2018 ifdown-tunnel
lrwxrwxrwx. 1 root root 22 Aug 14 2019 ifup -> ../../../usr/sbin/ifup
-rwxr-xr-x. 1 root root 12415 Aug 24 2018 ifup-aliases
-rwxr-xr-x. 1 root root 910 Aug 24 2018 ifup-bnep
-rwxr-xr-x. 1 root root 13475 Aug 24 2018 ifup-eth
-rwxr-xr-x. 1 root root 10114 Oct 31 2018 ifup-ib
-rwxr-xr-x. 1 root root 12075 Aug 24 2018 ifup-ippp
-rwxr-xr-x. 1 root root 11893 Aug 24 2018 ifup-ipv6
lrwxrwxrwx. 1 root root 9 Aug 14 2019 ifup-isdn -> ifup-ippp
-rwxr-xr-x. 1 root root 650 Aug 24 2018 ifup-plip
-rwxr-xr-x. 1 root root 1064 Aug 24 2018 ifup-plusb
-rwxr-xr-x. 1 root root 4997 Aug 24 2018 ifup-post
-rwxr-xr-x. 1 root root 4154 Aug 24 2018 ifup-ppp
-rwxr-xr-x. 1 root root 2001 Aug 24 2018 ifup-routes
-rwxr-xr-x. 1 root root 3303 Aug 24 2018 ifup-sit
-rwxr-xr-x. 1 root root 1755 Mar 18 2017 ifup-Team
-rwxr-xr-x. 1 root root 1876 Mar 18 2017 ifup-TeamPort
-rwxr-xr-x. 1 root root 2711 Aug 24 2018 ifup-tunnel
-rwxr-xr-x. 1 root root 1836 Aug 24 2018 ifup-wireless
-rwxr-xr-x. 1 root root 5419 Aug 24 2018 init.ipv6-global
-rw-r--r--. 1 root root 20671 Aug 24 2018 network-functions
-rw-r--r--. 1 root root 31027 Aug 24 2018 network-functions-ipv6

解决方法

此时需要自己手动添加网卡配置文件,步骤如下:

使用 nmcli con show 命令查看 eht1 网卡的UUID信息并记录,后续会用到

1
2
3
4
[root@localhost network-scripts]# nmcli con show 
NAME UUID TYPE DEVICE
eth0 0212fe99-0ea0-49bc-a44a-b63275101e6d ethernet eth0
eth1 7eb403e4-057f-3823-a6c6-6f89c24e6ebf ethernet eth1 # 记下该项 UUID 值

使用 ip addr 命令查看网卡信息,记录 eth1 的MAC地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@localhost network-scripts]# 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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:80:69:1f brd ff:ff:ff:ff:ff:ff
inet 192.168.245.129/24 brd 192.168.245.255 scope global noprefixroute dynamic eth0
valid_lft 1491sec preferred_lft 1491sec
inet6 fe80::1c28:54a4:1513:c8ee/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:80:69:29 brd ff:ff:ff:ff:ff:ff # 记下eth1的MAC地址
inet 192.168.245.131/24 brd 192.168.245.255 scope global noprefixroute eth1
valid_lft forever preferred_lft forever
inet6 fe80::cae2:5268:7768:d61e/64 scope link noprefixroute
valid_lft forever preferred_lft forever

添加新的网卡配置文件,这里复制并修改了 eth0 的网卡文件

注:新的网卡配置文件需要使用静态地址,如果使用dhcp可能会出现报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@localhost network-scripts]# cp ifcfg-eth0 ifcfg-eth1
[root@localhost network-scripts]# vi ifcfg-eth1
TYPE=Ethernet
BOOTPROTO=static # 修改为固定IP地址
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=eth1 # 修改网卡名称
UUID=7eb403e4-057f-3823-a6c6-6f89c24e6ebf # 修改网卡UUID值(唯一标识符)
DEVICE=eth1 # 修改系统逻辑设备名
ONBOOT=yes
PEERDNS=yes
PEERROUTES=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_PRIVACY=no
IPADDR=192.168.245.131 # 指定IP地址
GATEWAY=192.168.245.2 # 指定网关
NETMASK=255.255.255.0 # 指定子网掩码
HWADDR=00:0c:29:80:69:29 # 增加该行,指定网卡MAC地址

最后重启网网络服务即可。

1
[root@localhost network-scripts]# systemctl restart network