Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is there any way to swap network interfaces (eth1 <-> eth0) after system installation.

My brand new Debian 6.0 install assigned PCI network card as "eth0" and motherboards integrated network device as "eth1" by default. The problem is I want to use the integrated device as default (eth0) network interface.

I already edited :

/etc/udev/rules.d/70-persistent-net.rules

to swap the names and everything seems to be ok and network is working but programs are still trying to use the PCI network card (which is now "eth1") as the default interface. For example iftop now tries to use "eth1" as default device as it used "eth0" before the swap.

Is this purely a software problem as the applications are trying to use the first found device as a default device despite their interface naming or is there any way to fix this by configuring OS?


edit: I wrote a small app to print out iflist and the PCI device (eth1) came up before "eth0". Any ideas how to swap the device order.


edit: I found a thread about the same problem and I tried everything they suggested and none of the solutions are working except for swapping the names "virtually".

share

migrated from serverfault.com Mar 29 '11 at 0:45

This question came from our site for professional system and network administrators.

5 Answers 5

up vote 8 down vote accepted

I am answering to my own question now because I finally found a workaround for this problem.

I found out that it is possible to reorder the devices by unloading the drivers and then loading them in correct order.

First method (bruteforce):

So the first method I came up with was simple to bruteforce the driver reload with init.d script.

Following init script is tailored for Debian 6.0 but the same principle should work on almost any distribution using proper init.d scripts.

#!/bin/sh -e

### BEGIN INIT INFO
# Provides:          reorder-nics
# Required-Start:
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Reloads the nics in correct order
### END INIT INFO

#
# This script should reload the nic drivers in corrected order.
# Basically it just unloads and then loads the drivers in different order.
#

echo "Reloading NICs!"

# unload the drivers
modprobe -r driver_0        # eth0 nic interface
modprobe -r driver_1        # eth1 nic interface

# load the drivers in corrected order
modprobe driver_1
modprobe driver_0

#EOF

Then the script must be added to proper runlevel directory. This can be done easily on Debian with "update-rc.d" command. For example: update-rc.d reorder-nics start S


Second method (Better I think):

I also found a bit more elegant way (at least for Debian & Ubuntu systems).

First Make sure that kernel doesnt automatically load the NIC drivers. This can be done by creating a blacklist file in /etc/modprobe.d/. I created a file named "disable-nics.conf". Note that files in /etc/modprobe.d/ must have .conf suffix. Also naming modules in /etc/modprobe.d/blacklist.conf do not affect autoloading of modules by the kernel so you have to make your own file.

# Disable automatic loading of kernel driver modules
# Disable NIC drivers

blacklist driver_0     # eth0 by default
blacklist driver_1     # eth1 by default

Then run 'depmod -ae' as root

Recreate your initrd with 'update-initramfs -u'

And finally add the driver names in corrected order into /etc/modules file.

# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
# Parameters can be specified after the module name.

# drivers in wanted order
driver_1    # this one should be loaded as eth0
driver_0    # this one should be loaded as eth1

Changes should come in effect after the next boot.

Reboot is not necessary though, it's easy to switch the devices with following command (as root ofcourse):

modprobe -r driver_0; modprobe -r driver_1; modprobe driver_1; modprobe driver_0

Some usefull links I found while searching the solution:

share
1  
+1 You definitely deserve more than only one up vote for that. –  bahamat Jul 9 '11 at 17:27
    
I have a similar problem: I have an integrated NIC, then sometimes I run a VPN program which creates another interface. The VPN interface always seems to take precedence, but I don't know why. I only want it to be used in very rare circumstances (I want the application that uses it to have to specify the VPN interface). Any ideas? ^_^ I'll probably post a new question pretty soon anyway. –  Limited Atonement Jul 9 at 13:04

You can use the netdev= kernel command line parameter (you need to pass that to the kernel in grub) to instruct the kernel to link a given irq to a given interface, e.g.: netdev=irq=2,name=eth0

share
    
I booted my box with the grub config modifications and I am still getting eth1 as the default device in many apps. I checked the dmesg for network info and and it says that the integrated NIC has still 'eth1' as ifname: forcedeth 0000:00:04.0: ifname eth1, PHY OUI 0x57d @ 1, addr 40:40:00:40:40:40. This is not too serious problem but it really grinds my gears because the integrated is 1gb card and it should be the default device. –  Athabaska Dick Mar 29 '11 at 10:19
    
I have now tried to use nameif to change the network interface names but looks like it does the same that udev does. No change in "real" NIC order. I also tried to change the PCI NICs physical location but it didn't help either. Integrated NIC has IRQ 22 and PCI NIC has IRQ 17 so looks like kernel is ordering them by IRQ and user cannot change that fact in any way. Any fresh ideas? –  Athabaska Dick Mar 29 '11 at 12:15

You are likely going to have to go into each affected programs configuration files and change 'eth1' to 'eth0.' Such programs defaults are setup when they are installed or first run with the currently detected NICs.

I use Linux as a router, and had this issue when using scripts. I now have a nice script fragment called netconf that I source in for any other script whenever i need to use NIC names, this file gives me a central location to specify them (i.e. LAN_IFACE=eth0, WAN_IFACE=eth1, etc.)

share
2  
Looks like that lots of programs are just relying on if_nameindex() function from <net/if.h> header. They just use the first device they find and totally ignores the interface names. I can see why it is done like that, its much easier to use the first found device than sort the names. –  Athabaska Dick Mar 29 '11 at 14:22

You can't change which interface is used by default in applications like iftop. They call the C library function if_nameindex and use the first element in the returned array by default. GNU libc's if_nameindex on Linux is a thin wrapper around the SIOCGIFCONF ioctl. That returns interfaces in a fixed order, based on the order in which the network drivers were initialized and the order in which each driver detected each device.

If you really don't want to have to pass -i to iftop and similar programs, you can make a small wrapper around if_nameindex that reorders the elements in the returned list, with LD_PRELOAD. I would call that a lot more trouble than it's worth.

share
    
I managed pinpoint the "problem" into if_nameindex function too. Perhaps I just leave it for now. Luckily some apps really checks the interface name. On the other hand some apps doesnt even have default configuration options so I just have to use -i option. I was just wondering why kernel loading option netdev=irq=22,name=eth0 does not work? I thought it should be possible to change the NIC order at kernel startup. –  Athabaska Dick Mar 29 '11 at 21:16
    
From a quick look at the kernel source (the for_each_netdev macro), interfaces are enumerated in the order in which drivers are loaded (roughly). Your interfaces probably use different drivers, so you'd have to arrange for the drivers to be loaded in the order you want. I would expect this to be difficult, especially if you want your tweak to work across a kernel upgrade. –  Gilles Mar 29 '11 at 21:21

If they have different drivers, at least back in the day you could put into one of the module configuration files:

alias eth0 driver1
alias eth1 driver2

That's some pretty old knowledge but it may help.

share

This site is currently not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .