Table of Contents

Persistent Ethernet Interface Naming by MAC Address

This guide explains how to assign persistent interface names to Ethernet devices based on their MAC addresses during system boot.

This is useful when:

The script will rename only interfaces explicitly defined in the configuration file.

Create init script

Create the following file:

/etc/init.d/staticeth

Insert the following content:

#!/bin/sh /etc/rc.common
 
START=11
 
# don't run within buildroot
[ -n "${IPKG_INSTROOT}" ] && return 0
 
# use busybox grep as GNU grep may behave differently
grep(){
    /bin/busybox 'grep' $@
}
 
# shutdown interfaces and assign temporary names
for i in $( ls /sys/class/net/*/device/uevent | awk -F'/' '{print $5}' | tr '\n' ' ' ) ;
do
 
    mac_address=$( grep $i /etc/config/mac-static-interfaces | awk '{print $3}' | tr -d '"' )
 
    if [ "$mac_address" != '' ]; then
 
        ip link set "$i" down
        ip link set "$i" name old"$i"
 
    fi
done
 
# assign configured interface names based on MAC address
for i in $( ls /sys/class/net/*/device/uevent | awk -F'/' '{print $5}' | tr '\n' ' ' ) ;
do
 
    mac_address=$( cat /sys/class/net/$i/address )
    interface_name=$( grep -i $mac_address /etc/config/mac-static-interfaces | awk '{print $2}' )
 
    if [ "$interface_name" != '' ]; then
 
        ip link set "$i" down
        ip link set "$i" name "$interface_name"
 
    fi
done

Make script executable

chmod +x /etc/init.d/staticeth

Enable service

Enable the service at boot:

service staticeth enable

Create configuration file

Create the following file:

/etc/config/mac-static-interfaces

Example configuration:

config mac-static-interfaces
    option eth0 "70:85:c2:8a:57:4d"
    option eth6 "00:0e:c6:70:2a:22"
    option eth8 "00:0e:c6:c3:82:a6"

Configuration syntax:

option <interface_name> "<mac_address>"

Example:

option eth1 "AA:BB:CC:DD:EE:FF"

Reboot system

After completing the configuration reboot the device:

reboot

Verification

Verify interface names after reboot:

ip link show

Notes

References

This method is based on the original forum post by bobafetthotmail: https://forum.openwrt.org/t/stable-network-interface-names-for-usb-ethernet-dongles/98539/18