when you put your harddisk into another machine, and Linux recognize the new NIC, there will be a new number attached to eth for example eth0 -> eth1

This breaks down some my personal scripts, for example get IP address of the system.

Is there any way to reset the number every time when reboot OR new NIC is found?


Check the udev rules in /etc/udev/rules.d.

In my Fedora distribution, there is an automatically written rule in 70-persistent-net.rules

SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:f1:cc:2f:1a", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0"

which clearly links the ethernet address to the device name. The comments at the top of the file give good clues how to adjust this behavior.

  • cool i try this, ubuntu has this rule as well.
    – c2h2
    Apr 26 '11 at 4:01
  • 2
    @c2h2 If your goal is to simply re-number your NIC(s) from scratch, the simplest solution is to simply delete this file and then reboot -- it will be automatically regenerated during the bootup process, and your first NIC will now be eth0.
    – Kromey
    Apr 26 '11 at 16:57
  • yeps, it works perfectly!
    – c2h2
    Apr 27 '11 at 12:54

Add this to your script. It may need to be run as root depending on your system.

ethX=`ifconfig -s | awk 'NR==2 {print $1}'`

Then you can use $ethX as a variable to your NIC.
This just takes the output of ifconfig -s and parses out everything except for the text on line 2 and column 1.


From what I understand, Linux uses bus numbering to determine the order of the interfaces and their numbering in a given machine. It can be PCI bus, mini-PCI or whatever else. Lately, due to the parallel startup scripts of all sorts and kinds, things have become outright random sometimes from one boot to another (actually, NICs have been more or less named the same on the same machine, but other devices not so much).

Obviosuly, the order of the devices on the bus changes from machine to machine. What doesn't change is the MAC address.

My solution:

If you are using udev, you can use the udev config rules similar to the ones described here (for Debin, ubuntu is similar) to force a binding between specific interface and the MAC address of the NIC.

Caveat Emptor: some NICs do allow to edit their MACs. In this case, it won't work.

Extra-curricular info: A harder-to-implement alternative would be to use the new mechanism Matt Domsch/Dell are proposing for Fedora, reviewed here

As far as Ubuntu goes, there is a proposal to integrate that scheme in as well. According to 1 above: Although Fedora is shipping biosdevname first, other Linux distributions are also expected to adopt it. There is reportedly a blueprint for integrating this in Ubuntu 11.04 already and a feature request for inclusion in OpenSUSE as well.

Hopefully this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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