How to associate an interface name with it's MAC?

Colleagues, tell me please, how to associate an interface name with it's MAC?

Periodically, unpleasant things happen related to the number of network interfaces of the same type, which entail their automatic renumbering and the failure of all settings.
The router had five network adapters of the same type. One of them failed and rebooted the computer. All adapters have changed names and all network settings have been moved to other boards - addresses, masks, routing, firewall, etc.
Now it's the other way around - a villain engineer came and added another interface. Of course, everything has shifted again.

What is the best way to associate a specific interface with its name. To make the network adapter with MAC 11:22:33:44:55:66 always be re5? Even if adapters there are only two left... Those, I want not to change the MAC of the interface, but to make the MAC determine all its parameters, no matter how the network cards are rearranged in the computer.

Thanks for the answers to the question,
Ogogon.
 
I had a play with this and made it work.

BEWARE: it will change the name of any NICs enumerated in /usr/local/etc/ifmap, which could break things.

My code below had "DEBUG=YES" to prevent any actual actions:
Code:
$ cat /usr/local/etc/ifmap
52:54:00:98:51:c9 MYNICNAME

$ cat /usr/local/etc/devd/nicalias.conf
# Create an alias for NICs based on their MAC address
#
notify 100 {
        match "system"          "IFNET";
        match "type"            "LINK_UP";
        media-type              "ethernet";
        action "/usr/local/bin/mapnic $subsystem";
};

$ cat /usr/local/bin/mapnic
#!/bin/sh
# Create an alias for NICs based on their MAC address
PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/sbin:/usr/sbin
export PATH
IFMAP=/usr/local/etc/ifmap
DEBUG=YES
dev=$1
mac=$(ifconfig "$dev" | grep ether | awk '{print $2;}')
name=$(grep -i "^$mac" $IFMAP | awk '{print $2;}')
if [ $DEBUG = "YES" ]
then
    (
        echo "$0: $(date)"
        echo "    dev=$1"
        echo "    mac=$mac"
        echo "    name=$name"
        echo "    ifconfig \"$dev\" name \"$name\""
        echo "======"
    ) >>/tmp/ifmap.log
else
    [ -n "$name" ] && ifconfig "$dev" name "$name"
fi

$ cat /tmp/ifmap.log
/usr/local/bin/ifmap: Thu Jun  8 15:37:29 AEST 2023
    dev=vtnet0
    mac=52:54:00:98:51:c9
    name=MYNICNAME
    ifconfig "vtnet0" name "MYNICNAME"
======
Note: /usr/local/bin/mapnic must be executable.

Also, I didn't actually change the name of my NIC, so there may be timing issues at boot. Suggest you play with a test system... and report back for those who get here via google...
 
I didn't know there is a port for ethname.

Besides the wonderful work that gpw928 has presented, have also a look on ethname, a rc(8) script for re-naming devices based on their MAC address, created by Eric A. Borisch. I did a quick test of ethname in a VM, the new interface names are assigned after the MAC addresses as advertised by the author.
 
Eric A. Borisch's sysutils/ethname rc script covers the requirements very nicely:
Code:
# Self-contained rc.d script for re-naming devices based on their MAC address.
# Renaming is performed before interface bring-up -- netif -- so all
# configurations of the devices can be done with the new names.
It also addresses the boot timing issue that I pondered, but never tested.
 
Thank you. But I have an important clarification. Do these handy scripts only worked at system startup or all the time?

Some routers have interfaces that can be disabled during operation, and later reconnected. (OpenVPN tunnels, mpd5 netgraph interfaces, just USB adapters - they can appear and disappear for various reasons.)

Will the described system work every time the interface appears?
 
Will the described system work every time the interface appears?
/etc/rc.d/netif brings up the Ethernet interfaces at boot time with ifconfig(8) commands.

/usr/local/etc/rc.d/ethname runs just once during boot before netif:
Code:
$ grep BEFORE /usr/local/etc/rc.d/ethname
# BEFORE: netif
So sysutils/ethname will work on devices that enumerate reliably at boot time. e.g. motherboard and PCIe bus Ethernet adapters (because the ifconfig would be executed after the name change).

I'm not sure that USB Ethernet adapters enumerate reliably at boot time. In my experience they need time to settle.

/etc/rc.d/devd starts after netif:
Code:
$ grep REQUIRE /etc/rc.d/devd
# REQUIRE: netif ldconfig
So I don't expect that my method would work for motherboard and PCIe bus Ethernet adapters (because the ifconfig would be executed before the name change).

My method would work for devices that appear unexpectedly, and subject to "dynamic" ifconfig(8).

I think that you need my method for transient NICs and Eric A. Borisch's method for permanent NICs...

All theory. Suggest you test...
 
Back
Top