Udev 不会重命名 USB 以太网设备

Udev 不会重命名 USB 以太网设备

我使用的是 Arch Linux、Udev v234、SystemD v234.11-8。

我有一台运行 Arch Linux 的笔记本电脑,还有一部 Nexus 5 Android 手机,它比我的笔记本电脑接收 WiFi 信号更好。我希望当我通过 USB 网络共享插入手机时,我的笔记本电脑能够自动连接到互联网。为了做到这一点,我尝试了一些方法:

  1. 我在以下位置创建了这个(也是唯一的)udev 规则/etc/udev/rules.d/99-nexus-plugged.rules

    #!/bin/sh
    ACTION=="add", ATTRS{idVendor}=="18d1", ATTRS{idProduct}=="4ee2", IMPORT="/lib/udev/rename_netiface %k hello", RUN+="/home/babken/test/adb-enable-tethering"
    

    我希望 udev 将我的设备重命名为hello,但它仍然创建一个随机名称。我究竟做错了什么?

    我知道这条规则是匹配的,因为我的脚本被执行了。

  2. 我创建了 2 个 udev 规则。第一个是通过远程连接到我的手机并启用网络共享来启用 USB 网络共享:

    #!/bin/sh
    SUBSYSTEM=="usb", ATTRS{idVendor}=="18d1", ATTRS{idProduct}=="4ee*",  RUN+="/home/babken/test/adb-enable-tethering"
    

    第二条规则应将以太网名称从随机名称重命名enp0setchello

    #!/bin/sh
    SUBSYSTEM=="net", ACTION=="add", KERNEL=="enp0s29u1u2", ATTR{address}=="6a:e7:28:13:45:f7", NAME="hello", RUN+="/bin/touch /tmp/hello"
    

    第二条规则甚至不会被触发,它不会创建/tmp/hello文件。

    有趣的是,如果我运行udevadm test /sys/class/net/enp0s29u1u2第二条规则,确实会被触发,并且我的设备确实会重命名为hello.但当我拔掉/重新插入手机时就不会了。那为什么这个规则没有触发呢?

我总是sudo udevadm control --reload在更改 udev 规则后运行来重新加载 udev 规则。

这是我插入设备时的 Journalctl 日志:

