由于RHEL7中采用新的网卡名称命名方法,导致网卡名变得难以理解和记忆,本文介绍怎样将网卡名称修改为传统的eth0的命名方式,可以实现定义任意网卡为eth0.
要修改网卡名称,首先我们要了解linux网卡设备重命名的过程,根据红帽官方文档介绍,设备重命名的过程如下:
A rule in /usr/lib/udev/rules.d/60-net.rules instructs the udev helper utility,/lib/udev/rename_device,to look into all /etc/sysconfig/networkscripts/ifcfg-suffix files.If it finds an ifcfg file with a HWADDR entry matching the MAC address of an interface it renames the interface to the name given in the ifcfg file by the DEVICE directive.
A rule in /usr/lib/udev/rules.d/71-biosdevname. rules instructs biosdevname to rename the interface according to its naming policy, provided that it was not renamed in a previous step, biosdevname is installed, and biosdevname=0 was not given as a kernel command on the boot command line.
A rule in /lib/udev/rules.d/75-net-description.rules instructs udev to fill in the internal udev device property values ID_NET_NAME_ONBOARD, ID_NET_NAME_SLOT, ID_NET_NAME_PATH, ID_NET_NAME_MAC by examining the network interface device.Note,that some device properties might be undefined.
A rule in /usr/lib/udev/rules.d/80-net-name-slot.rules instructs udev to rename the interface, provided that it was not renamed in step 1 or 2, and the kernel parameter net.ifnames=0 was not given, according to the following priority: ID_NET_NAME_ONBOARD,ID_NET_NAME_SLOT,ID _NET_NAME_PATH. It falls through to the next in the list, if one is unset. If none of these are set, then the interface will not be renamed.
简单翻译如下:
/usr/lib/udev/rules.d/60-net.rules文件中的规则会让udev帮助工具/lib/udev/renamedevice查看所有/etc/sysconfig/networkscripts/ifcfg-suffix文件。如果发现包含HWADDR条目的ifcfg文件与某个接口的MAC地址匹配,它会将该接口重命名为ifcfg文件中由DEVICE指令给出的名称。
/usr/lib/udev/rules.d/71-biosdevname.rules中的规则让biosdevname根据其命名策略重命名该接口,该命名方法执行的前提是在上一步中没有重命名该接口,但是已安装biosdevname、且在boot命令行中将biosdevname=0作为内核命令给出。
/lib/udev/rules.d/75-net-description.rules中的规则让udev通过检查网络接口设备,填写内部udev设备属性值ID_NET_NAME_ONBOARD、ID_NET_NAME_SLOT、ID_NET_NAME_PATH。注:有些设备属性可能处于未定义状态。
/usr/lib/udev/rules.d/80-net-name-slot.rules中的规则让udev重命名该接口,优先顺序如下:ID_NET_NAME_ONBOARD、ID_NET_NAME_SLOT、ID_NET_NAME_PATH。也就是说,没有在步骤1或2中重命名该接口,同时未给出内核参数net.ifnames=0,则会按照该规则进行重命名。按照该规则重命名时,若一个参数未设定,则会按列表的顺序设定下一个。如果没有设定任何参数,则不会重命名该接口。
根据以上介绍,这里可以按照步骤1中的介绍来进行网卡重命名:
ifconfig 命令查看各网卡MAC地址
[root@test01 ~]# ifconfig enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ether 08:00:27:43:c1:1d txqueuelen 1000 (Ethernet) RX packets 233 bytes 43354 (42.3 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 enp0s8: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.56.102 netmask 255.255.255.0 broadcast 192.168.56.255 inet6 fe80::a00:27ff:fe56:9530 prefixlen 64 scopeid 0x20<link> ether 08:00:27:56:95:30 txqueuelen 1000 (Ethernet) RX packets 259 bytes 48117 (46.9 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 50 bytes 8735 (8.5 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 enp0s9: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.56.101 netmask 255.255.255.0 broadcast 192.168.56.255 inet6 fe80::a00:27ff:fea5:4fd prefixlen 64 scopeid 0x20<link> ether 08:00:27:a5:04:fd txqueuelen 1000 (Ethernet) RX packets 1213 bytes 131145 (128.0 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 771 bytes 86683 (84.6 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 0 (Local Loopback) RX packets 1220 bytes 105112 (102.6 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1220 bytes 105112 (102.6 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
由此可知,网卡MAC地址分别为:
enp0s3: 08:00:27:43:c1:1d enp0s8: 08:00:27:56:95:30 enp0s9: 08:00:27:a5:04:fd
这里将要修改网卡名称的对应关系如下:
enp0s3: 08:00:27:43:c1:1d --eth0 enp0s8: 08:00:27:56:95:30 --eth1 enp0s9: 08:00:27:a5:04:fd --eth2 # 备注:可以自定义各网卡名称的对应关系,如将enp0s8修改为eth2,enp0s9修改为eth2.
重命名网卡配置文件
[root@test01 ~]# cd /etc/sysconfig/network-scripts/ [root@test01 network-scripts]# ls ifcfg-* ifcfg-enp0s3 ifcfg-lo [root@test01 network-scripts]# # 如果没有网卡配置文件,可以使用以下命令添加 [root@test01 network-scripts]# nmcli connection add type ethernet ifname enp0s8 con-name enp0s8 autoconnect yes 成功添加的连接 ‘enp0s8‘(71306055-0869-4125-955a-4b7959cc9226)。 [root@test01 network-scripts]# nmcli connection add type ethernet ifname enp0s9 con-name enp0s9 autoconnect yes 成功添加的连接 ‘enp0s9‘(9d5a3033-b478-4076-95b0-83e62aa62cbe)。 [root@test01 network-scripts]# ls ifcfg-* ifcfg-enp0s3 ifcfg-enp0s8 ifcfg-enp0s9 ifcfg-lo [root@test01 network-scripts]# mv ifcfg-enp0s3 ifcfg-eth0 [root@test01 network-scripts]# mv ifcfg-enp0s8 ifcfg-eth1 [root@test01 network-scripts]# mv ifcfg-enp0s9 ifcfg-eth2 [root@test01 network-scripts]# [root@test01 network-scripts]# ls ifcfg-* ifcfg-eth0 ifcfg-eth1 ifcfg-eth2 ifcfg-lo
修改网卡配置文件
修改好配置文件名称后,修改或向配置文件中添加HWADDR,DEVICE,NAME参数。
[root@test01 network-scripts]# cat 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 --这里修改NAME=eth0 UUID=77c7f1b8-ddef-4d96-bf74-68ecedc99a1a DEVICE=eth0 --这里修改DEVICE=eth0 ONBOOT=yes IPADDR=192.168.56.10 PREFIX=24 HWADDR=08:00:27:43:c1:1d --这里添加HWADDR
按照以上方法修改所有网卡配置文件。
拷贝配置文件60-net.rules
[root@test01 network-scripts]# cp /usr/lib/udev/rules.d/60-net.rules /etc/udev/rules.d/
重启系统查看网卡名称
shutdown -r 0
按照以上方法修改,可以自由定义不同MAC地址的网卡对应的网卡名称,在配置文件中添加完HWADDR的前提下,只需要修改配置文件名称ifcfg-ethx和其中的DEVICE=ethx,NAME=ethx.
[root@test01 ~]# ifconfig eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 ether 08:00:27:43:c1:1d txqueuelen 1000 (Ethernet) RX packets 9 bytes 3788 (3.6 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eth1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.56.102 netmask 255.255.255.0 broadcast 192.168.56.255 inet6 fe80::a00:27ff:fe56:9530 prefixlen 64 scopeid 0x20<link> ether 08:00:27:56:95:30 txqueuelen 1000 (Ethernet) RX packets 16 bytes 5825 (5.6 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 21 bytes 4149 (4.0 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eth2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.56.101 netmask 255.255.255.0 broadcast 192.168.56.255 inet6 fe80::a00:27ff:fea5:4fd prefixlen 64 scopeid 0x20<link> ether 08:00:27:a5:04:fd txqueuelen 1000 (Ethernet) RX packets 66 bytes 11712 (11.4 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 63 bytes 10460 (10.2 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
本文出自 “未来人” 博客,请务必保留此出处http://zaa47.blog.51cto.com/6181689/1927440
原文地址:http://zaa47.blog.51cto.com/6181689/1927440