Aus Bennys Wiki
Wechseln zu: Navigation, Suche

How to reorder or rename logical interfaces names in Linux[Bearbeiten]

One of the problems of Linux is that the order of the network interfaces is unpredictable. Between reboots it does stay the same, but it is very well possible that after an upgrade to a new kernel or the addition or replacement of a network card (NIC) that the other of all network interface changes. For example, what used to be eth0 now becomes eth1 or eth2 or visa versa.

Obviously there is some logic to which network interface gets which name, but Linux documentation states that this may change and no user or program should ever assume anything about this. Obviously, this is annoying, in particular if your management interface is at eth1 at one node in a cluster and at eth2 in another node of the same cluster (which we have experienced). I personally like to have my (primary) mangement interface always to be at eth0.

Thankfully, there are ways to achieve this. They can be divided in four methods:

  1. Order the network interfaces based on the physical location of the NIC in the computer
  2. Order the network interfaces based on the driver of the NIC.
  3. Order the network interfaces based on the MAC address of the NIC.
  4. Order the network interfaces based on physical properties of the NIC.

Test //Note//: Linux kernels up to 2.4 did only probe for the first Ethernet card, ignoring other NICs. We assume you use a 2.6 or higher kernel or already fixed this, for example by specifying ether=0,0,eth1 as kernel parameter.

Based on the physical location in the computer[Bearbeiten]

**Warning**: This only works if the driver is built into the kernel, not as a loadable module.

//Note: It is relatively hard to get this to work, and we encountered problems with it. |the other methods are recommended.//

It is possible to name the network interfaces based on the interrupt (IRQ) and memory address. This should work if you have network cards in PCI busses, and it involves appending the proper parameters to the "ether=" or "netdev=" kernel parameters.

First of all, you can detect the PCI slot of the devices using %%lspci -v%% This is reported to fail sometimes for certain cards. Now, write down the IRQ and IO (memory) address of each network card, and use this information to specify the interface name in your LILO or GRUB configuration file.

For LILO, you need to add an add line to the appropriate boot configuration. For example: %%append="netdev=irq=21,io=0x2040,name=eth0 netdev=irq=20,io=0x3000,name=eth1 netdev=irq18,io=0x2000,name=eth2"%% Under grub, it can just be listed as parameter. e.g.: %%kernel /boot/vmlinuz netdev=irq=24,name=eth0%%

More Information[Bearbeiten]

Based on the driver[Bearbeiten]

**Warning**: This only works if the driver is not built into the kernel, but is available as a loadable module.

First of all, determine which driver is used by each network card. Thankfully Linux does have a system to load the appropriate driver automatically, based on the [PCI ID] of the network card. Unfortunately, there is no single command to simply get the driver (and other information like the link speed) based on just the interface name in Linux. Your best bet is to look for kernel messages: %%dmesg | grep eth%% This should give you a good estimate of the driver name. You can further verify if the name you have in mind indeed does exist and is loaded: %%lsmod%%

**Note**: lsmod gave: %%e1000 84868 0 tg3 70816 0%% However, the 0 indicates that these drivers are not controlling any device! That is strange, since ##modprobe -r tg3## and ##modprobe -r e1000## do disable the network cards. Apparently, this is a flaw in ##lsmod##.

Note that running ##modprobe tg3## en then ##modprobe e1000## does bring them up in the correct order, with the correct interface names. This is a good check if this approach (using the driver to decide the interface name) can work.

Red Hat[Bearbeiten]

In Red Hat, if the driver is called "tg3" (the Tigon driver), you simply specify the network name by adding this enty in ##/etc/modules.conf##: %%~alias eth0 tg3%%


On a Debian system, ##/etc/modules.conf## is generated automatically and should not be edited directly. Instead, create a file in the subdirectory ##/etc/modules/## (do not use ##/etc/modprobe.d/##, that seems out-of-date). For example, create the file ##/etc/modutils/interfaces## and add the appropriate modules. For example: %%alias eth0 tg3 alias eth1 e1000%% Next, update /##/etc/modules.conf## by running: %%update-modules%%