Aug 25 15:41:44 lenovo-laptop kernel: usb 2-1.2: new high-speed USB device number 38 using ehci-pci
Aug 25 15:41:49 lenovo-laptop kernel: usb 2-1.2: USB disconnect, device number 38
Aug 25 15:41:49 lenovo-laptop kernel: usb 2-1.2: new high-speed USB device number 39 using ehci-pci
Aug 25 15:41:49 lenovo-laptop kernel: rndis_host 2-1.2:1.0 usb0: register 'rndis_host' at usb-0000:00:1d.0-1.2, RNDIS device, c6:d5:df:ba:ba:4d
Aug 25 15:41:50 lenovo-laptop systemd-udevd[20464]: Process '/home/babken/test/adb-enable-tethering' failed with exit code 1.
Aug 25 15:41:50 lenovo-laptop systemd-udevd[20710]: Process '/home/babken/test/adb-enable-tethering' failed with exit code 1.
Aug 25 15:41:50 lenovo-laptop mtp-probe[20778]: checking bus 2, device 39: "/sys/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2"
Aug 25 15:41:50 lenovo-laptop mtp-probe[20778]: bus: 2, device: 39 was not an MTP device
Aug 25 15:41:50 lenovo-laptop systemd-udevd[20464]: link_config: autonegotiation is unset or enabled, the speed and duplex are not writable.
Aug 25 15:41:50 lenovo-laptop kernel: rndis_host 2-1.2:1.0 enp0s29u1u2: renamed from usb0
Aug 25 15:41:50 lenovo-laptop dhcpcd[3516]: enp0s29u1u2: waiting for carrier
Aug 25 15:41:50 lenovo-laptop dhcpcd[3516]: enp0s29u1u2: waiting for carrier
Aug 25 15:41:50 lenovo-laptop dhcpcd[3516]: enp0s29u1u2: carrier acquired
Aug 25 15:41:50 lenovo-laptop dhcpcd[3516]: enp0s29u1u2: IAID 28:13:45:f7
Aug 25 15:41:50 lenovo-laptop dhcpcd[3516]: enp0s29u1u2: adding address fe80::5f59:ada0:7eae:987b
Aug 25 15:41:50 lenovo-laptop dhcpcd[3516]: enp0s29u1u2: soliciting an IPv6 router
Aug 25 15:41:50 lenovo-laptop ifplugd(enp0s29u1u2)[12486]: Link beat detected.
Aug 25 15:41:50 lenovo-laptop dhcpcd[3516]: enp0s29u1u2: rebinding lease of 192.168.42.150
Aug 25 15:41:50 lenovo-laptop dhcpcd[3516]: enp0s29u1u2: probing address 192.168.42.150/24
Aug 25 15:41:51 lenovo-laptop ifplugd(enp0s29u1u2)[12486]: Executing '/etc/ifplugd/ifplugd.action enp0s29u1u2 up'.
Aug 25 15:41:51 lenovo-laptop ifplugd(enp0s29u1u2)[12486]: Program executed successfully.
Aug 25 15:41:56 lenovo-laptop dhcpcd[3516]: enp0s29u1u2: leased 192.168.42.150 for 3600 seconds
Aug 25 15:41:56 lenovo-laptop dhcpcd[3516]: enp0s29u1u2: adding route to 192.168.42.0/24
Aug 25 15:41:56 lenovo-laptop dhcpcd[3516]: enp0s29u1u2: adding default route via 192.168.42.129
Aug 25 15:42:03 lenovo-laptop dhcpcd[3516]: enp0s29u1u2: no IPv6 Routers available

