Sep 082006
 

Sponsored Link

udev is a daemon which dynamically creates and removes device nodes from /dev/, handles hotplug events and loads drivers at boot time. It replaces the hotplug package and requires a kernel not older than 2.6.12.udev -- /dev/ and hotplug management daemon

Install udev in Debian

#apt-get install udev

After installing this you need to reboot your machine.

Upgrade from devfs to udev on Debian Sarge

Some of you used devfs under Woody, or under their previous Sarge install. When going kernel 2.6, it may be useful to revert or upgrade to udev instead. The problem is that their nomenclatura is different, so your system may be rendered unbootable. Let's see what to care about to switch from devfs to udev.

First, the bootloader will no longer need the devfs=mount parameter on the kernel command-line. So, wheter you use GRUB or LILO, remove or comment the reference to devfs=mount.

Second, check in your /etc/fstab that you have nothing in devfs-nomenclatura, like /dev/ide/host0/bus0/target0/lun0/part3, but everything in the standard nomenclatura like /dev/hda3.

Third, install the udev package, it will probably bring some dependencies too. That's ok.

Last, check the links in /etc/udev/rules.d/. There are probably two links to the upper directory: compat-full.rules and devfs.rules. Remove them and create a new link to udev.rules and to cd-aliases.rules like this

# cd /etc/udev/rules.d/
# rm compat-full.rules devfs.rules
# ln -s ../udev.rules .
# ln -s ../cd-aliases.rules .

Now, reboot, and if you want, remove package devfsd.

Note: It may also be necessary to reconfigure some special packages which store device information in their configuration file, like XFree86.

If you want rename a network interface's device name when using udev. This can be practical to do since the automatically assigned device name may actually change around a bit due to random variation in the initialisation process of them.

For instance, eth0 can on one bootup refer to network interface A and eth1 to network interface B, but on another bootup can interface A be assigned eth1 and interface B be assigned eth0, which can cause problems if only one of them has a network cable attached and is configured to obtain an address over DHCP — the interface without the cable attached will then attempt to do that instead!

udev Overview

Unlike the traditional Linux system, where the device nodes in the /dev directory have been a static set of files, udev dynamically provides only the nodes for the devices actually present on a system. Although devfs provided a similar functionality, advocates for udev cited a number of reasons for preferring its implementation over devfs.

udev supports persistent device naming, which does not depend on, for example, the order in which the devices are plugged into the system. The default udev setup provides persistent names for storage devices. Any hard disk is recognized by its unique filesystem id, the name of the disk and the physical location on the hardware it is connected to.

udev executes entirely in user space, as opposed to devfs' kernel space. One consequence is that udev moved the naming policy out of the kernel and can run arbitrary programs to compose a name for the device from the device's properties, before the node is created.

udev operation

Udev is a generic kernel device manager. It runs as a daemon on a Linux system and listens to events the kernel sends out if a new device is initialized or a device is removed from the system. The system provides a set of rules that match against exported values of the event and properties of the discovered device. A matching rule will possibly name and create a device node and run configured programs to set-up and configure the device. Udev rules can match on properties like the kernel subsystem, the kernel device name, the physical location of the device, or properties like the device's serial number. Rules can also request information from external programs to name a device or specify a custom name that will always be the same, regardless of the order devices are discovered by the system.

Obtaining the MAC addresses from your Debian machine

If you want to obtain MAC address of any linux machine you need to run the following command

#ifconfig

This will give you all the existing network interfaces in your machine

example:-

# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:AB:CD:12:34:56
BROADCAST MULTICAST MTU:1500 Metric:1
RX packets:0 errors:0 dropped:0 overruns:0 frame:0
TX packets:3 errors:0 dropped:0 overruns:0 carrier:3
collisions:0 txqueuelen:1000
RX bytes:0 (0.0 b) TX bytes:180 (180.0 b)
Interrupt:185 Base address:0xc000

In the above example HWaddr is your MAC address 00:AB:CD:12:34:56

Using udev

Basic Rules

udev provides several different match keys which can be used to write rules which match devices very precisely. Some of the most common keys are introduced below, others will be introduced later in this document. For a complete list, see the udev man page.

KERNEL -- match against the kernel name for the device

SUBSYSTEM -- match against the subsystem of the device

DRIVER -- match against the driver name for the device

After you have used a series of match keys to precisely match a device, udev gives you fine control over what happens next, through a range of assignment keys. For a complete list of possible assignment keys, see the udev man page. The most basic assignment keys are introduced below. Others will be introduced later in this document.

NAME -- the name that shall be used for the device node

SYMLINK -- a list of symbolic links which act as alternative names for the device node

Create a new file in the udev rules directory, e.g. /etc/udev/rules.d/010_netinterfaces.rules

