网上资料都是要修改 /etc/default/grub 文件,增加 net.ifnames=0, biosdevname=0 参数。该方法重启后,网卡名称统一变为eth开头,无法达到任意修改的目的。整理如下方法,亲测 Centos 7.8 可行。

环境信息:CentOS Linux release 7.8.2003 (Core)

网卡信息:

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@renyb: rules.d # 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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:63:bf:af brd ff:ff:ff:ff:ff:ff
inet 192.168.1.110/24 brd 192.168.1.255 scope global noprefixroute dynamic mynet1
valid_lft 6511sec preferred_lft 6511sec
inet6 fe80::a788:f7ed:8c55:1b9f/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 52:54:00:0f:32:90 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
valid_lft forever preferred_lft forever
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
link/ether 52:54:00:0f:32:90 brd ff:ff:ff:ff:ff:ff

root@renyb: rules.d # nmcli connection show
NAME UUID TYPE DEVICE
ens33 b40bd401-b463-4d44-ab63-247307f2a2d6 ethernet ens33
virbr0 7f07fa4e-e769-4fd1-ab39-1b70c27a0952 bridge virbr0

临时配置

通过 ip 命令进行配置,重启失效

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 查看网络设备信息
nmcli connection show

# down网卡
ip link set dev ens33 down

# 修改网卡DEVICE名称(ens33为设备名称)
ip link set dev ens33 name mynet1

# 修改网卡NAME(ens33为网卡名称)
nmcli connection modify ens33 connection.id mynet1

# 启动网卡
ip link set mynet1 up

# 重新加载网络设备信息
nmcli connection reload

永久配置

通过 udev 实现。Centos系统中,规则目录如下:

  • 系统规则:/usr/lib/udev/rules.d/
  • 自定义规则:/etc/udev/rules.d

udev解析

网卡相关udev的rule执行流程:

1
2
3
4
5
6
7
8
9
10
11
12
13
# 用户自定义为优先级最高

# 依据/usr/lib/udev/rules.d/60-net.rules, 查看是否有ifcfg-xx配置文件(路径在/etc/sysconfig/network-scripts/),是否有定义了指定MAC地址的配置文件(ifcfg-xx ,xx必须和配置文件的内容DEVICE一致),如果有,则命名改网卡
/usr/lib/udev/rules.d/60-net.rules

# 依据/usr/lib/udev/rules.d/71-biosdevname.rules,如果biosdevname使能了(安装了biosdevname这个包,且内核启动参数显式设置为1),且网卡没有在step1中定义,则按照biosdevname命名规则rename网卡;(注意,如果没有安装biosdevname这个包,就没有这个文件)
/usr/lib/udev/rules.d/71-biosdevname.rules

# 依据/lib/udev/rules.d/75-net-description.rules,将udev工具会根据device属性将填写网卡的属性命名,可能一个网卡会有多个维度的名称哦
/lib/udev/rules.d/75-net-description.rules

# udev 根据step3中的赋值,按照指定的scheme规则,去给在step1 step2中没有命名的网卡命名
/usr/lib/udev/rules.d/80-net-name-slot.rules

重命名流程

  1. 新建文件:/etc/udev/rules.d/70-persistent-net.rules
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# This is a sample udev rules file that demonstrates how to get udev to
# set the name of IPoIB interfaces to whatever you wish. There is a
# 16 character limit on network device names.
#
# Important items to note: ATTR{type}=="32" is IPoIB interfaces, and the
# ATTR{address} match must start with ?* and only reference the last 8
# bytes of the address or else the address might not match the variable QPN
# portion.
#
# Modern udev is case sensitive and all addresses need to be in lower case.
#
# ACTION=="add", SUBSYSTEM=="net", DRIVERS=="?*", ATTR{type}=="32", ATTR{address}=="?*00:02:c9:03:00:31:78:f2", NAME="mlx4_ib3"

# ATTR{address}: mac address
# NAME: device name
ACTION=="add", SUBSYSTEM=="net", DRIVERS=="?*", ATTR{type}=="1", ATTR{address}=="00:0c:29:63:bf:af", NAME="mynet1"
  1. 修改网卡配置文件:/etc/sysconfig/network-scripts/ifcfg-ens33
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
root@renyb: network-scripts # pwd
/etc/sysconfig/network-scripts
root@renyb: network-scripts # cat ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
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
NAME=mynet1
UUID=b40bd401-b463-4d44-ab63-247307f2a2d6
# 修改网卡DEVICE NAME
DEVICE=mynet1
ONBOOT=yes
IPV6_PRIVACY=no
root@renyb: network-scripts # # 修改网卡配置文件名称
root@renyb: network-scripts # mv ifcfg-ens33 ifcfg-mynet1

参考文档