Renaming Network Interfaces with Udev Rules

linuxRecently I have noticed something while troubleshooting some network traffic issues. My wireless interface decided to rename itself to an obscure string presumably after an update to either Network Manager or udev. Specifically wlan0 was being renamed to wlp0s22f2u1 when it had previously been wlan0 for as long as I can remember. Digging around, I realized that I no longer had the file: /etc/udev/rules.d/70-persistent-net.rules. This file contains rules that udev will use to name network interfaces, but it was missing. Fortunately it is easy to create or alter if you want to rename network interfaces to stable names.

Let's first grab the hardware address of the problem interface:
ifconfig -a

You will see some output like so:

wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1492
        inet  netmask  broadcast
        inet6 fe80::2c0:caff:fe40:b198  prefixlen 64  scopeid 0x20<link>
        ether 00:01:02:03:04:05  txqueuelen 1000  (Ethernet)
        RX packets 4407  bytes 2748119 (2.6 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 6714  bytes 1423408 (1.3 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Amongst all the noise, we can see from our output of ifconfig that our hardware address is 00:01:02:03:04:05(ether or hwaddr line*). Now as root, create the file 70-persistent-net.rules with your favorite editor in /etc/udev/rules.d:

# vi /etc/udev/rules.d/70-persistent-net.rules

Now enter or copy/paste in this information. Remember, each rule needs to be on it's own line.

# wlan0
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:01:02:03:04:05", ATTR{dev_id}=="0x0", ATTR{type}=="1", KERNEL=="wlan*", NAME="wlan0"

Now you can either reboot or attempt a udev restart. A reboot will ensure rules are reloaded, but you can try some of these commands on various distros:

# udevadm control --reload-rules
# udevadm trigger

Closing thoughts. The system does work perfectly fine without renaming interfaces to stable names. And quite possibly a future udev upgrade may wipe this rule out. But for anyone like me who likes clean interface names or anyone needing to rename interfaces for reasons out of context here, this is one way to do this on many Linux distros.