Red Hat Training

A Red Hat training course is available for RHEL 8

1.6. 使用 udev 规则分配用户定义的网络接口名称

udev 设备管理器支持一组规则来自定义接口名称。

流程

  1. 显示所有网络接口及其 MAC 地址:

    # ip link list
    
    enp6s0f0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
        link/ether b4:96:91:14:ae:58 brd ff:ff:ff:ff:ff:ff
    enp6s0f1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
        link/ether b4:96:91:14:ae:5a brd ff:ff:ff:ff:ff:ff
    enp4s0f0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
        link/ether 00:90:fa:6a:7d:90 brd ff:ff:ff:ff:ff:ff
  2. 创建具有以下内容的文件 /etc/udev/rules.d/70-custom-ifnames.rules

    SUBSYSTEM=="net",ACTION=="add",ATTR{address}=="b4:96:91:14:ae:58",ATTR{type}=="1",NAME="provider0"
    SUBSYSTEM=="net",ACTION=="add",ATTR{address}=="b4:96:91:14:ae:5a",ATTR{type}=="1",NAME="provider1"
    SUBSYSTEM=="net",ACTION=="add",ATTR{address}=="00:90:fa:6a:7d:90",ATTR{type}=="1",NAME="dmz"

    这些规则与网络接口的 MAC 地址相匹配,并将它们重命名为 NAME 属性中指定的名称。在这些示例中,ATTR{type} 参数值 1 定义了接口类型为 Ethernet。

验证

  1. 重启系统:

    # reboot
  2. 验证每个 MAC 地址的接口名称是否与您在规则文件的 NAME 参数中设置的值相匹配:

    # ip link show
    
    provider0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
        link/ether b4:96:91:14:ae:58 brd ff:ff:ff:ff:ff:ff
       altname enp6s0f0
    provider1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
        link/ether b4:96:91:14:ae:5a brd ff:ff:ff:ff:ff:ff
        altname enp6s0f1
    dmz: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
        link/ether 00:90:fa:6a:7d:90 brd ff:ff:ff:ff:ff:ff
        altname enp4s0f0

其他资源

  • udev(7) 手册页
  • udevadm(8) 手册页
  • /usr/src/kernels/<kernel_version>/include/uapi/linux/if_arp.hkernel-doc 软件包提供