Ubuntu – Ubuntu 17.04: how to rename a USB network interface based on Path? (NOT based on MAC)

17.04networkingsystemdudev

No, this ain't a duplicate of Why doesn't my Wi-Fi adapter show up as wlan0 in 16.04?, as this explains how to restore eth0 naming, but not how to individually name a specific interface.

I don't want to assign based on MAC match. All I need is to assign name based on the USB path.

When I plug in an USB network adapter, on Ubuntu 17.04 it gets assigned a network interface name based on its MAC, such as encx000000000000. Now I would like to rename it to something more telling, based on its ID_PATH=pci-000:02:03.0-usb-0:1:1.0. In particular, I don't want to match based on MAC address.
The rationale is that after interface replacement all would break down, but if someone simply replaces network interface hardware so that the new hardware is in the same place as the old one, the system will tuck on without needing IT and reconfiguration support.

So I created a file /etc/systemd/network/50-my-island.link:

[Match]
Path=pci-000:02:03.0-usb-0:1:1.0
[Link]
Name=island0

Unfortunately, the name is never assigned. Using udevadm info I can see that there is ID_NET_NAME=island0 set, but it does not apply. Trying NamePolicy= or NamePolicy=name under the [Link] section doesn't help either.

I'm lost! How can I assign my name in the .link file to my USB network adapter? Is this possible at all without resorting to some udev rule? Why then having .link files at all when they don't seem to work in my case? Where did I make an error?

Best Answer

So I finally figured out myself what really is happening.

Alas, the problem I'm seeing that renaming USB-based network interfaces doesn't work is actually caused by the udev rule /lib/udev/rules.d/73-usb-net-by-mac.rules on Ubuntu/Debian (and thus also Raspbian). The culprit in it is here:

ACTION=="add", SUBSYSTEM=="net", SUBSYSTEMS=="usb", NAME=="", \
  ATTR{address}=="?[014589cd]:*", \
  TEST!="/etc/udev/rules.d/80-net-setup-link.rules", \
  TEST!="/etc/systemd/network/99-default.link", \
    IMPORT{builtin}="net_id", NAME="$env{ID_NET_NAME_MAC}"

Notice how this specific rule checks for /etc/udev/rules.d/80-net-setup-link.rules to be present; if it's not, then NAME will be set to a MAC48-based name, and the later default install rule /lib/udev/rules.d/80-net-setup-link.rules will never get a chance to assign NAME. Now that's sad. Again.

In order to enable the ability to assign user-defined names to USB network interfaces, we need to have /etc/udev/rules.d/80-net-setup-link.rules, as this is the rule set 73-usb-net-by-mac.rules checks against before assigning NAME.

This means that simply linking from /etc/udev/rules.d/80-net-setup-link.rules to /lib/udev/rules.d/80-net-setup-link.rules is needed in order to avoid that user-assigned network interface names get ignored any longer.

sudo ln -s /lib/udev/rules.d/80-net-setup-link.rules /etc/udev/rules.d/80-net-setup-link.rules

Reboot. Done.

Please note that a side-effect of the way 73-usb-net-by-mac.rules is set up, this causes all USB-based network interfaces to assume "old" naming eth0, et cetera, unless explicitly named in a .link file.

I don't know why the rules have been written as they are, as it would be fine to have MAC-based naming for all USB network adapters not explicitly named. On a second thought ... no, using MAC48-based names does not make any sense, unless you happen to label all your USB network dongles and constantly swap them around; but maybe the MAC-based names are used with docking stations, where it actually would make sense...?

Related Question