Linux – Udev does not rename usb ethernet device

androidarch linuxethernetudevusb device

I'm using Arch Linux, Udev v234, SystemD v234.11-8.

I have a laptop which runs Arch Linux, and a Nexus 5 android phone which catches WiFi signal better than my laptop. I want my laptop to automatically connect to the internet when I plug in the phone through USB tethering. For this to work I have tried a few things:

  1. I have created this (and the only) udev rule in /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"
    

    I expect udev to rename my device to hello, but it still creates a random name. What am I doing wrong?

    I know this rule is matching because my script gets executed.

  2. I have created 2 udev rules. First one is for enabling USB tethering by remoting into my phone and enabling tethering:

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

    Second rule should rename the ethernet name from random name of enp0setc to hello:

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

    This second rule does not even get triggered, it does not create /tmp/hello file.

    Interestingly enough if I run udevadm test /sys/class/net/enp0s29u1u2 this second rule DOES get triggered and my device DOES rename to hello. But not when I unplug/replug my phone. Why does this rule not trigger then?

I always reload udev rules by running sudo udevadm control --reload after making changes to udev rules.

Here are my journalctl logs when I plug in the device:

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

Output of 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==""

Best Answer

Try the following rule to do the renaming part. Feel free to modify things to get your scripts running:

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

This rule is derived from that one.

If that does not work then you should watch out for udev rules that were created by your system automatically. For example, my Debian system uses the following rule to manage the wifi interface (the rule quoted here is a bit tweaked, see below):

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

Note the MAC address c0:f8:da:03:c1:a8 that was inserted. Maybe something similar helps you to create your custom rule that renames your device accordingly.

Edit: Second rule provided above is now essentially cut down to the one mentioned here (as also noted by Tom Yan down in the comments).

Related Question