Manage Linux Hardware with udev (Part 2) - Page 2

Getting around Linux device management can be tricky. Here's the second part of our guide to untangling the mysteries of udev. This week we look at how to connect devices to your system.

 By Carla Schroder
Page 2 of 2   |  Back to Page 1
Print Article

Creating Persistent Device Names

Linux has come a long way in a few short years. Modern distributions handle a multitude of hardware devices, and can hotplug just about everything. The downside is non-persistent device names. Your Linux distribution should come with a batch of pre-fab persistent names for hard drives and CD/DVD drives. But devices like USB storage drives or USB network cards might still be moving targets.

There are two ways to give additional names to devices. One is to add another name, another way is to create symlinks. For example, my second hard drive, /dev/sda, is a mondo 300-gigabyte SATA drive that I use for storing photo archives. I can create a 10-local.rules file with this entry:

# nickname for /dev/sda
KERNEL=="sda", NAME="photo-archives"

After reboot there will be a /dev/photo-archives. I can use this like any storage drive- make an entry in /etc/fstab using the /dev/photo-archives name, mount it manually, whatever I like.

While this is fast and easy, it's better to create a symlink, because symlinks point back to the original device node. Names do not, so I'll have to search /etc/udev to find out what it really is, because I will forget. Run udevinfo to see the drive information:

$ udevinfo -a -p /block/sda
device '/sys/block/sda' has major:minor 8:0
  looking at class device '/sys/block/sda':


Then choose an attribute or two and create a symlink like this:

# nickname for /dev/sda
KERNEL=="sda", SYSFS{size}=="156301488", SYMLINK+="photo-archives"

If you use more than one sysfs attribute, make sure they come from the same section of the udevinfo output, or it won't work. Mind your punctuation- the KERNEL and SYSFS values are called match keys, and must use the equality operator or ==. SYMLINK is an assignment key. The plus sign means "add this symlink to the list of symlinks", and = means "assign this value". If you leave off the plus sign, you'll overwrite the existing symlinks.

I added the size for insurance, as it's the only drive of that size on the system. If either of the match keys change the rule will not work.

Reboot and, hurrah, the new symlink shows up just like it's supposed to:

 $ ls -l /dev/photo-archives
lrwxrwxrwx 1 root root 3 2006-10-09 22:19 /dev/photo-archives -> sda

Nailing Down USB Network Interfaces

Ordinary PCI Ethernet cards are best given permanent name assignments by using ifrename on Debian systems, and via the usual configuration methods on Fedora. (See Nail Down Network Interface Names with ifrename.) USB NICs are another kettle of clams entirely, but a simple one. All you need is the MAC address and then a rule like this example for a wireless USB NIC:

KERNEL=="wlan*", SYSFS{address}=="", NAME="wlan0"

You don't have to use wlan0; you can call it anything you like- fred, barney, wan-thing, whatever you want. Just remember to use the name in all of your configuration and startup files.

You may use /sbin/ifconfig -a to obtain MAC addresses and kernel names, or udevinfo:

$ udevinfo -a -p /sys/class/net/wlan0

All network interfaces are in /sys/class/net/.

Reloading Udev Changes

On Kubuntu you must reboot to activate changes, as it uses a very different udev implementation with a customized command set. (Translation: lots of missing bits, but lovely rules.d files.) On Fedora and its relatives, reload rule changes and restart udev with these commands:

# udevcontrol reload_rules
# udevstart

This is all rather a lot of hassle, but keep in mind that udev is still a youngun, so sometime down the road we're bound to see some nicer tools for customizing udev rules. Especially if some inspired user helps to create them.


  • man udev
  • Writing udev rules. This howto by Daniel Drake is the ultimate resource for learning how to write udev rules.
This article was originally published on Apr 15, 2008
Get the Latest Scoop with Networking Update Newsletter