Nail Down Network Interface Names with ifrename

Enterprise Networking Planet content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Who among us has not experienced the joy of rebooting a multi-homed Linux server, and found that our network interfaces have come up in a different order, and been assigned different names by the kernel? This is especially amusing when doing a kernel upgrade on multiple servers, and all of them do the random-re-assignment dance. Sure, everyone knows we have all the time in the world for useless tasks like re-configuring network cards, but still, there must be a better way.

Of course there is a better way, else I would not be writing this article. The ifrename utility, by the esteemed Jean Tourrilhes of the Wireless Tools for Linux project, assigns permanent, arbitrary names to all types of network interfaces: ISA, PCI, USB, CardBus, and PCMCIA. This means you can easily give your devices sensible names like “lan”, “wan”, and “dmz”, or use their driver names, instead of being stuck with the uninformative eth0/eth1 etc. naming scheme. ifrename is part of the wireless-tools package.

Users of Red Hat and Red Hat-spawned systems like Fedora, White Box, CentOS and so forth, already have the ability to staple their network interfaces immovably in place, which we shall get to presently. Debian is the bigger problem child when it comes to managing multiple network interfaces, so we’ll start there.

ifrename on Debian
ifrename is available as a separate package for Debian. It installs ready to go to work, with a boot script and configuration file already in place. The configuration file is /etc/iftab. A simple configuration looks like this:


# This file assigns persistent names to network interfaces. See iftab(5).
lan mac 00:0b:6a:ef:7e:8d
wan mac 00:04:5a:55:5c:9d

Then you must use these names in /etc/network/interfaces, and in every script that uses interface names.

I chose the names “lan” and “wan” for my two PCI Fast Ethernet interfaces, and mapped them to their MAC addresses. MAC addresses are unique for every device, so they make good identifiers for this. How do you find the MAC number? With either the ifconfig -a command, or the ip command, like this:


$ ip link show
1: lo: mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth1: mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:04:5a:55:5c:9d brd ff:ff:ff:ff:ff:ff
3: eth0: mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0b:6a:ef:7e:8d brd ff:ff:ff:ff:ff:ff

The ip command is part of the iproute package. While the iproute maintainers seem to want the net-tools suite to go away, which includes ifconfig, netstat, nameif, route, mii-tool, and arp, it’s OK to have both, as each one has its uses. (See Resources.)

What about hotpluggable network cards, like USB or laptop cards? As long as your hotplug system is working, no problem. Same song, same dance. Check out Jean Tourrilhes’ Hotplug.txt page for help with tweaking hotplug cards on Debian.

You may use other static criteria, like driver name, IRQ, base address, or other arcane goodies as listed in man iftab.

Interface Configuration
Don’t forget to use your nice new sensible interface names in /etc/network/interfaces:


auto lo
iface lo inet loopback

auto lan wan
iface lan inet static
address 192.168.1.23
netmask 255.255.255.0
gateway 192.168.1.1

iface wan inet static
address 10.0.0.1
netmask 255.255.255.0
gateway 1.2.3.4


Reboot, and when the system comes back up ifconfig -a or ip link show will show your new device names.

Boot Script
Debian comes with an init script for ifrename, which can be used on any system. If you want to use ifrename on a different system, you must have a boot script. Other systems don’t always include an init script for ifrename. Here is the entire script. Naturally you may tweak it to suit, and mind your filepaths:


#!/bin/sh

NAME=ifrename
IFRENAME=/sbin/ifrename
IFTAB=/etc/iftab

test -f $DAEMON || exit 0
test -f $IFTAB || exit 0

case "$1" in
    start|reload|force-reload|restart)
     $IFRENAME -d -p
     ;;
     stop)
     ;;
     *)
         echo "Usage: invoke-rc.d $NAME {start|stop|reload|force-reload|restart}"
     ;;
esac

exit 0

Take a look at the $IFRENAME -d -p line. The -d flag is Debian-specific; it loads only the drivers for interfaces named in /etc/network/interfaces. -p tells ifrename to probe for the interface modules, and load any that are not already loaded. This is a nice feature that doesn’t hurt anything if it’s not needed, and it makes sure that the correct drivers get loaded.

The Debian init script also includes the -t flag, which I have deleted. This allows interface name-swapping. For example, when one interface is down, a second interface can take its name. There may be times when you want this to happen; me, I want them to stay put.

The Red Hat Way
Red Hat handles this naming business in a different way. Suppose you want “lan” and “wan” instead of “eth0” and “eth1.” Simply use the DEVICE and HWADDR directives in the /etc/sysconfig/networking/devices/ifcfg-* files:


DEVICE=lan
HWADDR=11:22:33:44:55:66

You can even change the configuration file names to help you keep track, like /etc/sysconfig/networking/devices/ifcfg-lan.
Again, remember to use your new device names in all scripts that include them, like your iptables scripts.

Hardware Tips
When you’re building a multi-homed server, the most sure-fire way to determine which card is which is to connect them one at a time to a second PC and ping them. Then use a nice marking pen or sticky label. Using network cards with different drivers also gives you a way to tell them apart.

Resources

Get the Free Newsletter!

Subscribe to Daily Tech Insider for top news, trends, and analysis.

Latest Articles

Follow Us On Social Media

Explore More