Set a Stable Network Interface Name with systemd.link
A kernel-assigned interface name like enp0s31f6 can change after a reboot, and systemd.link is how you pin one down for good. You will create a .link file that matches a network device by its MAC address and gives it a predictable name such as lan0, inspect the rule before it goes live, then apply it the safe way. Allow about fifteen minutes. You need root access to write under /etc/systemd/network, and console access if the interface carries your current connection.
The route
Jump straight to the step you need, or tick off Done means at the end.
The examples below match the systemd 255 interface installed here, package version 255.4-1ubuntu8.17. Replace AA:BB:CC:DD:EE:FF with the real address of your device and lan0 with a name that is unique on your host.
1. Identify the device and its current address
Start with read-only commands, so you never match an interface by a kernel name that may change after a reboot:
$ ip -br link
$ ip link show enp0s31f6
Use the interface name from your own output. The link/ether value is the current MAC address. If something else deliberately changes that address, match on a property that describes the device reliably instead, such as PermanentMACAddress=, Path=, or Driver=. OriginalName= matches the udev interface name, but the manpage warns that kernel-assigned names are not stable between reboots.
Checkpoint
Record the target MAC address, then check that the proposed name is not already in use.
$ ip link show lan0
Device "lan0" does not exist.
Your diagnostic wording may differ. If lan0 already exists, pick another name rather than relying on a race between two interfaces.
2. Create an early matching rule
Local link files belong in /etc/systemd/network. The first matching file in alphanumeric order wins, and the shipped 99-default.link would otherwise be considered first, so give your file a numeric prefix below 70:
$ sudo install -d -m 0755 /etc/systemd/network
$ sudoedit /etc/systemd/network/10-lan.link
Enter this configuration, replacing the placeholder MAC address:
[Match]
MACAddress=AA:BB:CC:DD:EE:FF
[Link]
Name=lan0
[Match]selects the device. Every setting in that section must succeed.[Link]describes the change. Because this file does not setNamePolicy=, the explicitName=lan0can take effect.- Interface names must be 1 to 15 characters. They cannot be fully numeric and must not contain
:,/, or%. Avoid names such aseth0, which can collide with a kernel-assigned name.
This is a persistent configuration change, though saving the file alone does not apply it. Keep a second session or local console available before you touch a live network interface.
3. Check what udev would apply
Test the installed net_setup_link builtin against the interface's current sysfs path. This is a read-only diagnostic and does not rename the device:
$ sudo SYSTEMD_LOG_LEVEL=debug udevadm test-builtin net_setup_link /sys/class/net/INTERFACE
...
Replace INTERFACE with the current name, for example enp0s31f6. Look for lines like these in the output:
Config file /etc/systemd/network/10-lan.link is applied
ID_NET_LINK_FILE=/etc/systemd/network/10-lan.link
ID_NET_NAME=lan0
The debug text carries far more hardware and naming detail than this excerpt, and the wording can change between versions. The useful checkpoint is the path to your file and the proposed ID_NET_NAME. If /usr/lib/systemd/network/99-default.link shows up as the applied file instead, your rule did not match, or it was ordered too late. Check the MAC spelling, the current interface path, and the numeric filename prefix.
4. Apply the rule during a maintenance window
Saving a .link file does not retroactively rename an existing device. Reload udev's rules first:
$ sudo udevadm control --reload
Warning
Applying a link rule can rename the interface, interrupt traffic, and confuse whatever service manages its addresses. Stop or pause that service if your host needs it. Then, only with console access or an out-of-band session, use the documented reapplication sequence:
$ sudo ip link set INTERFACE down
$ sudo udevadm trigger --verbose --settle --action add /sys/class/net/INTERFACE
Replace INTERFACE in both commands with the old name. Do not run this blind over SSH on the only reachable interface. If NetworkManager or systemd-networkd manages the device, restart or resume it according to its own normal procedure after the rename.
Verify the result with ordinary, unprivileged commands:
$ ip -br link
lan0 UP ...
$ udevadm info --query=property --path=/sys/class/net/lan0 | grep '^ID_NET_LINK_FILE='
ID_NET_LINK_FILE=/etc/systemd/network/10-lan.link
Exact columns and extra properties vary. Confirm the new name exists, the link has the expected state, and your network service has restored its configuration.
5. Undo a mistaken rule
If the rule matches the wrong device, or the new name breaks service configuration, remove or rename the file and repeat the controlled application process. Removing a file under /etc is destructive, so back it up first if you might need to inspect it:
$ sudo cp --preserve=all /etc/systemd/network/10-lan.link /tmp/10-lan.link.backup
$ sudo rm /etc/systemd/network/10-lan.link
$ sudo udevadm control --reload
Recovery
Keep the backup until the old naming and service configuration have been tested. If the interface is currently called lan0, reapplication may need that current name in the sysfs path, plus restoring the network service afterwards. A reboot is a possible recovery route, but it is not a substitute for checking the service configuration, and it can leave a remote host unreachable.
Common traps
- An empty
[Match]section matches everything. That causes a udev warning; useOriginalName=*if you really mean it. - Ordering spans directories. Matching files are sorted together across
/usr/lib,/run, and/etc, so a later local file cannot override an earlier match just because/etcnormally wins on priority. MACAddress=in[Link]needs a clear policy. It only takes effect whenMACAddressPolicy=is unset, empty, ornone. Do not combine a fixed MAC with a conflicting policy.NamePolicy=beatsName=. If a policy succeeds, your explicit name may never be used.- Drop-ins only pick up
.conffiles. A directory such as10-lan.link.d/can override one setting without editing the main file, but it still follows the same ordering rules.
Done means
- The rule matches a verified device property, not an assumed kernel name.
- The file lives under
/etc/systemd/networkwith an early numeric prefix. udevadm test-builtinreports your file and proposed name.- The live change was scheduled with recovery access available.
- The renamed interface and its network service have both been verified.