1UDEV(7) udev UDEV(7)
2
3
4
6 udev - Dynamic device management
7
9 udev supplies the system software with device events, manages
10 permissions of device nodes and may create additional symlinks in the
11 /dev/ directory, or renames network interfaces. The kernel usually just
12 assigns unpredictable device names based on the order of discovery.
13 Meaningful symlinks or network device names provide a way to reliably
14 identify devices based on their properties or current configuration.
15
16 The udev daemon, systemd-udevd.service(8), receives device uevents
17 directly from the kernel whenever a device is added or removed from the
18 system, or it changes its state. When udev receives a device event, it
19 matches its configured set of rules against various device attributes
20 to identify the device. Rules that match may provide additional device
21 information to be stored in the udev database or to be used to create
22 meaningful symlink names.
23
24 All device information udev processes is stored in the udev database
25 and sent out to possible event subscribers. Access to all stored data
26 and the event sources is provided by the library libudev.
27
29 The udev rules are read from the files located in the system rules
30 directories /usr/lib/udev/rules.d and /usr/local/lib/udev/rules.d, the
31 volatile runtime directory /run/udev/rules.d and the local
32 administration directory /etc/udev/rules.d. All rules files are
33 collectively sorted and processed in lexical order, regardless of the
34 directories in which they live. However, files with identical filenames
35 replace each other. Files in /etc/ have the highest priority, files in
36 /run/ take precedence over files with the same name under /usr/. This
37 can be used to override a system-supplied rules file with a local file
38 if needed; a symlink in /etc/ with the same name as a rules file in
39 /usr/lib/, pointing to /dev/null, disables the rules file entirely.
40 Rule files must have the extension .rules; other extensions are
41 ignored.
42
43 Every line in the rules file contains at least one key-value pair.
44 Except for empty lines or lines beginning with "#", which are ignored.
45 There are two kinds of keys: match and assignment. If all match keys
46 match against their values, the rule gets applied and the assignment
47 keys get the specified values assigned.
48
49 A matching rule may rename a network interface, add symlinks pointing
50 to the device node, or run a specified program as part of the event
51 handling.
52
53 A rule consists of a comma-separated list of one or more
54 key-operator-value expressions. Each expression has a distinct effect,
55 depending on the key and operator used.
56
57 Operators
58 "=="
59 Compare for equality. (The specified key has the specified value.)
60
61 "!="
62 Compare for inequality. (The specified key doesn't have the
63 specified value, or the specified key is not present at all.)
64
65 "="
66 Assign a value to a key. Keys that represent a list are reset and
67 only this single value is assigned.
68
69 "+="
70 Add the value to a key that holds a list of entries.
71
72 "-="
73 Remove the value from a key that holds a list of entries.
74
75 ":="
76 Assign a value to a key finally; disallow any later changes.
77
78 Values
79 Values are written as double quoted strings, such as ("string"). To
80 include a quotation mark (") in the value, precede it by a backslash
81 (\"). Any other occurrences of a backslash followed by a character are
82 not unescaped. That is, "\t\n" is treated as four characters:
83 backslash, lowercase t, backslash, lowercase n.
84
85 The string can be prefixed with a lowercase e (e"string\n") to mark the
86 string as C-style escaped[1]. For example, e"string\n" is parsed as 7
87 characters: 6 lowercase letters and a newline. This can be useful for
88 writing special characters when a kernel driver requires them.
89
90 Please note that NUL is not allowed in either string variant.
91
92 Keys
93 The following key names can be used to match against device properties.
94 Some of the keys also match against properties of the parent devices in
95 sysfs, not only the device that has generated the event. If multiple
96 keys that match a parent device are specified in a single rule, all
97 these keys must match at one and the same parent device.
98
99 ACTION
100 Match the name of the event action.
101
102 DEVPATH
103 Match the devpath of the event device.
104
105 KERNEL
106 Match the name of the event device.
107
108 KERNELS
109 Search the devpath upwards for a matching device name.
110
111 NAME
112 Match the name of a network interface. It can be used once the NAME
113 key has been set in one of the preceding rules.
114
115 SYMLINK
116 Match the name of a symlink targeting the node. It can be used once
117 a SYMLINK key has been set in one of the preceding rules. There may
118 be multiple symlinks; only one needs to match.
119
120 SUBSYSTEM
121 Match the subsystem of the event device.
122
123 SUBSYSTEMS
124 Search the devpath upwards for a matching device subsystem name.
125
126 DRIVER
127 Match the driver name of the event device. Only set this key for
128 devices which are bound to a driver at the time the event is
129 generated.
130
131 DRIVERS
132 Search the devpath upwards for a matching device driver name.
133
134 ATTR{filename}
135 Match sysfs attribute value of the event device.
136
137 Trailing whitespace in the attribute values is ignored unless the
138 specified match value itself contains trailing whitespace.
139
140 ATTRS{filename}
141 Search the devpath upwards for a device with matching sysfs
142 attribute values. If multiple ATTRS matches are specified, all of
143 them must match on the same device.
144
145 Trailing whitespace in the attribute values is ignored unless the
146 specified match value itself contains trailing whitespace.
147
148 SYSCTL{kernel parameter}
149 Match a kernel parameter value.
150
151 ENV{key}
152 Match against a device property value.
153
154 CONST{key}
155 Match against a system-wide constant. Supported keys are:
156
157 "arch"
158 System's architecture. See ConditionArchitecture= in
159 systemd.unit(5) for possible values.
160
161 "virt"
162 System's virtualization environment. See systemd-detect-virt(1)
163 for possible values.
164
165 Unknown keys will never match.
166
167 TAG
168 Match against a device tag.
169
170 TAGS
171 Search the devpath upwards for a device with matching tag.
172
173 TEST{octal mode mask}
174 Test the existence of a file. An octal mode mask can be specified
175 if needed.
176
177 PROGRAM
178 Execute a program to determine whether there is a match; the key is
179 true if the program returns successfully. The device properties are
180 made available to the executed program in the environment. The
181 program's standard output is available in the RESULT key.
182
183 This can only be used for very short-running foreground tasks. For
184 details, see RUN.
185
186 Note that multiple PROGRAM keys may be specified in one rule, and
187 "=", ":=", and "+=" have the same effect as "==".
188
189 RESULT
190 Match the returned string of the last PROGRAM call. This key can be
191 used in the same or in any later rule after a PROGRAM call.
192
193 Most of the fields support shell glob pattern matching and alternate
194 patterns. The following special characters are supported:
195
196 "*"
197 Matches zero or more characters.
198
199 "?"
200 Matches any single character.
201
202 "[]"
203 Matches any single character specified within the brackets. For
204 example, the pattern string "tty[SR]" would match either "ttyS" or
205 "ttyR". Ranges are also supported via the "-" character. For
206 example, to match on the range of all digits, the pattern "[0-9]"
207 could be used. If the first character following the "[" is a "!",
208 any characters not enclosed are matched.
209
210 "|"
211 Separates alternative patterns. For example, the pattern string
212 "abc|x*" would match either "abc" or "x*".
213
214 The following keys can get values assigned:
215
216 NAME
217 The name to use for a network interface. See systemd.link(5) for a
218 higher-level mechanism for setting the interface name. The name of
219 a device node cannot be changed by udev, only additional symlinks
220 can be created.
221
222 SYMLINK
223 The name of a symlink targeting the node. Every matching rule adds
224 this value to the list of symlinks to be created.
225
226 The set of characters to name a symlink is limited. Allowed
227 characters are "0-9A-Za-z#+-.:=@_/", valid UTF-8 character
228 sequences, and "\x00" hex encoding. All other characters are
229 replaced by a "_" character.
230
231 Multiple symlinks may be specified by separating the names by the
232 space character. In case multiple devices claim the same name, the
233 link always points to the device with the highest link_priority. If
234 the current device goes away, the links are re-evaluated and the
235 device with the next highest link_priority becomes the owner of the
236 link. If no link_priority is specified, the order of the devices
237 (and which one of them owns the link) is undefined.
238
239 Symlink names must never conflict with the kernel's default device
240 node names, as that would result in unpredictable behavior.
241
242 OWNER, GROUP, MODE
243 The permissions for the device node. Every specified value
244 overrides the compiled-in default value.
245
246 SECLABEL{module}
247 Applies the specified Linux Security Module label to the device
248 node.
249
250 ATTR{key}
251 The value that should be written to a sysfs attribute of the event
252 device.
253
254 SYSCTL{kernel parameter}
255 The value that should be written to kernel parameter.
256
257 ENV{key}
258 Set a device property value. Property names with a leading "." are
259 neither stored in the database nor exported to events or external
260 tools (run by, for example, the PROGRAM match key).
261
262 TAG
263 Attach a tag to a device. This is used to filter events for users
264 of libudev's monitor functionality, or to enumerate a group of
265 tagged devices. The implementation can only work efficiently if
266 only a few tags are attached to a device. It is only meant to be
267 used in contexts with specific device filter requirements, and not
268 as a general-purpose flag. Excessive use might result in
269 inefficient event handling.
270
271 RUN{type}
272 Specify a program to be executed after processing of all the rules
273 for the event. With "+=", this invocation is added to the list, and
274 with "=" or ":=", it replaces any previous contents of the list.
275 Please note that both "program" and "builtin" types described below
276 share a common list, so clearing the list with ":=" and "=" affects
277 both types.
278
279 type may be:
280
281 "program"
282 Execute an external program specified as the assigned value. If
283 no absolute path is given, the program is expected to live in
284 /usr/lib/udev; otherwise, the absolute path must be specified.
285
286 This is the default if no type is specified.
287
288 "builtin"
289 As program, but use one of the built-in programs rather than an
290 external one.
291
292 The program name and following arguments are separated by spaces.
293 Single quotes can be used to specify arguments with spaces.
294
295 This can only be used for very short-running foreground tasks.
296 Running an event process for a long period of time may block all
297 further events for this or a dependent device.
298
299 Note that running programs that access the network or mount/unmount
300 filesystems is not allowed inside of udev rules, due to the default
301 sandbox that is enforced on systemd-udevd.service.
302
303 Starting daemons or other long-running processes is not allowed;
304 the forked processes, detached or not, will be unconditionally
305 killed after the event handling has finished. In order to activate
306 long-running processes from udev rules, provide a service unit and
307 pull it in from a udev device using the SYSTEMD_WANTS device
308 property. See systemd.device(5) for details.
309
310 LABEL
311 A named label to which a GOTO may jump.
312
313 GOTO
314 Jumps to the next LABEL with a matching name.
315
316 IMPORT{type}
317 Import a set of variables as device properties, depending on type:
318
319 "program"
320 Execute an external program specified as the assigned value
321 and, if it returns successfully, import its output, which must
322 be in environment key format. Path specification,
323 command/argument separation, and quoting work like in RUN.
324
325 "builtin"
326 Similar to "program", but use one of the built-in programs
327 rather than an external one.
328
329 "file"
330 Import a text file specified as the assigned value, the content
331 of which must be in environment key format.
332
333 "db"
334 Import a single property specified as the assigned value from
335 the current device database. This works only if the database is
336 already populated by an earlier event.
337
338 "cmdline"
339 Import a single property from the kernel command line. For
340 simple flags the value of the property is set to "1".
341
342 "parent"
343 Import the stored keys from the parent device by reading the
344 database entry of the parent device. The value assigned to
345 IMPORT{parent} is used as a filter of key names to import (with
346 the same shell glob pattern matching used for comparisons).
347
348 This can only be used for very short-running foreground tasks. For
349 details see RUN.
350
351 Note that multiple IMPORT{} keys may be specified in one rule, and
352 "=", ":=", and "+=" have the same effect as "==". The key is true
353 if the import is successful, unless "!=" is used as the operator
354 which causes the key to be true if the import failed.
355
356 OPTIONS
357 Rule and device options:
358
359 link_priority=value
360 Specify the priority of the created symlinks. Devices with
361 higher priorities overwrite existing symlinks of other devices.
362 The default is 0.
363
364 string_escape=none|replace
365 When "replace", possibly unsafe characters in strings assigned
366 to NAME, SYMLINK, and ENV{key} are replaced. When "none", no
367 replacement is performed. When unset, the replacement is
368 performed for NAME, SYMLINK, but not for ENV{key}. Defaults to
369 unset.
370
371 static_node=
372 Apply the permissions specified in this rule to the static
373 device node with the specified name. Also, for every tag
374 specified in this rule, create a symlink in the directory
375 /run/udev/static_node-tags/tag pointing at the static device
376 node with the specified name. Static device node creation is
377 performed by systemd-tmpfiles before systemd-udevd is started.
378 The static nodes might not have a corresponding kernel device;
379 they are used to trigger automatic kernel module loading when
380 they are accessed.
381
382 watch
383 Watch the device node with inotify; when the node is closed
384 after being opened for writing, a change uevent is synthesized.
385
386 nowatch
387 Disable the watching of a device node with inotify.
388
389 db_persist
390 Set the flag (sticky bit) on the udev database entry of the
391 event device. Device properties are then kept in the database
392 even when udevadm info --cleanup-db is called. This option can
393 be useful in certain cases (e.g. Device Mapper devices) for
394 persisting device state on the transition from initramfs.
395
396 log_level=level
397 Takes a log level name like "debug" or "info", or a special
398 value "reset". When a log level name is specified, the maximum
399 log level is changed to that level. When "reset" is set, then
400 the previously specified log level is revoked. Defaults to the
401 log level of the main process of systemd-udevd.
402
403 This may be useful when debugging events for certain devices.
404 Note that the log level is applied when the line including this
405 rule is processed. So, for debugging, it is recommended that
406 this is specified at earlier place, e.g., the first line of
407 00-debug.rules.
408
409 Example for debugging uevent processing for network interfaces.
410
411 # /etc/udev/rules.d/00-debug-net.rules
412 SUBSYSTEM=="net", OPTIONS="log_level=debug"
413
414 The NAME, SYMLINK, PROGRAM, OWNER, GROUP, MODE, SECLABEL, and RUN
415 fields support simple string substitutions. The RUN substitutions are
416 performed after all rules have been processed, right before the program
417 is executed, allowing for the use of device properties set by earlier
418 matching rules. For all other fields, substitutions are performed while
419 the individual rule is being processed. The available substitutions
420 are:
421
422 $kernel, %k
423 The kernel name for this device.
424
425 $number, %n
426 The kernel number for this device. For example, "sda3" has kernel
427 number 3.
428
429 $devpath, %p
430 The devpath of the device.
431
432 $id, %b
433 The name of the device matched while searching the devpath upwards
434 for SUBSYSTEMS, KERNELS, DRIVERS, and ATTRS.
435
436 $driver
437 The driver name of the device matched while searching the devpath
438 upwards for SUBSYSTEMS, KERNELS, DRIVERS, and ATTRS.
439
440 $attr{file}, %s{file}
441 The value of a sysfs attribute found at the device where all keys
442 of the rule have matched. If the matching device does not have such
443 an attribute, and a previous KERNELS, SUBSYSTEMS, DRIVERS, or ATTRS
444 test selected a parent device, then the attribute from that parent
445 device is used.
446
447 If the attribute is a symlink, the last element of the symlink
448 target is returned as the value.
449
450 $env{key}, %E{key}
451 A device property value.
452
453 $major, %M
454 The kernel major number for the device.
455
456 $minor, %m
457 The kernel minor number for the device.
458
459 $result, %c
460 The string returned by the external program requested with PROGRAM.
461 A single part of the string, separated by a space character, may be
462 selected by specifying the part number as an attribute: "%c{N}". If
463 the number is followed by the "+" character, this part plus all
464 remaining parts of the result string are substituted: "%c{N+}".
465
466 $parent, %P
467 The node name of the parent device.
468
469 $name
470 The current name of the device. If not changed by a rule, it is the
471 name of the kernel device.
472
473 $links
474 A space-separated list of the current symlinks. The value is only
475 set during a remove event or if an earlier rule assigned a value.
476
477 $root, %r
478 The udev_root value.
479
480 $sys, %S
481 The sysfs mount point.
482
483 $devnode, %N
484 The name of the device node.
485
486 %%
487 The "%" character itself.
488
489 $$
490 The "$" character itself.
491
493 systemd-udevd.service(8), udevadm(8), systemd.link(5)
494
496 1. C-style escaped
497 https://en.wikipedia.org/wiki/Escape_sequences_in_C#Table_of_escape_sequences
498
499
500
501systemd 250 UDEV(7)