udev — Dynamic device management
udev supplies the system software with device events, manages permissions of device nodes and may
create additional symlinks in the /dev/ directory, or renames network interfaces.
The kernel usually just assigns unpredictable device names based on the order of discovery. Meaningful
symlinks or network device names provide a way to reliably identify devices based on their properties or
current configuration.
The udev daemon, systemd-udevd.service(8), receives device uevents directly from the kernel whenever a device is added or removed from the system, or it changes its state. When udev receives a device event, it matches its configured set of rules against various device attributes to identify the device. Rules that match may provide additional device information to be stored in the udev database or to be used to create meaningful symlink names.
All device information udev processes is stored in the udev database and sent out to possible event subscribers. Access to all stored data and the event sources is provided by the library libudev.
This manual page documents the udev rule language. The rules are processed for every device event; see udevadm(8) for tools to inspect devices and test rules.
The udev rules are read from the files located in the system rules directories
/usr/lib/udev/rules.d and /usr/local/lib/udev/rules.d, the
volatile runtime directory /run/udev/rules.d and the local administration directory
/etc/udev/rules.d. All rules files are collectively sorted and processed in
lexicographic order, regardless of the directories in which they live. However, files with identical
filenames replace each other. Files in /etc/ have the highest priority, files in
/run/ take precedence over files with the same name under
/usr/. This can be used to override a system-supplied rules file with a local file
if needed; a symlink in /etc/ with the same name as a rules file in
/usr/lib/, pointing to /dev/null, disables the rules file
entirely. Rule files must have the extension .rules; other extensions are
ignored.
Rules files are line-based text files. Each line contains at most one rule, which consists of one
or more tokens (see below). A line ending with a backslash ("\") is joined with the
following line, and the result is processed as a single line. Lines beginning with "#"
are ignored, even within a continuation. An empty or whitespace-only line ends a pending continuation and
is otherwise ignored.
A rule consists of a comma-separated list of one or more tokens (key-operator-value expressions). Each token has a distinct effect, depending on the key and operator used. A rule may contain both matching and assigning tokens: the assignments are applied only if all matches succeed.
The general format of a token is:
key[{attribute}]operator[modifier]"value"
with the following parts:
key¶One of the key names described in the "Match Keys" and
"Assignments" sections below, for example KERNEL or
SYMLINK. The key determines what is matched against or assigned.
attribute¶An optional key-specific argument, enclosed in braces. For example,
ENV{ takes a property name,
property}ATTR{ takes a sysfs attribute name,
sysfs attribute}IMPORT{ takes an import type, and
type}TEST{ takes an octal file mode mask. Keys that
do not take an attribute must not be given one.mask}
operator¶One of "==", "!=", "=",
":=", "+=" or "-="; see
"Operators" below. Not every key accepts every operator; the allowed operators
are listed for each key.
modifier¶An optional prefix attached to the value: "e" for C-style escaped strings
and "i" for case-insensitive matching; see "Values"
below.
value¶The value to match against or assign, written as a double-quoted string; see
"Values" below. Depending on the key, the value is interpreted as a pattern (see
"Pattern Matching") and may contain string substitutions (see "String
Substitutions").
For example, in the rule
SUBSYSTEM=="net", ATTR{address}=="52:54:00:12:34:56", NAME="lan0"the first two tokens are matches (on the subsystem and on a sysfs attribute), and the third token assigns the interface name. The interface is only renamed if both matches succeed.
Tokens are separated by commas, optionally surrounded by whitespace. Although tokens may be
written in any order, they are evaluated in a fixed internal order, determined by the key: all match
keys are evaluated before all assignment keys, regardless of where they appear in the rule. The full
order and its consequences are described in "Token Processing Order" below.
The tokens of a rule are evaluated in the following order, determined by the key. Tokens with the same key and attribute are evaluated in the order they are specified. The assignments are applied only if all matches succeed.
The plain match keys: ACTION, DEVPATH,
KERNEL, SYMLINK, NAME,
ENV, CONST, TAG,
SUBSYSTEM, DRIVER, ATTR, and
SYSCTL
The parent device match keys: KERNELS, SUBSYSTEMS,
DRIVERS, ATTRS, and TAGS, evaluated
together while walking up the device chain (see "Matching Parent Devices"
below)
TEST
PROGRAM
IMPORT{file}
IMPORT{program}
IMPORT{builtin}
IMPORT{db}
IMPORT{cmdline}
IMPORT{parent}
RESULT
OPTIONS
OWNER, GROUP, and MODE
TAG
SECLABEL
ENV
NAME
SYMLINK
ATTR
SYSCTL
RUN{builtin}
RUN{program}
This order has several consequences:
No match key can see properties assigned in the same rule.
RESULT is evaluated after all PROGRAM keys of the rule,
so it is matched against the output of the last executed program.
Properties imported by IMPORT{} are visible to all assignments of the
rule, while properties assigned by ENV are visible only to the assignments that
follow it in the order above: NAME, SYMLINK,
ATTR, SYSCTL, and RUN.
The OPTIONS string-escape settings affect the NAME,
SYMLINK, and ENV assignments of the same rule.
=="¶Compare for equality: the specified key has the specified value. The value may be a
pattern; see "Pattern Matching" below.
!="¶Compare for inequality: the specified key does not have the specified value or the specified key is not present at all. The value may be a pattern.
="¶Assign a value to a key. Keys that represent a list are reset and only this single value is assigned.
+="¶Add the value to a key that holds a list of entries.
-="¶Remove the value from a key that holds a list of entries.
:="¶Assign a value to a key finally; disallow any later changes.
Values are written as double-quoted strings, such as '"string"'. To include a quotation mark (")
in the value, precede it by a backslash, i.e. '\"'. Any other occurrences of a backslash followed by a
character are not unescaped. That is, "\t\n" is treated as four characters: backslash,
lowercase "t", backslash, lowercase "n".
The string can be prefixed with a lowercase "e" to mark the string as C-style
escaped; see
Escape sequences in C.
For example, 'e"string\n"' is parsed as 7 characters: 6 lowercase letters and a newline. This can be
useful for writing special characters when a kernel driver requires them.
The string can be prefixed with a lowercase "i" to mark that the string or
pattern will match case-insensitively. For example, 'i"foo"' will match "foo",
"FOO", "FoO", and so on. The prefix can be used only for match
("==") or unmatch ("!=") rules, e.g.
ATTR{foo}==i"abcd". The "e" and "i" prefixes may
be combined, in either order, e.g. 'ei"foo"'.
Please note that NUL is not allowed in either string variant.
The values of most match keys are interpreted as patterns: they support shell glob pattern
matching and alternate patterns. This applies to the ACTION,
DEVPATH, KERNEL, SUBSYSTEM,
DRIVER, SYMLINK, NAME, ENV,
CONST, TAG, ATTR, SYSCTL,
KERNELS, SUBSYSTEMS, DRIVERS,
ATTRS, TAGS, and RESULT match keys. The values
of TEST, PROGRAM, and IMPORT are not patterns:
they are file paths and command lines (the value of IMPORT{parent} is a single glob
pattern matched against property names, and does not support alternate patterns). The following special
characters are supported in patterns:
*"¶Matches zero or more characters.
?"¶Matches any single character.
[]"¶Matches any single character specified within the brackets. For example, the pattern string
"tty[SR]" would match either "ttyS" or "ttyR".
Ranges are also supported via the "-" character. For example, to match on the
range of all digits, the pattern "[0-9]" could be used. If the first character
following the "[" is a "!", any characters not enclosed are
matched.
|"¶Separates alternative patterns. For example, the pattern string "abc|x*"
would match either "abc" or "x*".
An empty alternative — produced by a leading, trailing, or doubled "|", e.g.
"|foo" — matches an unset or empty value. For example,
ENV{FOO}=="bar|" matches when the property is set to "bar" or is
unset or empty. To test that a value is set and non-empty, the patterns '=="?*"' and
'!=""' are commonly used, e.g. ENV{FOO}=="?*". The
ATTR and ATTRS keys are an exception: if the attribute cannot be
read, the token is false regardless of the pattern, even with the "!="
operator.
The SYMLINK and TAG match keys test a list of values
(symlinks, tags); the token matches if any of the values matches. With the "!="
operator, the token is true only if none of the values matches.
For SUBSYSTEM, the value "subsystem" matches the devices that
represent the subsystem enumeration directories themselves.
Several keys and substitutions accept an attribute name in the following format, which addresses a specific device by its subsystem and kernel name instead of the event device:
[SUBSYSTEM/KERNEL]attribute
For example, the sysfs attribute "/sys/class/dmi/id/product_name" can be matched
with
ATTR{[dmi/id]product_name}=="ThinkPad*"The referenced device is looked up directly: the attribute is read from (or written to) it,
regardless of the event device or its parents. Attribute names in this format are looked up literally
by ATTR, ATTRS, and $attr{}: string
substitutions are not applied to them. TEST is an exception: its path is
substituted before the device reference is resolved, so e.g.
TEST=="[net/$env{INTERFACE}]operstate" is supported.
The format is supported by the following keys and substitutions:
ATTR{, for both matching and
assignment,attribute}
ATTRS{ (the attribute is read from
the referenced device instead of the devices along the devpath),attribute}
TEST (as a leading prefix of a relative path, resolved to the referenced
device's sysfs path), and
the $attr{ substitution.attribute}
The following tables summarize the keys, the attributes they take, the operators they accept, whether their match values are patterns, and where string substitutions are applied. See the next two sections for the detailed descriptions.
Match keys:
Table 1. Match keys
| Key | Attribute | Operators | Patterns | Substitutions |
|---|---|---|---|---|
ACTION | — | "==", "!=" | yes | — |
DEVPATH | — | "==", "!=" | yes | — |
KERNEL | — | "==", "!=" | yes | — |
SUBSYSTEM | — | "==", "!=" | yes | — |
DRIVER | — | "==", "!=" | yes | — |
SYMLINK | — | "==", "!=" | yes | — |
NAME | — | "==", "!=" | yes | — |
ENV | property name | "==", "!=" | yes | — |
CONST | "arch", "virt", "cvm" | "==", "!=" | yes | — |
TAG | — | "==", "!=" | yes | — |
ATTR | sysfs attribute | "==", "!=" | yes | attribute name |
SYSCTL | kernel parameter | "==", "!=" | yes | attribute name |
KERNELS | — | "==", "!=" | yes | — |
SUBSYSTEMS | — | "==", "!=" | yes | — |
DRIVERS | — | "==", "!=" | yes | — |
ATTRS | sysfs attribute | "==", "!=" | yes | attribute name |
TAGS | — | "==", "!=" | yes | — |
TEST | file mode mask | "==", "!=" | no (file path) | value |
PROGRAM | — | "==", "!=" | no (command line) | value |
IMPORT | import type | "==", "!=" | no | value (except "db" and "cmdline") |
RESULT | — | "==", "!=" | yes | — |
Assignment keys:
Table 2. Assignment keys
| Key | Attribute | Operators | Substitutions |
|---|---|---|---|
NAME | — | "=", ":=" | value |
SYMLINK | — | "=", ":=", "+=", "-=" | value |
OWNER, GROUP, MODE | — | "=", ":=" | value |
SECLABEL | LSM module name | "=", "+=" | value |
ENV | property name | "=", "+=" | value |
TAG | — | "=", "+=", "-=" | value |
ATTR | sysfs attribute | "=" | value |
SYSCTL | kernel parameter | "=" | attribute name and value |
RUN | "program", "builtin" | "=", ":=", "+=" | value |
LABEL | — | "=" | — |
GOTO | — | "=" | — |
OPTIONS | — | "=" | — |
The following keys can be used to match against device properties. With the "=="
and "!=" operators, they decide whether the rule (and thus the assignments in it) is
applied.
These keys are matched against the event device itself:
ACTION¶Match the name of the event action, e.g. "add" or
"remove". See the "Event Actions" section for the list of
actions and their meaning.
DEVPATH¶Match the devpath of the event device, e.g.
"/devices/pci0000:00/0000:00:14.0/usb1/1-2/1-2:1.0/ttyUSB0/tty/ttyUSB0".
KERNEL¶Match the name assigned by the kernel to the event device, e.g. "sda" or
"ttyUSB0".
SUBSYSTEM¶Match the subsystem of the event device, e.g. "net" or
"block".
DRIVER¶Match the driver name of the event device. Only set this key for devices which are bound to a driver at the time the event is generated.
SYMLINK¶Match the name of a symlink targeting the device node, without the
/dev/ prefix, e.g. "disk/by-label/root". It can be used
once a SYMLINK key has been set in one of the preceding rules. There may be multiple symlinks;
only one needs to match. If the operator is "!=", the token returns true only if
there is no symlink matched.
NAME¶Match the name of a network interface. It can be used once the NAME key has been set in one of the preceding rules.
ENV{property}¶Match against a device property value, e.g. properties set by the kernel (such as
"DEVTYPE" or "MODALIAS"), imported by
IMPORT{}, or assigned by earlier rules. An unset property is matched as the
empty value; see "Pattern Matching" above.
CONST{key}¶Match against a system-wide constant. Supported keys are:
arch"¶System's architecture. See ConditionArchitecture= in
systemd.unit(5)
for possible values.
virt"¶System's virtualization environment. See systemd-detect-virt(1) for possible values.
cvm"¶System's confidential virtualization technology. See systemd-detect-virt(1) for possible values.
Unknown keys will never match.
TAG¶Match against one of device tags. It can be used once a TAG key has been set in one of the
preceding rules. There may be multiple tags; only one needs to match. If the operator is
"!=", the token returns true only if there is no tag matched.
ATTR{filename}¶Match the value of a sysfs attribute of the event device, e.g.
ATTR{address} for a network interface. The attribute name may be a relative
path with "/" separators, e.g. ATTR{power/control}, and may
also address another device with the "[SUBSYSTEM/KERNEL]attribute" format; see
"Referring to Other Devices" above.
Trailing whitespace in the attribute values is ignored unless the specified match value itself contains trailing whitespace.
The attribute name supports string substitutions, unless it is specified in the
"[SUBSYSTEM/KERNEL]attribute" format, which is looked up literally; see
"String Substitutions" below.
SYSCTL{kernel parameter}¶Match the value of a kernel parameter (sysctl), e.g.
SYSCTL{kernel/hostname}. Both "/" and "."
may be used as separators in the parameter name. The parameter name supports string
substitutions; see "String Substitutions" below.
Devices are organized hierarchically in sysfs: every device except the root of its chain has a parent device (for example, a partition is a child of its disk, which is a child of the storage controller it is attached to). The following keys match against the devices along this chain:
KERNELS¶Search the devpath upwards for a matching device name.
SUBSYSTEMS¶Search the devpath upwards for a matching device subsystem name.
DRIVERS¶Search the devpath upwards for a matching device driver name.
ATTRS{filename}¶Search the devpath upwards for a device with a matching sysfs attribute value. Trailing
whitespace in the attribute values is ignored unless the specified match value itself contains
trailing whitespace. The attribute may also address another device with the
"[SUBSYSTEM/KERNEL]attribute" format; see "Referring to Other
Devices" above.
Referencing attributes through the "device/" symlink or through
"../" (e.g. ATTRS{device/serial} or
ATTRS{../serial}) is discouraged: these paths may not be available in future
kernels. Prefer matching the attribute of the parent device directly, as shown by
udevadm info --attribute-walk.
TAGS¶Search the devpath upwards for a device with matching tag. If the operator is
"!=", the token returns true only if there is no tag matched.
The search starts with the event device itself and continues towards the root of the device chain. If several parent match keys are specified in a single rule, all of them must match one and the same device in the chain (although different rules may match at different levels). If no device in the chain satisfies all parent match keys, the rule does not match.
The device where the parent search succeeded can be referenced in substitutions:
$id expands to its kernel name and $driver to its driver name;
see "String Substitutions" below. For example, in
SUBSYSTEM=="block", ENV{DEVTYPE}=="disk", SUBSYSTEMS=="usb", ATTRS{serial}=="0123456789", SYMLINK+="usb-$id"both SUBSYSTEMS and ATTRS must match the same USB device,
and "$id" expands to the kernel name of that USB device (e.g.
"3-2").
TEST[{mask}]¶Test the existence of a file. The value is a file path; if it is relative, it is taken
relative to the sysfs path of the event device, unless it starts with a
"["
prefix, which is resolved to that device's sysfs path (see "SUBSYSTEM/KERNEL]Referring to Other
Devices" above). A "/*/" sequence in the path is expanded: the
"*" is replaced by the first non-hidden entry of the preceding directory for
which the remainder of the path exists; only the first such sequence is expanded, and a
"*" that is not a whole path component is taken literally. The value supports
string substitutions, see "String Substitutions" below.
Optionally, an octal file mode mask may be specified as the attribute; the token then matches if the file exists and any of the bits set in the mask are also set in the file's mode. For example, the following rule matches block devices whose device node is writable by its owner:
SUBSYSTEM=="block", TEST{0200}=="$devnode", ENV{NODE_OWNER_WRITABLE}="1"PROGRAM¶Execute a program to determine whether there is a match; the key is true if the program
returns successfully. The program's standard output is available in the RESULT
key. How the command line is specified, and which restrictions apply to the executed program, are
described in "Program Execution" below.
Multiple PROGRAM keys may be specified in one rule; they are executed
sequentially, in the order they are specified, and the output of the last executed program is
used for the RESULT match. As with any other match key, the remaining tokens
of the rule are not evaluated once one of the programs fails. For example,
SUBSYSTEM=="block", \ PROGRAM=="/usr/lib/udev/example-probe $devnode", \ PROGRAM=="/usr/lib/udev/example-describe $devnode", \ RESULT=="usb*", SYMLINK+="example-%k"
RESULT¶Match the returned string of the last PROGRAM call. This key can be used
in the same or in any later rule after a PROGRAM call. If several
PROGRAM keys were executed, the output of the most recently executed one is
matched; see "Token Processing Order" above.
The following keys can get values assigned. Assignments in a rule are only applied if all match keys of the rule succeed.
NAME¶The name to use for a network interface. This key is only used to rename network interfaces;
other devices — in particular device nodes — cannot be renamed by udev. The interface is renamed
when the device is added; on other events, the assignment only affects the $name
substitution and later NAME matches. Assigning "%k" or the
empty string is rejected. See
systemd.link(5)
for a higher-level mechanism for setting the interface name.
SYMLINK¶The name of a symlink targeting the device node, e.g.
"disk/by-id/usb-1234567890" for /dev/sda. With
"+=", the value is added to the list of symlinks to be created; with
"=" or ":=", the list is reset to this value; with
"-=", the value is removed from the list.
The value must be a normalized path, i.e. it must not contain redundant or
trailing slashes, or "." or ".." as a path component.
A relative path is interpreted relative to /dev/, and an absolute
path must be located below /dev/. Otherwise, the value is
ignored.
The set of characters to name a symlink is limited. Allowed characters are
"0-9A-Za-z#+-.:=@_/", valid UTF-8 character sequences, and "\x00"
hex encoding. All other characters are replaced by a "_" character.
Multiple symlinks may be specified by separating the names by the space character. In case
multiple devices claim the same name, the link always points to the device with the highest link
priority, set with the link_priority= option of OPTIONS (0 if
unset). If the current device goes away, the links are re-evaluated and the device with the next
highest link priority becomes the owner of the link. If several devices have the same priority,
which one of them owns the link is undefined.
Symlink names must never conflict with the kernel's default device node names, as that would result in unpredictable behavior.
OWNER, GROUP, MODE¶The permissions for the device node. Every specified value overrides the compiled-in default value. The user and group may be specified as a name or a numeric ID. Note that assigning a user or group that is not a system user or group is deprecated.
SECLABEL{module}¶Applies the specified Linux Security Module label to the device node.
ENV{key}¶Set a device property value. With "+=", the value is appended to the current
value of the property, separated by a space; assigning the empty string removes the property.
Properties that are set by the kernel or by udev itself (such as "ACTION",
"DEVPATH", "DEVNAME", "DEVLINKS" or
"INTERFACE") cannot be assigned or removed.
Property names with a leading "." are neither stored in the database nor
exported to events or external tools (run by, for example, the PROGRAM match
key).
TAG¶Attach a tag to a device. With "+=", the tag is added to the current list of
tags; with "-=", the tag is removed from the current list of tags only — it
remains in the stored "TAGS" property and in the tag index, so libudev monitor and
enumeration filters still match it. With "=", tags added by earlier rules during
the current event are dropped, and the value is assigned.
This is used to filter events for users of libudev's monitor functionality, or to enumerate a group of tagged devices. The implementation can only work efficiently if only a few tags are attached to a device. It is only meant to be used in contexts with specific device filter requirements, and not as a general-purpose flag. Excessive use might result in inefficient event handling.
ATTR{key}¶The value that should be written to a sysfs attribute of the event device. The attribute name
may address another device with the "[SUBSYSTEM/KERNEL]attribute" format (see
"Referring to Other Devices" above), and a "/*/" sequence in it
is expanded as described for TEST above. The assigned value supports string
substitutions; see "String Substitutions" below.
SYSCTL{kernel parameter}¶The value that should be written to kernel parameter. The parameter name and the assigned
value support string substitutions; see "String Substitutions" below.
RUN{type}¶Specify a program to be executed after processing of all the rules for the event. With
"+=", this invocation is added to the list, and with "=" or
":=", it replaces any previous contents of the list. Please note that both
"program" and "builtin" types described below share a common
list, so clearing the list with ":=" and "=" affects both
types.
type may be:
How the command line is specified, and which restrictions apply to the executed program, are
described in "Program Execution" below.
LABEL¶A named label to which a GOTO may jump.
GOTO¶Jumps to the next LABEL with a matching name. The label must appear later
in the same rules file; a GOTO without a matching label is ignored.
IMPORT{type}¶Import a set of variables as device properties, depending on
type:
program"¶Execute an external program specified as the assigned value and, if it returns
successfully, import its output, which must be in environment key format. How the command
line is specified, and which restrictions apply to the executed program, are described in
"Program Execution" below. If the value matches the name of a built-in
program, the built-in is executed instead.
builtin"¶Similar to "program", but use one of the built-in programs rather than
an external one.
file"¶Import a text file specified as the assigned value, the content of which must be in environment key format.
db"¶Import a single property specified as the assigned value from the current device database. This works only if the database is already populated by an earlier event.
cmdline"¶Import a single property from the kernel command line. For simple flags the value of
the property is set to "1".
parent"¶Import the stored keys from the parent device by reading the database entry of the
parent device. The value assigned to IMPORT{parent} is used as a filter of
key names to import (with the same shell glob pattern matching used for comparisons). Unlike
match values, this pattern does not support alternate patterns separated by
"|".
Note that multiple IMPORT{} keys may be specified in one rule. The key is
true if the import is successful, unless "!=" is used as the operator which causes
the key to be true if the import failed.
OPTIONS¶Rule and device options:
link_priority=value¶Specify the priority of the created symlinks. Devices with higher priorities overwrite existing symlinks of other devices. The default is 0.
string_escape=none|replace¶When "replace", possibly unsafe characters in strings assigned to
NAME, SYMLINK, and
ENV{ are replaced. When
"key}none", no replacement is performed. When unset, the replacement is
performed for NAME, SYMLINK, but not for
ENV{. Defaults to unset.key}
static_node=¶Apply the permissions specified in this rule to the static device node with the
specified name. Also, for every tag specified in this rule, create a symlink in the directory
/run/udev/static_node-tags/ pointing at
the static device node with the specified name. Static device node creation is performed by
systemd-tmpfiles before systemd-udevd is started. The static nodes might not have a
corresponding kernel device; they are used to trigger automatic kernel module loading when
they are accessed.tag
watch¶Watch the device node; when the node is closed after being opened for writing, a change uevent is synthesized.
nowatch¶Disable the watching of a device node.
db_persist¶Set the flag (sticky bit) on the udev database entry of the event device. Device properties are then kept in the database even when udevadm info --cleanup-db is called. This option can be useful in certain cases (e.g. Device Mapper devices) for persisting device state on the transition from initrd.
log_level=level¶Takes a log level name like "debug" or "info", or a
special value "reset". When a log level name is specified, the maximum log
level is changed to that level. When "reset" is set, then the previously
specified log level is revoked. Defaults to the log level of the main process of
systemd-udevd.
This may be useful when debugging events for certain devices. Note that the log level
is applied when the line including this rule is processed. So, for debugging, it is
recommended that this is specified at earlier place, e.g., the first line of
00-debug.rules. For an example, see the "Examples"
section below.
dump¶Dump the status of the event currently processing. It may be useful for debugging udev rules by inserting this option.
dump-json¶Similar to dump, but dump the status of the event in JSON
format.
The PROGRAM and IMPORT{program} match keys and the
RUN{program} assignment key execute external programs as part of the event processing.
(The IMPORT{builtin} and RUN{builtin} variants use built-in
programs instead; see IMPORT{} and RUN{} above.)
The program name and following arguments are separated by spaces. Single quotes can be used to
specify arguments with spaces. If no absolute path is given, the program is expected to live in
/usr/lib/udev; otherwise, the absolute path must be specified.
The device properties are made available to the executed program in the environment.
Program execution can only be used for very short-running foreground tasks. Running an event process for a long period of time may block all further events for this or a dependent device.
Note that running programs that access the network or mount/unmount filesystems is not allowed
inside of udev rules, due to the default sandbox that is enforced on
systemd-udevd.service.
Starting daemons or other long-running processes is not allowed; the forked processes, detached or
not, will be unconditionally killed after the event handling has finished. In order to activate
long-running processes from udev rules, provide a service unit and pull it in from a udev device using
the SYSTEMD_WANTS device property. See
systemd.device(5) for
details.
Values (and, for some keys, attribute names) may contain references to device properties and other
data, which are replaced when the rule is processed. Substitutions are applied to the values of the
ENV, GROUP, MODE, NAME,
OWNER, RUN, SECLABEL,
SYMLINK, TAG and ATTR/SYSCTL
assignment keys, to the values of the TEST, PROGRAM and
IMPORT{file}, IMPORT{program}, IMPORT{builtin}
and IMPORT{parent} match keys, and to the attribute names of the
ATTR and ATTRS match keys and the SYSCTL
key.
Substitutions are performed while the individual rule is being processed: values of
RUN are substituted when the rule is processed (and the result is executed later,
after all rules have been processed), so device properties set by earlier rules can be used, but
properties set by later rules cannot. Which properties of the same rule are visible depends on the token
processing order; see "Token Processing Order" above. The values of the other match keys
are not substituted: for example, KERNEL=="%k" matches the literal string
"%k".
The available substitutions are:
$kernel, %k¶The kernel name for this device.
$number, %n¶The kernel number for this device. For example, "sda3" has kernel number 3.
It is empty if the kernel name does not end in digits.
$devpath, %p¶The devpath of the device.
$id, %b¶The kernel name of the device matched while searching the devpath upwards for
SUBSYSTEMS, KERNELS, DRIVERS, and
ATTRS.
$driver, %d¶The driver name of the device matched while searching the devpath upwards for
SUBSYSTEMS, KERNELS, DRIVERS, and
ATTRS.
$attr{file},
%s{file}¶The value of a sysfs attribute found at the device where all keys of the rule have matched.
If the matching device does not have such an attribute, and a previous KERNELS,
SUBSYSTEMS, DRIVERS, or ATTRS test selected a
parent device, then the attribute from that parent device is used.
The attribute may also address another device with the
"[SUBSYSTEM/KERNEL]attribute" format; see "Referring to Other
Devices" above.
If the attribute is a symlink, the last element of the symlink target is returned as the value.
$env{key},
%E{key}¶A device property value.
$major, %M¶The kernel major number for the device.
$minor, %m¶The kernel minor number for the device.
$result, %c¶The string returned by the external program requested with PROGRAM. A
single part of the string, separated by a space character, may be selected by specifying the part
number as an attribute: "%c{N}". If the number is followed by the
"+" character, this part plus all remaining parts of the result string are
substituted: "%c{N+}".
$parent, %P¶The node name of the parent device.
$name, %D¶The current name of the device. If not changed by a rule, it is the name of the kernel device.
$links, %L¶A space-separated list of the current symlinks. The value is only set during a remove event or if an earlier rule assigned a value.
$root, %r¶The udev_root value.
$sys, %S¶The sysfs mount point.
$devnode, %N¶The name of the device node.
$tempnode¶A deprecated alias for $devnode.
$sysfs{file}¶A deprecated alias for $attr{.file}
%%¶The "%" character itself.
$$¶The "$" character itself.
The ACTION match key tests the action that triggered the uevent. Uevents
originate from the kernel; the following actions are processed by udev:
Table 3. Device actions
| Action | Description |
|---|---|
"add" | A device has been created or initialized. |
"remove" | A device is being removed; see below. |
"change" | A device has changed its state or properties; the device node is kept, but rules are re-run and symlinks and permissions are re-created. udev itself synthesizes "change" events in some cases, e.g. for the watch option. |
"move" | A device has been moved or renamed; the previous devpath is available in the "DEVPATH_OLD" property. |
"online" | A device has been set online. |
"offline" | A device has been set offline. |
"bind" | A device driver has been bound to the device. |
"unbind" | A device driver has been unbound from the device. |
Which events are emitted, and when, is subsystem-specific; see the kernel documentation of the
kobject infrastructure for background.
When a device is removed, the kernel emits a "remove" event for each device of the
affected hierarchy (e.g. for each partition before the containing disk), after the device node itself
(such as /dev/sda1) has already been removed. Symlinks created by udev do not cause
separate events; they are cleaned up by udev when the corresponding "remove" event is
processed.
When a "remove" event is processed, the device properties stored by earlier events
are first read into memory and remain available for matching, and the $links
substitution expands to the symlinks that were created for the device. Before the rules are processed,
systemd-udevd already removes the tag index entries and the database entry of the device; the symlinks it
created are removed after the rules have been processed. Assignments that manage the device node
(NAME, SYMLINK, OWNER, GROUP,
MODE) have no effect.
To write a rule that matches a specific device, first find out which values are available to match against. The udevadm(8) tool provides several modes for this:
udevadm info --query=all device shows the
properties, device node name and symlinks of a device, i.e. the values tested by the
ENV, KERNEL, DEVPATH and
SYMLINK keys.
udevadm info --attribute-walk device shows the
device chain — for the event device and each of its parents, the values tested by the
KERNELS, SUBSYSTEMS, DRIVERS and
ATTRS keys are listed in order. This is the primary tool for writing parent device
matches: a rule that combines several parent match keys must match them all at one of the devices
shown.
udevadm monitor shows uevents as they arrive, including the action and devpath, while devices are (dis)connected or change state.
Rules can be tested before they are installed:
udevadm verify checks rules files for syntax errors and common mistakes (such as duplicate or conflicting expressions).
udevadm test --action=action
devpath processes all rules against a device without applying
changes — device nodes, symlinks and the database are not modified, and programs specified with
RUN are not executed — showing which rules match and what would be assigned. Of
the programs invoked by the PROGRAM and IMPORT{program} keys,
only a few well-known helpers (ata_id, cdrom_id,
dmi_memory_id, fido_id, mtd_probe and
scsi_id) are actually executed; other commands are skipped and reported as
successful with empty output.
For events processed by the running daemon, the log_level= and
dump options (see OPTIONS above) can be used to inspect the
processing of specific devices. Detailed per-token tracing — log messages for every token that passes,
fails, or is applied — is enabled with udevadm control --trace=yes, which also raises
the log level to debug (as does the udev.trace=1 kernel command line option), or shown
by udevadm test --verbose.
For devices identified by their hardware properties, the hardware database described in
systemd-hwdb(8) provides
an alternative to udev rules: it maps modalias-style match keys to device properties, which can be
imported into the event with IMPORT{builtin}="hwdb" and then matched with the
ENV key.
Example 1. Set the group and mode of USB serial devices
SUBSYSTEM=="tty", KERNEL=="ttyUSB[0-9]*", GROUP="dialout", MODE="0660"
Example 2. Rename a network interface based on its MAC address
SUBSYSTEM=="net", ACTION=="add", ATTR{address}=="52:54:00:12:34:56", NAME="lan0"Example 3. Create a symlink for a USB storage device
The SUBSYSTEMS and ATTRS keys both match at the USB device
in the parent chain of the block device:
SUBSYSTEM=="block", ENV{DEVTYPE}=="disk", SUBSYSTEMS=="usb", ATTRS{serial}=="1234567890", SYMLINK+="usbdisk"Example 4. Import properties and use them in later rules
For a partition, the properties of the containing disk (stored in its database entry during an
earlier event) are imported from the parent device. As IMPORT runs after the plain
match keys, the imported properties are matched in a second rule, where they are also available to the
SYMLINK assignment:
SUBSYSTEM=="block", ENV{DEVTYPE}=="partition", IMPORT{parent}=="ID_*"
SUBSYSTEM=="block", ENV{DEVTYPE}=="partition", ENV{ID_SERIAL}=="?*", \
SYMLINK+="disk-by-serial-$env{ID_SERIAL}-part$number"Example 5. Run a program and match on its output
SUBSYSTEM=="block", KERNEL=="sr[0-9]*", \ PROGRAM=="/usr/lib/udev/example-media-type $devnode", \ RESULT=="cd*|dvd*", SYMLINK+="media-$kernel"
Example 6. Write to a sysfs attribute if it exists
This mirrors the pattern used by the autosuspend rules: the TEST key checks
that the attribute exists before it is written:
ENV{ID_AUTOSUSPEND}=="1", TEST=="power/control", ATTR{power/control}="auto"Example 7. Skip parts of a rules file
# Network interface rules follow; skip everything else.
SUBSYSTEM!="net", GOTO="net_end"
ATTR{address}=="52:54:00:12:34:56", NAME="lan0"
LABEL="net_end"Example 8. Raise the log level for specific devices
Raising the log level early makes the worker log which rules are applied:
# /etc/udev/rules.d/00-debug.rules SUBSYSTEM=="block", OPTIONS="log_level=debug"
Example 9. Dump the state of a device
Dumping the event state in a late rules file shows the result of the processing:
# /etc/udev/rules.d/99-dump.rules SUBSYSTEM=="block", OPTIONS="dump"