udev and device files

Intermediate · 10 min

What a device file is, what major and minor numbers mean, how udev reacts to hardware events, and why you should mount disks by UUID, not /dev/sdb.

Before thisKernel modules and drivers

TatOG Tech · 10 min

udev and device files

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by TatOG TechReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A device file in /dev is a handle into a driver, not data on disk. Its type (character or block) and its major and minor numbers tell the kernel which driver and which device to route reads and writes to.
  • The kernel creates the basic nodes itself on a devtmpfs. udev (systemd-udevd) listens for kernel uevents, then loads drivers, sets ownership and permissions, renames network interfaces and adds symlinks.
  • Kernel names like sda, sdb and nvme0n1 follow discovery order and can change between boots. The symlinks under /dev/disk/by-uuid, by-label, by-id and by-path are stable, so use them in fstab and scripts.
  • Rules are read from /usr/lib/udev/rules.d (vendor), /run and /etc/udev/rules.d (yours), in lexical order across all three; a file in /etc with the same name overrides the vendor one.
  • udevadm info shows what udev knows about a device, udevadm monitor shows events live, and udevadm test simulates a rule run. After editing rules, reload and trigger.

A file that is really a driver

Unix’s trick is to make devices look like files, so the same open, read, write and permission checks work on a disk, a terminal or a random-number generator. Read /dev/zero and you get zeros; write to /dev/null and the data disappears. Nothing is stored. The “file” is a pointer into a driver.

That pointer is three pieces of metadata: a type, character (c, a stream of bytes, like a terminal) or block (b, random access in blocks, like a disk), and two numbers. The major number picks the driver. The minor number picks which device that driver should handle.

The kernel’s own registry of assigned numbers, “Linux allocated devices”, lists for example character major 1 as memory devices, where minor 3 is /dev/null and minor 5 is /dev/zero, and block major 8 as SCSI disks, where minor 0 is /dev/sda and 16 is /dev/sdb. Each disk gets 16 minors so partitions can have their own. NVMe and other newer devices draw numbers dynamically, from the “Block Extended Major” 259 for example.

I had no Linux machine, but macOS kept the same model, so here it is on this Mac, labelled as macOS. The numbers differ from Linux’s:

$ ls -l /dev/null /dev/disk0      # macOS, not Linux
brw-r-----  1 root  operator  0x1000000 Nov 24 00:57 /dev/disk0
crw-rw-rw-  1 root  wheel     0x3000002 Nov 28 04:00 /dev/null
$ stat -f '%N %HT major=%Hr minor=%Lr' /dev/null /dev/disk0
/dev/null Character Device major=3 minor=2
/dev/disk0 Block Device major=1 minor=0

On Linux, ls -l /dev/null prints 1, 3 where a file size would go.

Who creates /dev

Device files used to be created once with mknod and shipped on disk, thousands of them for hardware that might exist. Today /dev is a devtmpfs: the UAPI file-hierarchy spec says it is “usually … mounted as a devtmpfs instance” and “managed jointly by the kernel and a userspace component such as systemd-udevd”. The kernel creates a basic node as soon as a driver registers a device. That is why the container images inspected in earlier lessons ship an empty /dev.

The basic node is not enough, though. It is owned by root, it has the kernel’s unpredictable name, and nobody has loaded the driver for the next device yet. That is udev’s job.

What udev does

The udev(7) man page describes the flow. The daemon, systemd-udevd, “receives device uevents directly from the kernel whenever a device is added or removed from the system, or it changes its state”. It matches each event against its rules and can then:

  • load the right driver module, by asking for the device’s modalias, as in the modules lesson;
  • set the node’s owner, group and permissions, so the disk or video group can use it;
  • rename network interfaces to predictable names like enp3s0, covered in the networking group;
  • add symlinks with meaningful, stable names.

The man page states the motivation: “The kernel usually just assigns unpredictable device names based on the order of discovery.”

Stable names for disks