输出udevadm info -a /sys/class/net/enp0s29u1u2

Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.

  looking at device '/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0/net/enp0s29u1u2':
    KERNEL=="enp0s29u1u2"
    SUBSYSTEM=="net"
    DRIVER==""
    ATTR{addr_assign_type}=="3"
    ATTR{addr_len}=="6"
    ATTR{address}=="6a:e7:28:13:45:f7"
    ATTR{broadcast}=="ff:ff:ff:ff:ff:ff"
    ATTR{carrier}=="1"
    ATTR{carrier_changes}=="0"
    ATTR{dev_id}=="0x0"
    ATTR{dev_port}=="0"
    ATTR{dormant}=="0"
    ATTR{flags}=="0x1003"
    ATTR{gro_flush_timeout}=="0"
    ATTR{ifalias}==""
    ATTR{ifindex}=="36"
    ATTR{iflink}=="36"
    ATTR{link_mode}=="0"
    ATTR{mtu}=="1500"
    ATTR{name_assign_type}=="4"
    ATTR{netdev_group}=="0"
    ATTR{operstate}=="unknown"
    ATTR{proto_down}=="0"
    ATTR{tx_queue_len}=="1000"
    ATTR{type}=="1"

  looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2/2-1.2:1.0':
    KERNELS=="2-1.2:1.0"
    SUBSYSTEMS=="usb"
    DRIVERS=="rndis_host"
    ATTRS{authorized}=="1"
    ATTRS{bAlternateSetting}==" 0"
    ATTRS{bInterfaceClass}=="e0"
    ATTRS{bInterfaceNumber}=="00"
    ATTRS{bInterfaceProtocol}=="03"
    ATTRS{bInterfaceSubClass}=="01"
    ATTRS{bNumEndpoints}=="01"
    ATTRS{iad_bFirstInterface}=="00"
    ATTRS{iad_bFunctionClass}=="e0"
    ATTRS{iad_bFunctionProtocol}=="03"
    ATTRS{iad_bFunctionSubClass}=="01"
    ATTRS{iad_bInterfaceCount}=="02"
    ATTRS{interface}=="RNDIS Communications Control"
    ATTRS{supports_autosuspend}=="1"

  looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.2':
    KERNELS=="2-1.2"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
    ATTRS{authorized}=="1"
    ATTRS{avoid_reset_quirk}=="0"
    ATTRS{bConfigurationValue}=="1"
    ATTRS{bDeviceClass}=="ef"
    ATTRS{bDeviceProtocol}=="01"
    ATTRS{bDeviceSubClass}=="02"
    ATTRS{bMaxPacketSize0}=="64"
    ATTRS{bMaxPower}=="500mA"
    ATTRS{bNumConfigurations}=="1"
    ATTRS{bNumInterfaces}==" 3"
    ATTRS{bcdDevice}=="0232"
    ATTRS{bmAttributes}=="80"
    ATTRS{busnum}=="2"
    ATTRS{configuration}==""
    ATTRS{devnum}=="68"
    ATTRS{devpath}=="1.2"
    ATTRS{idProduct}=="4ee4"
    ATTRS{idVendor}=="18d1"
    ATTRS{ltm_capable}=="no"
    ATTRS{manufacturer}=="LGE"
    ATTRS{maxchild}=="0"
    ATTRS{product}=="Nexus 5"
    ATTRS{quirks}=="0x0"
    ATTRS{removable}=="removable"
    ATTRS{serial}=="0831bd3b21320609"
    ATTRS{speed}=="480"
    ATTRS{urbnum}=="685"
    ATTRS{version}==" 2.00"

  looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb2/2-1':
    KERNELS=="2-1"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
    ATTRS{authorized}=="1"
    ATTRS{avoid_reset_quirk}=="0"
    ATTRS{bConfigurationValue}=="1"
    ATTRS{bDeviceClass}=="09"
    ATTRS{bDeviceProtocol}=="01"
    ATTRS{bDeviceSubClass}=="00"
    ATTRS{bMaxPacketSize0}=="64"
    ATTRS{bMaxPower}=="0mA"
    ATTRS{bNumConfigurations}=="1"
    ATTRS{bNumInterfaces}==" 1"
    ATTRS{bcdDevice}=="0000"
    ATTRS{bmAttributes}=="e0"
    ATTRS{busnum}=="2"
    ATTRS{configuration}==""
    ATTRS{devnum}=="2"
    ATTRS{devpath}=="1"
    ATTRS{idProduct}=="0024"
    ATTRS{idVendor}=="8087"
    ATTRS{ltm_capable}=="no"
    ATTRS{maxchild}=="6"
    ATTRS{quirks}=="0x0"
    ATTRS{removable}=="fixed"
    ATTRS{speed}=="480"
    ATTRS{urbnum}=="152578"
    ATTRS{version}==" 2.00"

  looking at parent device '/devices/pci0000:00/0000:00:1d.0/usb2':
    KERNELS=="usb2"
    SUBSYSTEMS=="usb"
    DRIVERS=="usb"
    ATTRS{authorized}=="1"
    ATTRS{authorized_default}=="1"
    ATTRS{avoid_reset_quirk}=="0"
    ATTRS{bConfigurationValue}=="1"
    ATTRS{bDeviceClass}=="09"
    ATTRS{bDeviceProtocol}=="00"
    ATTRS{bDeviceSubClass}=="00"
    ATTRS{bMaxPacketSize0}=="64"
    ATTRS{bMaxPower}=="0mA"
    ATTRS{bNumConfigurations}=="1"
    ATTRS{bNumInterfaces}==" 1"
    ATTRS{bcdDevice}=="0412"
    ATTRS{bmAttributes}=="e0"
    ATTRS{busnum}=="2"
    ATTRS{configuration}==""
    ATTRS{devnum}=="1"
    ATTRS{devpath}=="0"
    ATTRS{idProduct}=="0002"
    ATTRS{idVendor}=="1d6b"
    ATTRS{interface_authorized_default}=="1"
    ATTRS{ltm_capable}=="no"
    ATTRS{manufacturer}=="Linux 4.12.8-2-ARCH ehci_hcd"
    ATTRS{maxchild}=="2"
    ATTRS{product}=="EHCI Host Controller"
    ATTRS{quirks}=="0x0"
    ATTRS{removable}=="unknown"
    ATTRS{serial}=="0000:00:1d.0"
    ATTRS{speed}=="480"
    ATTRS{urbnum}=="27919"
    ATTRS{version}==" 2.00"

  looking at parent device '/devices/pci0000:00/0000:00:1d.0':
    KERNELS=="0000:00:1d.0"
    SUBSYSTEMS=="pci"
    DRIVERS=="ehci-pci"
    ATTRS{broken_parity_status}=="0"
    ATTRS{class}=="0x0c0320"
    ATTRS{companion}==""
    ATTRS{consistent_dma_mask_bits}=="32"
    ATTRS{d3cold_allowed}=="1"
    ATTRS{device}=="0x1c26"
    ATTRS{dma_mask_bits}=="32"
    ATTRS{driver_override}=="(null)"
    ATTRS{enable}=="1"
    ATTRS{irq}=="23"
    ATTRS{local_cpulist}=="0-3"
    ATTRS{local_cpus}=="0f"
    ATTRS{msi_bus}=="1"
    ATTRS{numa_node}=="-1"
    ATTRS{revision}=="0x05"
    ATTRS{subsystem_device}=="0x3975"
    ATTRS{subsystem_vendor}=="0x17aa"
    ATTRS{uframe_periodic_max}=="100"
    ATTRS{vendor}=="0x8086"

  looking at parent device '/devices/pci0000:00':
    KERNELS=="pci0000:00"
    SUBSYSTEMS==""
    DRIVERS==""

