13
$\begingroup$

For an embedded Linux system, if I have two or more network interfaces, how do I ensure that they always get the same interface names every boot

In other words, I want, for example, eth0 to always map to one physical Ethernet port, eth1 to the next, etc.

My Linux "distribution" is home-grown, and I use devtmpfs for populating /dev. I use busybox for init (and most everything else), along with custom init scripts for system startup and shutdown.

I do not need hotplug facilities of mdev or udev -- I'm referring to "fixed" Ethernet ports.

$\endgroup$
1
  • $\begingroup$ I assumed there would be some way to specify this in the devicetree file, but I haven't been able to find out how. $\endgroup$
    – bnewbold
    Commented Jul 31, 2015 at 19:41

2 Answers 2

7
$\begingroup$

This works for me with Linux 3.9.0 on an x86_64 architecture.

#!/bin/sh

# This assumes the interfaces come up with default names of eth*.
# The interface names may not be correct at this point, however.
# This is just a way to get the PCI addresses of all the active
# interfaces.
PCIADDRLIST=
for dir in /sys/class/net/eth* ; do
  [ -e $dir/device ] && {
    PCIADDRLIST="`readlink -f $dir/device` ${PCIADDRLIST}"
  }
done

# Now assign the interface names from an ordered list that maps
# to the PCI addresses of each interface.

# IFNAMES could come from some config file.  "dummy" is needed because of
# my limited tr- and awk-fu.
IFNAMES="eth0 eth1 eth2 dummy"

for dir in `echo ${PCIADDRLIST} | tr " " "\n" | sort` ; do
  [ -e $dir/net/*/address ] && {
    MACADDR=`cat $dir/net/*/address`
    IFNAME=`echo $IFNAMES | awk '{print $1}'`
    IFNAMES=`echo $IFNAMES | awk '{ for (i=2; i<=NF; i++) printf "%s ", $i; }'`
    echo -n "$IFNAME "
    nameif $IFNAME mac=$MACADDR
  }
done
$\endgroup$
1
  • 2
    $\begingroup$ From man nameif: "This program is obsolete. For replacement check ip link." Also note that if the system you are on does use udev, your strategy will be prone to failure as /sys/class/net/eth[whatever] may not exist. $\endgroup$
    – delicateLatticeworkFever
    Commented Jul 28, 2015 at 22:24
2
$\begingroup$

You mentioned that you do not need udev but if you are not opposed to installing it then you may want to consider using biosdevname. There is a great wiki page on freedesktop.org that discusses this issue. As noted on that page, if you're using udev version 197 or higher then you don't even need biosdevname because udev will already do what you need.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.