From Gentoo Wiki
Jump to:navigation Jump to:search
This page is a translated version of the page Udev and the translation is 31% complete.
Other languages:
Deutsch • ‎English • ‎español • ‎français • ‎русский • ‎中文(中国大陆)‎ • ‎日本語 • ‎한국어

Warning: Display title "udev/zh-cn" overrides earlier display title "Udev".


Not to be confused with eudev.

udev(user /dev)是systemd的 Linux内核设备管理器。它管理/dev 中的设备节点,并在添加或删除设备时处理所有用户空间操作。

什么是 udev?


大多数Linux用户都知道 /dev/sda1 只是内核找到的第一个磁盘上的第一个分区的快速方法。这很简单,对吧?

但请考虑USB,IEEE 1394,热插拔PCI等热插拔设备。这些设备的第一个设备是什么?而且持续多久?当第一个设备消失时,其他设备会被命名为什么?这将如何影响正在进行的事务?如果一个打印作业突然从高端激光打印机转移到几乎死机的矩阵打印机,仅仅是因为有人决定拔下激光打印机上的插头(恰好是第一台打印机),这会不会很有趣?

进入设备管理器。一个现代设备管理器(包括 udeveudev)必须:

  • 在用户空间中运行。
  • 动态创建和删除device file
  • 提供一致的设备命名。
  • 提供用户空间应用程序接口(API)。

每当设备结构发生变化时,内核都会发出一个由设备管理器获取的 uevent ,然后设备管理器遵循 /etc/udev/rules.d, /run/udev/rules.d/lib/udev/rules.d 目录中声明的规则,根据uevent中包含的信息,它会找到触发和执行所需操作所需的规则。这些动作可能涉及设备文件的创建或删除,还可能触发将特定固件文件加载到内核内存中。


更新udev时, 从 udev 升级指南 获取信息,以防止系统无法启动。


udev 需要以下内核选项:

General setup  --->
    [*] Configure standard kernel features (expert users)  --->
        [ ] Enable deprecated sysfs features to support old userspace tools
        [*] Enable signalfd() system call
Enable the block layer  --->
    [*] Block layer SG support v4
Networking support  --->
    Networking options  --->
        <*> Unix domain sockets
Device Drivers  --->
    Generic Driver Options  --->
        ()  path to uevent helper
        [*] Maintain a devtmpfs filesystem to mount at /dev
    < > ATA/ATAPI/MFM/RLL support (DEPRECATED)  --->
File systems  --->
    [*] Inotify support for userspace
    Pseudo filesystems --->
        [*] /proc file system support
        [*] sysfs file system support

USE 标记

Portage knows the udev USE flag for enabling support for udev in other packages. Adding this USE flag value to the USE flag list (default in all Linux profiles) will pull in the virtual/udev package automatically:

FILE /etc/portage/make.conf

USE flags for sys-fs/udev Linux dynamic and persistent device naming support (aka userspace devfs)

acl Add support for Access Control Lists
kmod Enable kernel module loading/unloading support using sys-apps/kmod
selinux !!internal use only!! Security Enhanced Linux support, this must be set by the selinux profile or breakage will occur
split-usr Enable behavior to support maintaining /bin, /lib*, /sbin and /usr/sbin separately from /usr/bin and /usr/lib*
test Enable dependencies and/or preparations necessary to run tests (usually controlled by FEATURES=test but can be toggled independently)


设置 USE 标志后更新系统以使更改生效:

root #emerge --ask --changed-use --deep @world



要在引导时启动 udev,请将其添加到系统初始化运行级别。这可以通过使用 root 权限运行以下命令来完成:

root #rc-update add udev sysinit



udev provides a set of rules that match against exported values of uevents (events sent by the kernel) and properties of the discovered device. A matching rule will possibly name and create a device node and run configured programs to setup and configure the device.

The rule definitions are stored in two locations:

  1. /lib/udev/rules.d/ - Rules in this directory are installed by certain packages, they generally should not be changed by users;
  2. /etc/udev/rules.d/ - This folder is for end-user specified rules. Any new rules should be added in this directory;

