Renaming Interfaces in Linux Debian 9 Stretch

There are a million articles on this, but very few of them actually worked on Debian Stretch 9. So most of the other articles I think are older and all talk about writing udev rules or editing 70-persistent, bla bla bla. None of that actually works on Stretch. They changed the mechanism or something it looks like. This way is actually much easier anyway. It’s creating systemd interface.link files for each interface.

If  you’re just wanting to add an interface description, or “ifAlias”, then you don’t need to do any of this, just do the following.

sudo ip link eth0 set alias dmz1

Now if you actually want to rename the interface itself, or make the alias change persistent, you still need to follow the process.

first you have to edit grub config file and tell it not to use the names that bios gives to the interfaces. I did this step, but I’m not sure it’s necessary with the new process. I’ll cover it just in case, but you might try skipping this step

sudo vi /etc/default/grub
# edit the following line to add the biosdevname=0 part:
GRUB_CMDLINE_LINUX_DEFAULT="biosdevname=0"
:wq

sudo update-grub

Then if you want this to take affect you have to reload I think.
I don’t like reloading, but….it’s the bootloader. That’s kind of when it runs.
But again – not positive this step is needed with the new process.
Network link configuration is performed by the net_setup_link udev builtin

So the new process goes like this:
Create a link file the interface in /etc/systemd/network
below is an example of my file Gi0.link file I created


[Match]
MACAddress=9c:8e:99:2c:67:84

[Link]
Name=Gi0
Alias=outside

Next step is to adjust the /etc/network/services files so it will actually recognize the new interface name.

sudo vi /etc/network/interfaces
# below is my file after editing:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug Gi0
iface Gi0 inet dhcp

I tried a bunch of different ways to get it reload the NIC mapping, including telling udev to reload rules, but nogo.
Now you have reload the NIC driver or reboot as best I can tell.
First you have to find out what your NIC driver module is so you have to use udevadm to find that.

You need to know the “current” interface name to do this, which you can get with

ip link show

Take note of the interface name

udevadm info -q all --path /sys/class/net/eth2
substitute your interface name instead of eth2

output will look similar to this:

P: /devices/pci0000:00/0000:00:07.0/0000:03:00.1/net/dmz1
E: DEVPATH=/devices/pci0000:00/0000:00:07.0/0000:03:00.1/net/dmz1
E: ID_BUS=pci
E: ID_MODEL_FROM_DATABASE=NetXtreme II BCM5709 Gigabit Ethernet (NC382i Integrated Multi-port PCI Express Gigabit Server Adapter)
E: ID_MODEL_ID=0x1639
E: ID_NET_DRIVER=bnx2
E: ID_NET_LINK_FILE=/etc/systemd/network/10-dmz.link
E: ID_NET_NAME=dmz1
E: ID_NET_NAME_MAC=enx9c8e992c6786
E: ID_NET_NAME_PATH=enp3s0f1
E: ID_OUI_FROM_DATABASE=Hewlett Packard
E: ID_PATH=pci-0000:03:00.1
E: ID_PATH_TAG=pci-0000_03_00_1
E: ID_PCI_CLASS_FROM_DATABASE=Network controller
E: ID_PCI_SUBCLASS_FROM_DATABASE=Ethernet controller
E: ID_VENDOR_FROM_DATABASE=Broadcom Limited
E: ID_VENDOR_ID=0x14e4
E: IFINDEX=11
E: INTERFACE=dmz1
E: INTERFACE_NAME=dmz
E: SUBSYSTEM=net
E: SYSTEMD_ALIAS=/sys/subsystem/net/devices/dmz1 /sys/subsystem/net/devices/dmz1
E: TAGS=:systemd:
E: USEC_INITIALIZED=55694716394

From this the line you’re interested in is ID_NET_DRIVER
Mine says bnx2

Next step is to bounce driver. I was ssh’d in and didn’t want to have to hook up a monitor to the box when I lost networking so I created a shell script for this step.

vi bounceNIC.sh

below is my shell script

#!/bin/bash
sudo rmmod bnx2
sudo modprobe bnx2

Then I did chmod +x bounceNIC.sh

then you need to run it detached so when your ssh session dies, it stays running and finishes the reset.

sudo bounceNIC.sh &

your SSH session will disconnect and a few seconds later reconnect it’s just treated as a network blip, you don’t have to log back in.

CONCLUSION:
It’s not that bad, but I wish there was a way of doing it without having to pull the NIC driver or reboot.
Since this is a 4 port NIC all using the same driver, I would inadvertantly end up bouncing ALL interfaces, which sucks.
There needs to be a better way.

If anyone knows of how I can run “net_setup_link udev builtin” directly, please let me know.

=========== EDIT =========
There is actually an easier way to do it, kind of but I don’t think it’s persistent.
However, if you need to rename an interface on the fly bouncing only THAT interface, this would be an improvement.

You could do below method to get it renamed for this session, then do above method (skip step of unloading/reloading driver) to make sure it stays persistent on reboot.

sudo /sbin/ifdown originalnicname
sudo ip link set name originalnicname newnicname

sudo vi /etc/network/interfaces
allow-hotplug newnicname
iface newnicname inet dhcp
:wq

sudo /sbin/ifup newnicname