In it specify the renaming in the following way for each interface on its own line

KERNEL="oldnameprefix*", SYSFS{address}=="MACaddress", NAME="newname"

where the oldnameprefix is typically eth. Note that in the MAC address, the hexadecimal digits should be in lowercase, otherwise udev fails to match them properly with the network interface.

You have quite a bit of freedom in choosing the new name, We recommend to keep it short and without any spaces or weird characters though. You can e.g. specify a fixed eth0, eth1, eth2 for specific MAC addresses, or you can name them after their use, or anything really. Remember that some applications that poke on a low level may dislike them not being called in the normal fashion of eth0, eth1..etc

Examples using udev

Example: Three network interfaces being present on a computer, setting a fixed eth0, eth1 and eth2 as their names.

KERNEL=="eth*", SYSFS{address}=="00:12:34:fe:dc:ba", NAME="eth0″
KERNEL=="eth*", SYSFS{address}=="00:56:78:98:76:54″, NAME="eth1″
KERNEL=="eth*", SYSFS{address}=="00:90:ab:32:10:fe", NAME="eth2″Example: Three network interfaces (one Intel, one NVIDIA, and one 3Com) being present on a computer, naming them after the manufacturer of the interfaces.

KERNEL=="eth*", SYSFS{address}=="00:12:34:fe:dc:ba", NAME="eth-intel"
KERNEL=="eth*", SYSFS{address}=="00:56:78:98:76:54″, NAME="eth-nv"
KERNEL=="eth*", SYSFS{address}=="00:90:ab:32:10:fe", NAME="eth-3com"

Updating network configuration

If you named the interfaces in a different fashion as they were named before, the network configuration needs to be updated for the new interface device names to be used.

Edit the /etc/network/interfaces file, and change all instances of the old names to the new names.

E.g. if you previously used eth0 and have renamed it newname, you'd replace all instances of eth0 in that file with newname

But if you just put a fixed eth0, eth1, ... as their names, you just need to make sure the one you want to have as the primary network interface is set to the one you want in the file.

Example

Having renamed the existing eth0, eth1, and eth2 to eth-intel, eth-nv and eth-3com, choosing to use the eth-intel one as the primary interface

The /etc/network/interfaces file before changes

# The primary network interface
auto eth0
iface eth0 inet dhcp

# Currently unused network interfaces
iface eth1 inet dhcp
iface eth2 inet dhcpThe file after changes:

# The primary network interface
auto eth-intel
iface eth-intel inet dhcp

# Currently unused network interfaces
iface eth-nv inet dhcp
iface eth-3com inet dhcp

Now you need to Reboot the computer and verify that the new network interface names are in use with e.g. ifconfig

#ifconfig newname

Where newname is the new interface name you specified. Repeat procedure for each one you renamed.

Rename CD/DVD drives

If you want to change the names of a DVD reader (hdf), and a DVD rewriter (hdg) to view like /dev/dvd for convenience.

As we know the KERNEL names for these devices, rule writing is simple. Here are some examples for my system:

BUS=="ide", KERNEL=="hdf", SYMLINK+="dvd", GROUP="cdrom"
BUS=="ide", KERNEL=="hdg", SYMLINK+="dvdrw", GROUP="cdrom"

Rename Partitions USB Hard Disk

If you have a 200GB USB hard disk, you might want to partition it, in which case we can take advantage of udev's string substitutions:

BUS=="usb", KERNEL=="sd*", SYSFS{product}=="USB 2.0 Storage Device", NAME="%k", SYMLINK+="usbhd%n"This rule creates symlinks such as:

/dev/usbhd -- The fdiskable node
/dev/usbhd1 -- The first partition (mountable)
/dev/usbhd2 -- The second partition (mountable)

Sponsored Link

 Posted by at 2:40 pm

  5 Responses to “Rename Network Interface using Udev in Linux”

  1. Very useful page!
    Thanks, Bob.

  2. Just the information I was needing.
    Thanks for taking time to publish it!

    Regards,
    ///Pablo

  3. great!!!
    Thanks,
    bsurfer

  4. For this rule
    BUS==”usb”, KERNEL==”sd*”, SYSFS{product}==”USB 2.0 Storage Device”, NAME=”%k”, SYMLINK+=”usbhd%n

    Hal says:
    Jun 6 10:39:10 ice udevd[2949]: add_to_rules: invalid rule ‘/etc/udev/rules.d/50-uDiskOnChip.rules:23′

  5. There’s a another way to rename the interfaces. I’m using slackware 12.1 and I changed only the file /etc/udev/rules.d/70-persistent-net.rules where in NAME=”eth1″ I’ve changed to NAME=”eth0″ again.

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>