In these directories, multiple rule files (with suffix .rules) are traversed in alphanumerical order. Inside the rules files, udev will find expressions that might match a uevent together with the state to match (is the uevent because a device is added or removed) and the command to execute.

The event matching is based on information such as:

  • The SUBSYSTEM of the uevent (for which type of device is the uevent fired);
  • The ACTION that is taken (add, change, or remove);
  • One or more attributes (through ATTR or ATTRS), such as the device class, vendor or other device information;
  • The kernel-provided name (through KERNEL), such as sd* (for SCSI/SATA disks) or input* (for input devices such as mice and keyboards);
  • One or more environment settings (through ENV), used to send information between multiple rules.

Based on this information, the rule can then state if:

  1. Some information needs to be shared with later events (through environment variables)
  2. Links need to be created in /dev
  3. Commands need to be executed

Udev does this for every rule that matches (it does not stop after the first match) to allow a flexible device management approach.

Persistent device names

The kernel detects devices asynchronously, udev mirrors the kernel's sysfs filesystem and so the devices are named and numbered in order of detection. So by default udev provides no persistent device names. However there are mechanisms for some device classes to provide these:

  • Udev creates for storage devices additional symlinks based on the device's ID, label, UUID and path. See the /dev/disk/by-* directory. So instead of using e.g. the device file /dev/sda use the file /dev/disk/by-label/SOME_LABEL.
  • The same for input devices in the /dev/input directory.
  • Using custom rules enables users to create their own device files.


Some useful commands are:

  • Show all messages about a given device file:
root #udevadm info --query=all --name=/dev/DEVICE_FILE
  • Monitor udev activities:
root #udevadm monitor
  • Udev rules file example - assign a persistent name to an Ethernet device:
FILE /etc/udev/rules.d/50-ethernet.rules
SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="01:23:45:67:89:ab", NAME="eth0"
  • After changing a rules file in /etc/udev/rules.d/, either reboot or make udev reload them with:
root #udevadm control --reload-rules
  • For already added devices (plugged USB, etc.), the reloaded rules take effect either after changing the device (replugging USB, etc.) or by making udev request kernel events again via udevadm trigger. Various event actions can be triggered. By default, the change event action is triggered:
root #udevadm trigger

See the udevadm man page for more information.


Log monitor messages

To log all message when udevadm monitor is ran, modify the following configuration file:

FILE /etc/conf.d/udev

It will create the new log file located at /run/udev/udevmonitor.log

Debug mode

Enabling debug mode will output more log messages:

FILE /etc/conf.d/udev

Set the logging priority:

FILE /etc/udev/udev.conf

The log file /run/udevdebug.log will be created but no messages will be logged to it. The most recent versions of udev will log all messages to dmesg.

Missing device files /dev/null and /dev/console

Some udev versions need the /dev/null and /dev/console files in order to work properly, but can not create them on their own. To manually create these files for udev run the following commands with root privileges:

root #mkdir test
root #mount --bind / test
root #cd test/dev
root #mknod -m 660 console c 5 1
root #mknod -m 660 null c 1 3
root #cd ../..
root #umount test
root #rmdir test

NIC assigned eth0, but is moved to eth1

Those having dual network cards on their motherboards may run into a situation where ifconfig may show no eth0 or eth1. dmesg may show their NIC detected as eth0, and later moved to eth1. Performing a ifconfig -a will also show the NIC as eth1. This is caused by using the kernel assigned names in the first place. Users should write custom rules like /etc/udev/rules.d/70-my-network.rules to use free names like lan0 or wireless0 or use predictable interface names (which have been enabled by default since udev version 197).

Remember to also remove old files from old versions of udev:

root #rm /etc/udev/rules.d/70-persistent-net.rules /etc/systemd/network/99-default.link

Also make sure not to pass net.ifnames=0 on the kernel commandline. This setting would disable the predictable interface names feature of udev altogether.

See also

External resources

Bug 575718 - Request for council decision regarding virtual/udev default provider - Default virtual/udev provider changed to sys-fs/eudev