421. udev and device groups
A device in /dev is usable without root only when two things agree: a udev rule gives the device node to a group, and the account is a member of that group. Neither works alone. This page covers the device manager and how it starts, the groups the shipped account is in, KDOS’s own rules, modules loaded by name and the blacklists, and the procedures for checking a device, granting one to a group and writing a rule of your own.
Installation
| Part | Port or file |
|---|---|
The device manager, udevd and udevadm |
eudev |
Module loading, for udev and for 02_modules |
kmod |
Starting udevd and replaying the devices present at boot |
fs/etc/init.d/01_udev.sh |
| Loading the modules no device asks for | fs/etc/init.d/02_modules.sh |
| KDOS’s rules, modules and blacklist | fs/etc/udev/rules.d, fs/etc/modules-load.d, fs/etc/modprobe.d |
The groups and the kdos account’s memberships |
fs/etc/group |
All of it is in every image, from the base filesystem (see The fs/ overlay). The packages’ own rules are installed in /lib/udev/rules.d, the same directory as /usr/lib/udev/rules.d. The hardware database is compiled once over the finished tree, into /etc/udev/hwdb.bin, by the image step 060_udev_hwdb.sh; after that, kpkg recompiles it whenever a package that installs a .hwdb file is added or removed (see Shared indexes).
Configuration
01_udev
udevd is started by the first service script. Before starting it, the script creates the nodes of the modules that load only when their node is opened, /dev/uhid, /dev/uinput, /dev/snd/seq and the rest; after it, it replays every device already present with the action add, which is what loads their drivers:
. /etc/init.d/service_helper
NAME="udev"
DAEMON="/usr/sbin/udevd"
case "$1" in
start)
if [ ! -x "$DAEMON" ]; then
echo "[SKIP] $NAME: $DAEMON not found"
exit 0
fi
echo "[KDOS] Starting $NAME..."
mkdir -pv /run/udev
chmod 755 /run/udev
# The nodes of modules that load when their node is OPENED: uhid,
# uinput, snd/seq, vhost-net, cuse and the rest of modules.devname.
# No uevent names them before the module loads and nothing loads the
# module until the node exists, so without this /dev/uhid never
# appears and every Bluetooth keyboard and mouse pairs and then types
# nothing. Made BEFORE udevd starts, because udevd applies the rules'
# static_node= ownership (snd/seq to audio, vhost-net to kvm) to the
# nodes it finds at start-up and to no others.
if [ -x /usr/bin/kmod ]; then
/usr/bin/kmod static-nodes --format=tmpfiles 2>/dev/null |
while read -r _t _p _m _u _g _a _dev; do
case "$_t" in c*|b*) ;; *) continue ;; esac
[ -e "$_p" ] && continue
mkdir -p "${_p%/*}"
mknod -m "$_m" "$_p" "${_t%%!*}" "${_dev%%:*}" "${_dev##*:}"
done
fi
"$DAEMON" --daemon
# --action=add, and not the default. udevadm trigger replays every
# device with action "change", but /lib/udev/rules.d/80-drivers.rules
# — the rule that modprobes a driver from MODALIAS — opens with
# ACTION!="add", GOTO="drivers_end". A plain trigger therefore loads
# NO module at all: the sound controller stays unclaimed and ALSA
# finds no card. Subsystems before devices, so a bus module is in
# place before its children are replayed.
/usr/sbin/udevadm trigger --action=add --type=subsystems
/usr/sbin/udevadm trigger --action=add --type=devices
/usr/sbin/udevadm settle
echo "[KDOS] $NAME ready"
;;
stop)
echo "[KDOS] Stopping $NAME..."
killall udevd 2>/dev/null
;;
status)
if pgrep -x udevd >/dev/null 2>&1; then
echo "[ OK ] $NAME is running"
else
echo "[DOWN] $NAME"
fi
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 1
;;
esac
The service is named udev. It leaves no supervised process: udevd runs on its own once the script has started it (/usr/sbin/udevd --daemon), and service status udev reports whether it is running, as [ OK ] udev is running. What the script printed at boot is in /run/kdos-init.udev.log: [KDOS] Starting udev..., then [KDOS] udev ready.
The groups
The shipped /etc/group. The kdos account is in wheel, which administers the machine, in users, and in the hardware groups tty, dialout, audio, video, cdrom, render, input, kvm and lpadmin:
root:x:0:
bin:x:1:
sys:x:2:
kmem:x:3:
tty:x:5:kdos
tape:x:6:
daemon:x:7:
floppy:x:8:
disk:x:9:
lp:x:10:
dialout:x:11:kdos
audio:x:12:kdos
video:x:13:kdos
utmp:x:14:
at:x:15:
cdrom:x:16:kdos
man:x:17:
dial:x:18:
games:x:20:
render:x:28:kdos
input:x:29:kdos
sgx:x:30:
kvm:x:32:kdos
lock:x:54:
sddm:x:64:
sambashare:x:65:
nobody:x:99:
users:x:100:kdos
network:x:101:
tss:x:993:
lpadmin:x:995:kdos
sshd:x:996:
messagebus:x:997:
wheel:x:998:kdos
dhcpcd:x:999:
kdos:x:1000:
On the image id kdos lists one group more, brlapi, which the brltty package creates and adds the account to when it is installed, so that a screen reader can reach the braille display server.
What each hardware group makes usable, and what removing it stops, is in Users and groups.
/etc/udev/rules.d
KDOS’s rules are the 70-kdos-*.rules files here, one per family of device. Each family’s rule grants a device node to a group with mode 0660; some also tag it uaccess, which elogind turns into an access-control entry for the user of the active session on the seat, moved at every session switch, so a device the person at the machine uses needs no group. Two files grant nothing: 70-kdos-live-medium.rules hides the medium the live session runs from, so udisks2 and the desktop never list it or offer to unmount or format it, and 99-qemu-tablet.rules marks QEMU’s emulated USB tablet as a pointer. The GPS receivers’ rule, 70-kdos-gpsd.rules, comes with the gpsd package and is in /lib/udev/rules.d. The serial rule is the shape of most of them:
# KDOS — serial adapters reachable by the console user.
#
# These rules and `kdos`'s membership of `dialout` in fs/etc/group are one
# mechanism: without both, every serial programmer, dev board, instrument and
# GPS in the ports tree is installed and unopenable, and the failure presents
# as a broken cable rather than as a permission.
#
# MODE 0660 with GROUP dialout, not 0666: a serial port is a device other users
# on a multi-user machine have no business reading.
SUBSYSTEM=="tty", KERNEL=="ttyUSB[0-9]*", GROUP="dialout", MODE="0660"
SUBSYSTEM=="tty", KERNEL=="ttyACM[0-9]*", GROUP="dialout", MODE="0660"
SUBSYSTEM=="tty", KERNEL=="ttyAMA[0-9]*", GROUP="dialout", MODE="0660"
SUBSYSTEM=="tty", KERNEL=="ttyS[0-9]*", GROUP="dialout", MODE="0660"
# FTDI, Prolific, Silicon Labs and WCH bridges reached as raw USB rather than
# through the tty layer — libftdi1 and libserialport both open these directly,
# and a tty rule does not cover them.
SUBSYSTEM=="usb", ATTR{idVendor}=="0403", GROUP="dialout", MODE="0660"
SUBSYSTEM=="usb", ATTR{idVendor}=="067b", GROUP="dialout", MODE="0660"
SUBSYSTEM=="usb", ATTR{idVendor}=="10c4", GROUP="dialout", MODE="0660"
SUBSYSTEM=="usb", ATTR{idVendor}=="1a86", GROUP="dialout", MODE="0660"
fs/etc/udev/rules.d/70-kdos-serial.rules
The complete list, with the group each file grants and the devices it covers, is in The device groups and the udev rules. A rule of your own goes in this directory too; a file here with the same name as one in /lib/udev/rules.d replaces the packaged file.
/etc/modules-load.d and 02_modules
A driver that declares nothing udev can match is loaded by name. 02_modules reads every .conf file in /etc/modules-load.d, one module per line, and only that directory:
# Load kernel modules listed in /etc/modules-load.d/*.conf.
# Runs after udev so module aliases are resolvable.
CONF_DIR="/etc/modules-load.d"
[ -d "$CONF_DIR" ] || exit 0
case "$1" in
start)
echo "[KDOS] Loading kernel modules from $CONF_DIR..."
for f in "$CONF_DIR"/*.conf; do
[ -r "$f" ] || continue
while IFS= read -r mod; do
# strip comments and whitespace
mod="${mod%%#*}"
mod="${mod//[[:space:]]/}"
[ -z "$mod" ] && continue
# modprobe's own message is the reason: absent hardware,
# a misspelt name, a module this kernel does not carry
# and a refused option all fail here, and only the
# message tells them apart.
if _err=$(modprobe "$mod" 2>&1); then
echo "[KDOS] loaded: $mod"
else
_err="${_err%%$'\n'*}"
echo "[KDOS] skip: $mod (${_err:-modprobe failed})"
fi
done < "$f"
done
;;
stop|status)
;;
*)
echo "Usage: $0 {start|stop|status}"
exit 1
;;
esac
One of the shipped files, for the I2C character devices that ddcutil needs:
# The character-device interface to the I2C buses, which is how ddcutil reaches
# a monitor's DDC/CI line. Built as a module and never autoloaded: i2c-dev
# declares no modalias, so no uevent names it and udev's autoload path cannot
# reach it. Without this line /dev/i2c-* does not exist and
# 70-kdos-i2c.rules has nothing to grant.
#
# ddcutil ships /usr/lib/modules-load.d/ddcutil.conf saying the same thing.
# 02_modules.sh reads /etc/modules-load.d only, so that copy is inert here.
i2c-dev
fs/etc/modules-load.d/kdos-i2c.conf
/etc/modprobe.d
KDOS’s one blacklist keeps the kernel’s TV-tuner driver off an RTL2832U radio stick:
# The kernel claims an RTL2832U as a DVB-T tuner on plug-in, which leaves
# librtlsdr unable to open a device that is present and enumerated.
# /etc/udev/rules.d/70-kdos-sdr.rules is the access half of the same fix.
blacklist dvb_usb_rtl28xxu
fs/etc/modprobe.d/kdos-sdr.conf
Packages add files of their own beside it, none of them a blacklist: on the image they are stlink_v1.conf, a usb-storage quirk for ST-Link programmers, and nvdimm-security.conf, which loads NVDIMM keys before libnvdimm. The other blacklist is the hwdata package’s /usr/lib/modprobe.d/dist-blacklist.conf, which keeps the old framebuffer drivers, the PC speaker, an Intel watchdog and a few others from loading on their own.
Usage
Checking why a device cannot be opened
Look at the node’s owner, group and mode:
ls -l /dev/ttyUSB0Check your own groups:
idShow what udev knows about the device, and every attribute a rule can match on it and on each parent device:
udevadm info /dev/ttyUSB0 udevadm info -a /dev/ttyUSB0kdos doctorchecks the serial, video-capture and USBTMC nodes that are present (/dev/ttyUSB0to/dev/ttyUSB7, and the same eight numbers of/dev/ttyACM,/dev/videoand/dev/usbtmc), lists the ones you cannot open and names the group that owns each:kdos doctor
Adding an account to a group
Add the group to the account’s list:
sudo usermod -aG dialout aliceLog out and in again. Group membership is read at login, so a running session keeps the groups it started with.
Watching devices arrive
udevadm monitor --udev --property
prints every event udev finishes processing, with its properties, as a device is plugged in or removed. Ctrl+C stops it.
Writing a rule
Find the device’s identifiers with
udevadm info -a, for example its USBidVendorandidProduct.Write a file in
/etc/udev/rules.d, with a number after the files it should follow, for example/etc/udev/rules.d/71-local.rules:SUBSYSTEM=="usb", ATTR{idVendor}=="1234", ATTR{idProduct}=="5678", GROUP="dialout", MODE="0660"Load the rules and replay the devices of that subsystem:
sudo udevadm control --reload sudo udevadm trigger --action=add --subsystem-match=usbUnplugging the device and plugging it in again does the same for that device.
Check the node with
ls -l, as above.
Loading a module at boot
Write the module’s name in a file of
/etc/modules-load.d, for example/etc/modules-load.d/local.conf.Load it for this boot, or wait for the next:
sudo modprobe <module>At boot,
02_moduleslogsloaded:orskip:for each, in/run/kdos-init.modules.log.
Keeping a driver off a device
Write blacklist <module> in a file of /etc/modprobe.d, then unload the module (sudo modprobe -r <module>) or reboot.
Adding to the hardware database
Put a .hwdb file in /etc/udev/hwdb.d under a name no package uses, such as 70-local.hwdb: the packages install their own files in the same directory. Then compile the database and replay the devices:
sudo udevadm hwdb --update
sudo udevadm trigger --action=add
Tips
- Use only the groups that exist in
/etc/group. udev turns a group it does not know into root: it logsspecified group '…' unknownin/var/log/messageswhen it reads the rules, and the node still appears with the rule’s mode, owned byroot:root. A rule copied from elsewhere that namesplugdevtherefore gives the device to root. The upstream rules that grant throughplugdevare not installed for that reason. - A device with no node in
/dev, such as a backlight, cannot takeGROUP=orMODE=: eudev skips the whole line for it,RUN+=included, and reports nothing. The screen’s brightness is set through elogind’sSetBrightnessfor the active session instead, which PowerDevil calls. getfacl /dev/<node>shows the access-control entryuaccessgave the active session’s user.- Match
ACTION=="add"in a rule that should also run for devices present at boot:01_udevreplays them with the actionadd, neverchange.kdos explain coldplugprints the reason. kdos why /etc/udev/rules.d/70-kdos-serial.rulessays what provides a file; a rule from the base filesystem is reported as owned by no package.sudo udevadm control --log-priority=debugraises the runningudevd’s log level, so it logs each rule it applies. The level lasts untiludevdis restarted, or until you set another with--log-priority=.udevdwatches every disk. When a program closes a disk it opened for writing,udevdcompares the partition table on the disk with the kernel’s, by number, start and size: only a table that differs is re-read, which removes and re-adds every partition node, and an unchanged one gets achangeevent for the disk and each of its partitions, which re-runs the rules and leaves the nodes in place. A tool that writes the whole disk and then formats one of its partitions therefore finds the node there, and after a raw write that changes the table, such as an image written withdd, the new partitions appear without a replug orblockdev --rereadpt.- On the live medium, a rule, a group membership or a module file you add is in memory and is lost at power-off unless a persistence store exists (
kdos persist). On an installed system it is on the root filesystem.
Troubleshooting
Permission denied opening a device, and ls -l shows the right group. Your session predates the membership. id lists the groups the current session has; log out and in again.
The node belongs to root:root despite a rule. The group the rule names does not exist, or the rule did not match. udevadm info -a shows the attributes the device really has; compare them with the rule’s keys, reload, and trigger again.
A device present at boot has no driver loaded. Read what the coldplug printed:
cat /run/kdos-init.udev.log
sudo grep udevd /var/log/messages
and check the module by hand with sudo modprobe <module> and dmesg. The system log holds udevd’s own complaints, such as failed to execute '…' for a rule’s program, which means the rule names a program that is not installed. The initramfs’s udevd, which runs in the first second of the boot before the second starting eudev line, logs none: the image build leaves out every rule whose program the initramfs does not carry (see The udev check).
A serial programmer, a board or an instrument looks like a broken cable. That is the missing group, not the cable. Run kdos doctor; it names the group that owns each node you cannot open.
An RTL2832U stick is listed by lsusb and no SDR program can open it. The TV-tuner driver has claimed it. Check lsmod | grep dvb_usb_rtl28xxu; the shipped blacklist keeps it off, so a line there means the module was loaded by hand or the blacklist was removed.
/dev/i2c-* does not exist. i2c-dev is loaded only by 02_modules, from /etc/modules-load.d/kdos-i2c.conf. Check /run/kdos-init.modules.log, or load it with sudo modprobe i2c-dev.
A keyboard or mouse added through Bluetooth or uinput gives no input. The node is made by 01_udev before udevd starts. Check ls -l /dev/uhid /dev/uinput: both exist, owned by root with mode 0600, since the programs that open them (bluetoothd, an input remapper) run as root; see Bluetooth.
Known gaps
- LVM volume groups are activated only at boot: lvm2’s hotplug rule is not installed
lsblkshows a filesystem’s type, label and UUID to root only- A SPICE viewer redirects only the USB devices you can already open
- Smart cards, drawing tablets and software radios have not met hardware: their rules are written and not tried against the devices
- A phone has not been reached over MTP
kdos doctorcannot check hardware a machine does not have
See also
- The device groups and the udev rules: every rule file, the narrow ones, and the modules loaded by name
- Users and groups: what each group grants, and adding an account
- rcS and the service scripts: where
01_udevand02_modulesrun - Databases stamped into the image: the hardware database
- The udev hardware database: the image step that compiles it
- Users and groups, Modules and sysctl, Live persistence, Printing and scanning, Bluetooth, Audio, Services