Network interface names via udev for USB NICs
Do you mean "shuffled" as in "keeps changing", or do you mean it as in "looks like garbage"?
Name stability
The current naming scheme your logs show is based on the MAC address of the network interface, as indicated by the 3rd x
letter in systemd-udev's naming scheme. The MAC address, aka the "hardware address", is just about the most stable identifier that one can expect from a network card (and indeed your USB NICs even use it as their serial number as well).
If you want to assign custom names to interfaces without regard to the order or physical USB port they're connected to, you're ultimately going to map each name to a MAC address.
So if the MAC address keeps changing on every plug-in, well, you're really out of luck. I hope that is not what you meant.
Name format
The MAC-address naming scheme, while present as an option in systemd-udev everywhere, has to be activated manually. In your case, it is activated for USB devices by udev rules installed by the Debian/Ubuntu systemd packaging team.
You could deactivate this, but the default systemd-udev naming scheme was actually based on the physical port number, which would be the opposite of what you're asking for. Deactivating systemd-udev's naming completely would give you simple counter-based names like usb0/usb1/usb2, which are based on the order the USB NICs were connected – which, again, is the opposite of what you're asking for.
Instead you can create custom rules (either udev rules or systemd-udev .link files) that assign exact names based on the interface's MAC address. You can find instructions in /usr/share/doc/udev/README.Debian.gz
, but to summarize:
If using systemd-udev .link files, create /etc/systemd/network/10-en0.link (a separate file for each interface):
[Match] MACAddress=00:24:9b:0d:6d:54 [Link] Name=startech_en0
If using udev rules, create
/etc/udev/rules.d/70-persistent-net.rules
(one file for all rules):ACTION=="add", SUBSYSTEM=="net", DEVTYPE!="?*", ATTR{address}=="00:24:9b:0d:6d:54", NAME="startech_en0"
The ATTR match is case-sensitive and format-sensitive (it is just a string comparison), so it must be provided a lowercase colon-separated address.
The DEVTYPE anti-match is needed to avoid accidentally renaming VLAN interfaces (if they're used), as those have identical MAC addresses to the parent device. I'm not sure if it's the best way,
ENV{ID_BUS}=="?*"
orENV{ID_BUS}=="usb"
might also work.
Related videos on Youtube
Templeton
Updated on September 18, 2022Comments
-
Templeton over 1 year
I have a laptop "server" (client requirement, for some reason) that has 6 USB3 ethernet NICs connected to it. As of late, the laptop has had to go with engineers on road trips, and the USB NICs get removed frequently, and then plugged back in. When Linux (Ubuntu 18.04) starts on this system, the names of the NICs when reported by
ip
/ifconfig
are now "shuffled". When each one is plugged in, we see something like so indmesg
:[7690167.101961] usb 2-5: new SuperSpeed USB device number 4 using xhci_hcd [7690167.125624] usb 2-5: New USB device found, idVendor=0b95, idProduct=1790 [7690167.125634] usb 2-5: New USB device strings: Mfr=1, Product=2, SerialNumber=3 [7690167.125640] usb 2-5: Product: AX88179 [7690167.125646] usb 2-5: Manufacturer: ASIX Elec. Corp. [7690167.125651] usb 2-5: SerialNumber: 0000249B0D6D54 [7690167.478989] ax88179_178a 2-5:1.0 eth0: register 'ax88179_178a' at usb-0000:00:14.0-5, ASIX AX88179 USB 3.0 Gigabit Ethernet, 00:24:9b:0d:6d:54 [7690167.480240] usbcore: registered new interface driver ax88179_178a [7690167.487172] ax88179_178a 2-5:1.0 enx00249b0d6d54: renamed from eth0
Question:
How can I (i.e. via
udev
rules) configure the system to:- Name all of these network interfaces something simpler (i.e. instead of
enx00249b0d6d54
, I would prefer something likestartech_en0
). - "Stabilize" the suffix/numbering, so if I plug in 6x StarTech USB NICs, the interface name reported in
ifconfig
is always the same for a specific USB NIC, even if I insert it into a different USB plug/jack or the NICs are inserted in a different/random order?
- Name all of these network interfaces something simpler (i.e. instead of
-
Templeton about 5 yearsThank you for this so far. I have one more additional quesion: is there a way to instruct
udev
to execute an external script to determine what the interface name should be (i.e. maybe I want to run the default interface name through some sort of parser/hash function to generate the new name)? i.e. pipe the default name through a BASH script to determine what the final name should be? -
user1686 about 5 yearsUse
IMPORT{program}
, see udev(7). But if you're thinking of transforming default names like eth0 (%k), note that the number/index is very unpredictable for USB devices. -
Templeton about 5 years
-
Templeton about 5 yearsWhat should I take into consideration for USB device number/indexes, so I'm not surprised by unexpected numbering?