This is the part that matters most on servers. Add a disk, change a controller, or boot with a USB stick plugged in, and what was /dev/sdb can become /dev/sdc. An /etc/fstab line naming /dev/sdb1 then mounts the wrong thing, or fails, or the machine drops to emergency mode.

udev solves it with the rules in 60-persistent-storage.rules, shipped by systemd. I read the current source on GitHub. It probes each block device with the built-in blkid and then adds lines like:

ENV{ID_FS_USAGE}=="filesystem|other|crypto", ENV{ID_FS_UUID_ENC}=="?*", SYMLINK+="disk/by-uuid/$env{ID_FS_UUID_ENC}"
ENV{ID_FS_USAGE}=="filesystem|other|crypto", ENV{ID_FS_LABEL_ENC}=="?*", SYMLINK+="disk/by-label/$env{ID_FS_LABEL_ENC}"

The result is a set of directories pointing at the current kernel name: /dev/disk/by-uuid/ (the filesystem’s UUID), by-label/, by-partuuid/ (the GPT partition’s UUID), by-id/ (model and serial number) and by-path/ (the physical port). Use UUID=... in fstab and root=UUID=... on the kernel command line. The mounting lesson builds on this.

Writing and debugging rules

Rules live in three places. udev(7) says files from the system directory, /run, and /etc/udev/rules.d “are collectively sorted and processed in lexicographic order, regardless of the directories in which they live”, and that a file in /etc with the same name as a vendor file replaces it. Put local rules in /etc/udev/rules.d/ with a number prefix that orders them after the defaults, such as 70- or 99-.

A rule is a list of matches (==) and assignments (=, +=). For example, written from the man page and not tested here, this gives a USB serial adapter a fixed name and a group:

SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{serial}=="A6008isP", SYMLINK+="gps0", GROUP="dialout", MODE="0660"

One trap: a rule’s RUN program must be quick. The man page warns that “starting daemons or other long-running processes is not allowed; the forked processes … will be unconditionally killed”. Start a systemd service with the SYSTEMD_WANTS property instead.

The udevadm tool covers the rest. udevadm info /dev/sda shows the properties and attributes you can match on. udevadm monitor prints events as you plug things in. udevadm test simulates a rule run with debug output. After editing, udevadm control --reload reloads rules, but the man page notes that does “not apply any changes to already existing devices”, so follow it with udevadm trigger or re-plug the device.

About the video

The embed is TatOG Tech’s “Linux Device Management | Udev”, August 2024, 10 minutes, in full. Its chapters are an overview of udev, its components, and commands and monitoring. It is a small channel and has no description; I judged it from its title and chapters, and English from the original English audio track. It does not go into major and minor numbers or the /dev/disk/by-* links in depth, which this lesson covers. DJ Ware’s udev section in deeper, with a live demonstration, is the runner-up.

What was run: the ls and stat above, on macOS, clearly not Linux. The persistent-storage rules were read from systemd’s GitHub source, device numbers from the kernel’s allocated-devices list, and behaviour from udev(7) and udevadm(8), checked 2026-11-28.

Check yourself

  1. 01After adding a new disk, the old /dev/sdb is now called /dev/sdc and a mount fails. How do you stop this happening again?

    Refer to the filesystem by a stable name, such as UUID=... in /etc/fstab or a /dev/disk/by-uuid/ path, instead of the kernel's discovery-order name.

  2. 02What do the major and minor numbers of a device file identify?

    The major number selects the driver (or class of device) and the minor number selects which device that driver handles, for example 1,3 is /dev/null and 8,0 is the first SCSI disk.

  3. 03You wrote a udev rule that runs a script which starts a long-lived daemon. The daemon dies a moment later. Why?

    udev kills processes forked from RUN once event handling finishes. Long-running work must be a systemd service pulled in with the SYSTEMD_WANTS property.

  4. 04You change a rule in /etc/udev/rules.d. Does it apply to devices already present?

    Not by itself. Reload the rules with udevadm control --reload, then replay events with udevadm trigger, or re-plug the device.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark