Skip to content

Instantly share code, notes, and snippets.

@Varstahl
Created January 19, 2021 15:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Varstahl/67eaebc9dbf65d82074937d0f9d19f9c to your computer and use it in GitHub Desktop.
Save Varstahl/67eaebc9dbf65d82074937d0f9d19f9c to your computer and use it in GitHub Desktop.
This script takes a list of space separated `desiredInterfaceName=HWaddr`, and renames them if necessary after network has been setup. Useful in OpenWRT 18+ and other embedded distros where udev is not available for remapping. Uses `ifconfig`, `awk`, `cut`. and `grep`.
#!/bin/sh /etc/rc.common
# OpenWRT udev-less physical interface renamer
#
# Place in /etc/init.d/ with +x
# /etc/init.d/<scriptname> enable
# /etc/init.d/<scriptname> enabled && echo on
# start after networking and logging
START=13
bindings="if1=MAC1 if2=MAC2 if3=MAC3"
start() {
# Generate IFACE MAC list
MACS=$(ifconfig -a | awk '/^eth/ {printf $1" "$5"\\n"}')
for i in $bindings; do
NEWNAME=$(echo $i | cut -d'=' -f 1)
MAC=$(echo $i | cut -d'=' -f 2)
OLDNAME=$(echo -e $MACS | grep $MAC | cut -d' ' -f 1)
if [ "$OLDNAME" -a "$NEWNAME" != "$OLDNAME" ]; then
ip link set ${OLDNAME} down
ip link set ${OLDNAME} name ${NEWNAME}
ip link set ${NEWNAME} up
fi
done
# Rename the remaining ethN starting from 0
N=0
MACS=$(ifconfig -a | awk '/^eth/ {printf $1"="$5" "}')
for i in $MACS; do
OLDNAME=$(echo $i | cut -d'=' -f 1)
MAC=$(echo $i | cut -d'=' -f 2)
NEWNAME=eth$N
N=$((N+1))
if [ "$OLDNAME" -a "$NEWNAME" != "$OLDNAME" ]; then
ip link set ${OLDNAME} down
ip link set ${OLDNAME} name ${NEWNAME}
ip link set ${NEWNAME} up
fi
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment