Kernel Korner - udev—Persistent Device Naming in User Space

Whether you're plugging a camera and scanner in to your laptop or adding another SCSI drive to your company server, it's time to end the current mess of major and minor numbers.

So, back to our two-printer example. To name both of these devices in a consistent manner, the following two udev rules might be used:

BUS="usb", SYSFS_serial="W09090207101241330", \
BUS="usb", SYSFS_serial="HXOLL0012202323480", \

These rules cause udev to look at the sysfs file serial for both printers and, depending on the value in the file, name the printer either lp_color or lp_plain. This ensures that no matter which device was plugged in first or detected first or whether another USB printer is added to the system, both of the printers have the same persistent name.

Advanced udev Rules

udev allows a number of printf-like string substitutions to be used in the NAME, SYMLINK and PROGRAM fields in the udev.rules file. These fields are:

  • %n: kernel number of the device; for example, the device sda3 has a kernel number of 3.

  • %k: kernel name for the device.

  • %M: kernel major number for the device.

  • %m: kernel minor number for the device.

  • %b: bus ID for the device.

  • %c: PROGRAM returned string. A number can be added to this modifier to pick up only a specific word within the string. This field does not work within the PROGRAM field for the obvious reason.

  • %%: % character itself.

Also, a number of the different keys support a simple form of shell-style pattern matching. These patterns are:

  • *: matches zero, one or more characters.

  • ?: matches any single character but does not match zero characters.

  • [ ]: matches any single character specified within the brackets; for example, the pattern string tty[SR] would match either ttyS or ttyR. Ranges also are supported within this match with the - character. For example, to match on the range of all digits, the pattern [0-9] would be used. If the first character following the [ is !, any character not enclosed is matched.

Because of the ability to do these simple string substitutions and string pattern matching, combined with the ability to have udev run any other program and use its result, udev has become an extremely flexible tool for naming devices. As an example of this power, take a look at the following rule:

KERNEL="[hs]d[a-z]", PROGRAM=" %M %m", \
NAME="%1c", SYMLINK="cdrom"

This rule matches any block device and calls the Perl script with the major and minor number of the device. If this program is successful, udev uses the first word of the program's output to name the device and creates a symlink called cdrom. The script can be found in the udev release.

What this script does is determine whether the device is a CD-ROM device. If it is, it queries the Free CDDB database to see if the CD-ROM present in the device is known in the database. If it is, it then names the device based on the CD. For example, my /dev looks like the following when using this rule:

$ ls -l /dev/S* /dev/cdrom
brw-------  1 root root 22, 64 Feb 15 08:26 /dev/Samiam-Astray
lrwxrwxrwx  1 root root      8 Feb 15 08:26 /dev/cdrom ->

This shows how udev can query a database across the Internet to determine how to name a device. Yes, it's a crazy naming scheme to try to use, but it shows how powerful and flexible udev can be.


The author would like to thank Daniel Stekloff of IBM, who has helped shape the design of udev in many ways. Without his perseverance, udev would not be available at all. Also, Kay Sievers has been instrumental in implementing the majority of the advanced features in udev, most notably the ability to have string modifiers and pattern matching, both of which are essential for using udev in the real world. Without his help, udev would not be anywhere near as powerful and useful as it is.

Also, without Pat Mochel's sysfs and driver model core, udev would not have been possible to implement. The author is indebted to him for undertaking what most thought of as an impossible task and for allowing others to build easily on his common framework, allowing all users to see the “Web woven by a spider on drugs” that the kernel keeps track of.

This article is based on the Ottawa Linux Symposium 2002 paper on udev (see Resources).

Resources for this article: /article/7496.

Greg Kroah-Hartman currently is the Linux kernel maintainer for a variety of different driver subsystems. He works for IBM, doing Linux kernel-related things, and can be reached at



Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

What about Garrick?

Anonymous's picture

After reading this fine article, I'm still confused about Garrick. Did he respond and is the font I'm seeing already the small one, or did he utterly ignore all requests directed at him? Would be great if somebody could provide some insight. Thanks.

Changing device order and modprobe.d

Anonymous's picture

I think I have been under the misapprehension that it is possible to change device order or allocation using udev rules. I wanted to make dsp dsp1 and dsp1 dsp, but it seems to me that that is impossible without changing the boot order first. Assigning names to devices only identifies them regardless of the boot order. However it is not possible to reassign those devices following startup, it's all locked up in /sys.
Also, how does /etc/modprobe.d fit into the scheme of aliases/device naming in light of udev. How is the modalias file in /sys applied to /etc/modprobe.d.

Writing udev rules is quite easy with...

VilleWitt's picture

I recently needed some custom stuff in /dev on my GNU/Linux Gentoo. Somehow I cam across this wounderfull article about how to actually implement some of all this stuff - and it works, is easy and very customizable.

Most of the problems people tend to smoke about would be solved by reading that document. These problems are
"They should be named with this syste: ...",
"I want a copy of a device node at ...",
"XXX application can't find /dev/YYY - udev is bad!",
udev rocks!

udev ruined Mepis

Anonymous's picture

I had to switch from Mepis to SUSE 10.0 because of problems with udev. As far as I can see it's not ready for prime time. THe fact of the matter is it wrecked a lot of previously good Linux distributions. Try managing an iPod SUffle with udev. It's as good as impossible.

udev and why I will not use the 2.6 kernel

Anonymous's picture

I am a gentoo user with to installations, one based on the 2.4 kernel and the other on the 2.6 kernel. I decided to do a system upgrade over the new year. What a mistake. I no longer have a useable modem, DRI still does not function and I suspect the problem may, in fact, be udev, which I have yet to determine how to correctly set rules for even with the docs currently available. Add to this that I once was able to view movies under linux I am no longer able to do so.

I am willing to admit I just do not have the big picture on this, but at the same time I have misgivings as to why such a system is really necessary. IF it is the default for th 2.6 kernel I can be sure of one thing...I will never use that kernel further.

Nice try, but no cigar.

.I will never use that kernel further.

Bill Savoie's picture

It is not good to smoke Cigars. You are quick to judgement and that makes you closed off to change. The mind is like a parachute, it only functions when it is open. Your attitude is like a child who throws a tantrum. udev is a great improvement and these people work for free. How about a bit more grace. Make peace and love your path..

auto-load drivers a la devfs

Arioch's picture

FAQ says that driver autoloading is not possible with udev and will not be implemented by ydev maintainer.

But i think there still are projects to achieve this.
For example in it is done by tmpfs + modules_lookup script

I wonder, if there is somewhere a list of such a projects?

Program field

phorm's picture

Well, you could try ysing the program field in udev to fire off an event to another program that could do driver lookups?

BUS="scsi", PROGRAM="/sbin/checkdrv", RESULT="somevalue" might be a start?

Trying sysfs with a USB memory stick

Anonymous's picture

Maybe it's more of a sysfs question, but I immediately tried Greg's suggestion with a USB memory stick. I have several of them, and they always appear at another /dev/sdx or /dev/sdx[1-3] device so this is a proper candidate for udev.

But nothing seems to appear in /sys/class/usb. An entry does appear in /sys/block/sda, but there's no identifying information like a serialno there, and there are suddenly 4 subdirs underneath: sda[1-4].

Is my sysfs set up correctly? How do I figure out the right partition (if any) to mount on such a 'pseudo disk' device?

Re: Kernel Korner: Kernel Korner: udev--Persistent Device Naming

Anonymous's picture

Given that the probe function for every registered USB driver will be called when a device is detected on the USB, what prevents the current lp driver from always choosing the same minor number for the same device regardless of location within the USB. Creating lp1 before or without an lp0 would not cause any problems, would it ?

Re: Kernel Korner: Kernel Korner: udev--Persistent Device Naming

Anonymous's picture

what prevents the current lp driver from always choosing the same minor number for the same device regardless of location within the USB

You are proposing that the kernel device driver should maintain a database for the every device it has handled over the time? This sounds like a really bad idea. A lot of devices are very difficult to distinguish from another, e.g. EPSON has the same USB ID's for a lot of printer models and you need to query the printer information with a vendor specific protocol, which surely belongs to userspace.