答案1

尝试以下规则来完成重命名部分。请随意修改一些内容以使您的脚本运行:

SUBSYSTEM=="net", ACTION=="add", ATTRS{idVendor}=="18d1", ATTRS{idProduct}=="4ee2", NAME="hallo"

这条规则源自一。

如果这不起作用,那么您应该留意系统自动创建的 udev 规则。例如,我的 Debian 系统使用以下规则来管理 wifi 接口(这里引用的规则进行了一些调整,见下文):

SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="c0:f8:da:03:c1:a8", NAME="wlan2"

c0:f8:da:03:c1:a8记下插入的MAC 地址。也许类似的东西可以帮助您创建自定义规则来相应地重命名您的设备。

编辑:上面提供的第二条规则现在基本上被简化为提到的一条这里(汤姆·严在评论中也指出了这一点)。

答案2

请注意,这个答案并不能解决我的 Udev 规则未触发的问题。然而,由于我最初的目标是当我将其插入不同的端口时获得一致的以太网设备名称,因此该解决方案可以完成它的工作。

事实证明 Udev 覆盖了我的配置,所以我只是禁用了它的网络规则:

 # ln -s /dev/null /etc/udev/rules.d/80-net-setup-link.rules

usb0现在我的设备一直被命名。看恢复为传统设备名称

答案3

当我想为我的 FairPhone 配置自动配置时,我遇到了这个问题。这应该仅限于我的 Fairphone,因为我只信任我的设备。所以我只想将我的 Fairphone 提供的网络设备重命名为 ifup/ifdown 可以识别的名称。

上面的建议帮助我让它运行。

有两件事应该牢记在心:

  1. 我的 fairphone 为网络设备生成多个不断变化的 MAC 地址。所以匹配以太网地址根本不起作用。
  2. SUBSYSTEM=="net"仅在 Android 中启用 USB 网络共享并创建网络接口后才会触发。

以下规则似乎对我有用:

SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", SUBSYSTEMS=="usb", DRIVERS=="usb", ATTRS{manufacturer}=="Hersteller", ATTRS{idProduct}=="dead", ATTRS{idVendor}=="beef", ATTRS{serial}=="deadbeef", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="usb*", NAME="mydevname0"

您必须根据您的设置更改字段。

相关内容