|
| 1 | ++++ |
| 2 | +title = "Renaming Linux Network Interfaces" |
| 3 | +date = "2024-03-18" |
| 4 | + |
| 5 | +#[taxonomies] |
| 6 | +#tags = ["linux", "udev"] |
| 7 | ++++ |
| 8 | + |
| 9 | +## Motivation |
| 10 | + |
| 11 | +**NOTE:** This assumes you have root privileges on the machine you will configure. |
| 12 | + |
| 13 | +Depending on the machine and Linux distribution, different network interfaces can and will appear with different names. For example, `eth0`, `eno1`, `ens1`, `enp1s0`, and `enx0c9863d1a379` are all [valid Ethernet interface names](https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/networking_guide/ch-consistent_network_device_naming). Whether you like consistency or just want to give your interfaces more meaningful names, `udev` makes renaming network interfaces straightforward. |
| 14 | + |
| 15 | +This guide generally targets WiFi and Ethernet interfaces, although concepts will apply to other interface types as well (except Bluetooth). It also assumes no pre-requisite software beyond a text editor, although `ethtool` may be useful. |
| 16 | + |
| 17 | +## Before Getting Started |
| 18 | + |
| 19 | +**Proceed with caution.** It's always a good idea to have physical access to the system when performing networking-related or system-level changes. This is especially true for static configurations which largely rely on the interface name. It's possible that changes made here will disable networking. |
| 20 | + |
| 21 | +If you're unfamiliar with using or configuring networking on Linux, I encourage you to familiarize yourself with the [Querying Network Information](@/blog/linux-cmds.md#querying-network-information) and [Managing Networking (Network Manager)](@/blog/linux-cmds.md#managing-networking-network-manager) sections of my Linux command cheatsheet. |
| 22 | + |
| 23 | +## Instructions |
| 24 | + |
| 25 | +### 1\. Identify Network Interface MAC Addresses |
| 26 | + |
| 27 | +Before renaming network interfaces, we'll need the MAC address for each network interface to rename. MAC addresses for non-Bluetooth devices will appear in the output of `ip link show` (or `ip -br link show` for less verbose output). |
| 28 | + |
| 29 | +If your system has easily identifiable interfaces, you can move to the next step. However, should your system contain multiple, harder to identify interfaces of the same type, read on. |
| 30 | + |
| 31 | +#### Uh-Oh You have Ten of the Same NIC |
| 32 | + |
| 33 | +There are a couple different ways to identify specific interfaces. Generally, for wired interfaces the `ethtool` command's `-p` option identifies interfaces by blinking the port's LED (when run as root). Wireless interfaces often times list their MAC address on a sticker on the RF shield (silver casing on the chip's PCB), often including both WiFi and Bluetooth when implemented on the same NIC. |
| 34 | + |
| 35 | +Should neither of these be an option, `ip -d link show` lists the interface's PCI(e) parent device which can then be cross-referenced to the output of `lspci`, should you know the specific interface brand and model you're looking for. |
| 36 | + |
| 37 | +### 2\. Create the udev Rule |
| 38 | + |
| 39 | +Similar to my post on [configuring USB serial ports details](@/blog/usb-serial.md#3-create-the-udev-rule) details, we'll now create a udev rule to rename the device. |
| 40 | + |
| 41 | +#### udev Background |
| 42 | + |
| 43 | +The tl;dr of udev rules for the unfamiliar is that `udev` is a daemon program that runs on many Linux distributions to manage and configure devices. |
| 44 | + |
| 45 | +When configuring a device, be it a network interface or otherwise, `udev` iterates through the rules available on the system pattern matching. Should the rule match, `udev` will perform an action. These actions vary from renaming the network interface to running a custom script. |
| 46 | + |
| 47 | +Distributions generally come with a number of pre-configured rules ready to use on first boot. It's generally a good idea to not touch these, though. On my Ubuntu and Fedora machines, these are available in `/usr/lib/udev/rules.d/` and `/usr/local/lib/udev/rules.d/`. |
| 48 | + |
| 49 | +#### Configuring the udev Rule |
| 50 | + |
| 51 | +With your MAC addresses and desired network interface names handy, create a rule in `/etc/udev/rules.d` of the format `XX-name.rule`, where `XX` is some positive number. Note that the higher the number here, the lower the priority of the rule when `udev` attempts to pattern match. |
| 52 | + |
| 53 | +On my systems, I use `70`, as this comes before most of the pre-configured `udev` network interface configuration rules (ensuring the name I configure isn't ignored and doesn't potentially cause issues later). |
| 54 | + |
| 55 | +For example, here is a rule I have on a multi-Ethernet port system I use as my home router. It relabels the Ethernet ports from the strange, hardware-based naming scheme (e.g. formats `enpXs0` and `enoX` in a non-intuitive ordering) to names that reflect how they're used. Note that each line is a separate rule. |
| 56 | + |
| 57 | +``` |
| 58 | +# /etc/udev/rules.d/70-rename-ethernet-ports.rules |
| 59 | +
|
| 60 | +# Single WAN port (leftmost port, labeled WAN1) |
| 61 | +SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="xx:xx:xx:xx:xx:xx", NAME="wan1" |
| 62 | +
|
| 63 | +# Three LAN port (right three ports, labeled LAN1, LAN2, LAN3) |
| 64 | +SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="xx:xx:xx:xx:xx:xx", NAME="lan1" |
| 65 | +SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="xx:xx:xx:xx:xx:xx", NAME="lan2" |
| 66 | +SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="xx:xx:xx:xx:xx:xx", NAME="lan3" |
| 67 | +``` |
| 68 | + |
| 69 | +To use a similar rule on your system, substitute in the MAC address for the quoted address listed after the `ATTR{address}==` section and your desired interface name for the quoted name that comes after `NAME=`. |
| 70 | + |
| 71 | +### 3\. Reboot and Validate Names Changed |
| 72 | + |
| 73 | +Unlike USB serial devices, reloading udev rules won't necessarily work to rename the interfaces. A reboot is basically required, especially given networking configuration that typically only happens on startup. |
0 commit comments