**Alternative method**: I have encountered scenario's where the kernel did already load the modules for the drivers, even before ##/etc/modules.conf## was read. The result was that in effect, the specification in ##/etc/modules.conf## was ignored, and this method did not work. As an alternative, it is possible to also list the drivers, in the appropriate order, in ##/etc/modules## (thus //not// ##/etc/modules.conf##): %%tg3 e1000%% The result will be that the tg3 driver is loaded before the e1000 kernel. Since ##/etc/modules## only exists for Debian, this trick will most likely not work for other distributions.

Based on the MAC address[Bearbeiten]

Last, it is also possible to name the network interface based on the MAC address of each NIC. The advantage is that it is possible to use this method if you have two NICs which use the same driver.

As a first start, you must determine the MAC address of your interfaces. You can do this locally on a machine using %%ifconfig -a%% The MAC address is listed as "hwaddr" (hardware address). Alternatively, you can even determine MAC addresses remotely using ##ping## and ##/sbin/arp##.

There are three ways to do map the MAC address to the logical interface name. By using the script, by using the nameif program or by using udev. The method below is know to work with Debian. Other distributions to not have the ##interface## file , but other mechanisms for interface configuration (like ##/etc/sysconfig/network-scripts/## files for Red Hat). So, it may not be trivial to apply this to other distributions. However, if you succeed, we very much welcome feedback!

Using the script[Bearbeiten]

The first solution is to use the script to do mapping by MAC address. On Debian, this script is distributed as part of the ifupdown package (in ##/usr/share/doc/ifupdown/examples/ With this script copied to some sane place (e.g. ##/usr/local/bin##), you can then setup ##/etc/network/interfaces## in this manner:

%%auto lo eth0 eth1

iface lo inet loopback

mapping eth0 eth1 script /usr/local/bin/ map 00:37:E9:17:64:AF netA map 00:21:E9:17:64:B5 netB

iface netA inet static address etc...

iface netB inet static address etc...%%


The disadvantage of this method is that does define a mapping, rather then changing the actual logical interface name.

Using the nameif program[Bearbeiten]

Alternative to the script, you can also use the slightly more convienant ##nameif## program, which is distributed as part of the net-tools package on Debian.

The advantage of nameif is that you can specify the interface names in the ##/etc/mactab## file: %%ethmgnt 00:37:E9:17:64:AF ethwireless 00:21:E9:17:64:B5%%

It is not possible to rename an interface to a name of an existing interface. So you can't rename eth1 to eth0 as long as eth0 still exists. It is possible to still swap the names eth0 and eth1 by using a temporary name (e.g. first rename eth1 to ethfoo, then eth0 to eth1 and finally ethfoo to eth0). It should be noted however, that this may lead to problems if you add another network cards or upgrade your kernel, and the interface names just before you call nameif are not as expected. Therefor, it is recommended to use other names like "ethmgmnt", "ethwired", "ethwireless" and "eth10ge", as shown in the example above.

Using udev[Bearbeiten]


Based on the physical properties[Bearbeiten]

Perhaps the most elegant way to name the ethernet NIC is to do so based on their physical properties, like link speed and port type.

Based on MAC address[Bearbeiten]

Rename network cards by editing /etc/udev/rules.d/30-net_persistent_names.rules. E.g. enter a line like

SUBSYSTEM=="net", ACTION=="add", SYSFS{address}=="00:00:6c:b8:33:2d", IMPORT="/lib/udev/rename_netiface %k eth0"

Using the ethtool and ip programs[Bearbeiten]

It is possible to check the NIC properties using the ethtool program, and to change the name using the ip program (thanks to Jollynn Schmidt for this tip):

%% if ethtool eth0 | grep -q "Port: FIBRE"; then ip link set dev eth0 name not_eth0 ip link set dev eth1 name eth0 ip link set dev not_eth0 name eth1 fi %%