{"id":18645,"date":"2026-05-11T07:31:30","date_gmt":"2026-05-11T06:31:30","guid":{"rendered":"https:\/\/roglacup.com\/klaus62\/?p=18645"},"modified":"2026-05-11T07:31:33","modified_gmt":"2026-05-11T06:31:33","slug":"linux-udev-rulesdoug-brown","status":"publish","type":"post","link":"https:\/\/roglacup.com\/klaus62\/2026\/05\/11\/linux-udev-rulesdoug-brown\/","title":{"rendered":"Linux udev rules|Doug Brown"},"content":{"rendered":"\n<p>&nbsp;have to admit: until recently,&nbsp;<a href=\"http:\/\/en.wikipedia.org\/wiki\/Udev\">udev<\/a>&nbsp;completely intimidated me. I needed udev rules for changing device permissions or creating symlinks with special names, but usually I had to use Google to find somebody else\u2019s rule. Sometimes a rule I found wouldn\u2019t work because the udev syntax changed\u2013for example, you need to use ATTR or ATTRS instead of SYSFS with recent versions. It still scares me a little bit, but at least I feel like I actually understand what\u2019s going on with it now. I\u2019d like to share a few tips and tricks I\u2019ve picked up about making udev rules.<\/p>\n\n\n\n<p>First of all,&nbsp;<a href=\"http:\/\/linux.die.net\/man\/7\/udev\">the man page for udev<\/a>&nbsp;is extremely useful. I know man pages can be boring, but this one has just about everything you need to know, and it\u2019s not too long.<\/p>\n\n\n\n<h6 class=\"wp-block-heading\">Where udev rules are stored<\/h6>\n\n\n\n<p>There are two directories where you can find udev rules:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\/etc\/udev\/rules.d<\/li>\n\n\n\n<li>\/lib\/udev\/rules.d<\/li>\n<\/ul>\n\n\n\n<p>The rules in \/lib\/udev\/rules.d are bundled rules that you probably shouldn\u2019t be messing with. Instead, put your custom rules in \/etc\/udev\/rules.d. You\u2019ll notice they are named in the form \u201cnumber-description.rules\u201d. The number is used for ordering the rules, so you can pick an order that makes sense for your needs. It\u2019s a good idea to use that same form so that you have a good idea of the order in which the rules will be parsed. This can definitely matter\u2013I have had troubles with creating symlinks for USB serial devices if the number is too small, probably due to some bundled rule that overrides something my rule does.<\/p>\n\n\n\n<p>If you want to override a bundled rule file that\u2019s in \/lib\/udev\/rules.d, you should create a file with the same name in \/etc\/udev\/rules.d. This will cause the new file in \/etc\/udev\/rules.d to be used instead of the file in \/lib\/udev\/rules.d.<\/p>\n\n\n\n<h6 class=\"wp-block-heading\">The format of udev rules<\/h6>\n\n\n\n<p>udev rules are basically a comma-separated list of things\u2013conditions and assignments. If all of the conditions in a rule are true, the rule is a match and all of the assignments are performed. Similar to many programming languages, a single equal sign (=) represents an assignment, while two equal signs (==) represent a comparison. != represents a \u201cnot equals\u201d comparison, += adds a value to a list, and := assigns a value for the final time, disallowing any higher-numbered rules from modifying whatever you assigned. Every condition or assignment is a key-value pair separated by one of the operators listed above.<\/p>\n\n\n\n<h6 class=\"wp-block-heading\">Keys you can match against<\/h6>\n\n\n\n<p>This section, I believe, is the most important part to understand. If you don\u2019t understand the intricacies of how these matches work, especially with the keys ending in S, you will probably make mistaken assumptions about how they work (I did!). You can match against these keys:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>ACTION<\/strong>\u00a0\u2014 what happened to the device that caused udev to be invoked. Common actions checked against are \u201cadd\u201d and \u201cremove\u201d.<\/li>\n\n\n\n<li><strong>KERNEL<\/strong>\u00a0\u2014 the name of the device as given by the kernel<\/li>\n\n\n\n<li><strong>KERNELS<\/strong>\u00a0\u2014 the name of the device or a parent device as given by the kernel<\/li>\n\n\n\n<li><strong>SUBSYSTEM<\/strong>\u00a0\u2014 the kernel subsystem of the device (example: tty or usb)<\/li>\n\n\n\n<li><strong>SUBSYSTEMS<\/strong>\u00a0\u2014 the subsystem of the device or a parent device<\/li>\n\n\n\n<li><strong>DRIVER<\/strong>\u00a0\u2014 the name of the device\u2019s driver<\/li>\n\n\n\n<li><strong>DRIVERS<\/strong>\u00a0\u2014 the name of the device\u2019s driver or the name of a parent device\u2019s driver<\/li>\n\n\n\n<li><strong>ATTR{<\/strong>name<strong>}<\/strong>\u00a0\u2014 a sysfs attribute of the device<\/li>\n\n\n\n<li><strong>ATTRS{<\/strong>name<strong>}<\/strong>\u00a0\u2014 a sysfs attribute of the device or a parent device<\/li>\n\n\n\n<li><em><strong>IMPORTANT NOTE:<\/strong><\/em>\u00a0If you are using any of the keys above that match against a parent device (KERNELS, SUBSYSTEMS, DRIVERS, ATTRS) in a rule, the parent device you\u2019re matching against must be the same. For example, you can\u2019t search for ATTRS of two different parent devices in the same rule. You also can\u2019t search for the ATTRS of one parent device and the SUBSYSTEMS of a different parent device in the same rule. You can work around this limitation by creating multiple rules and using GOTO, but you can\u2019t do it all in the same rule. This is actually a good thing because it allows you to do things like make sure you\u2019re looking at a USB serial number instead of a PCI serial number for example.<\/li>\n<\/ul>\n\n\n\n<p>This is very much an incomplete list, but these are some of the more important ones.<\/p>\n\n\n\n<h6 class=\"wp-block-heading\">How to find values to match against<\/h6>\n\n\n\n<p>The info above is great, but it doesn\u2019t really help much unless you know which driver, subsystem, and event attribute you\u2019re trying to match against. That\u2019s where a really handy command comes into play:<\/p>\n\n\n\n<p>udevadm info &#8211;attribute-walk &#8211;name=ttyUSB0<\/p>\n\n\n\n<p>ttyUSB0 is an example device name in this case \u2014 a USB serial port. This attribute walk command will find all of the keys\/attributes for a particular device and all of its parent devices. If you run this command on a USB serial port, you will notice it finds the tty device at the top of the output, which is actually fairly boring as far as matching goes. Then as you scroll down, you will see udevadm walk all the way up the tree of parent devices.. You will see it reach the actual USB device that provides the serial port, the USB hub it\u2019s connected to, the USB host controller the hub is connected to, and then probably a PCI controller or something like that depending on your host system.<\/p>\n\n\n\n<h6 class=\"wp-block-heading\">More complicated example<\/h6>\n\n\n\n<p>Here\u2019s a rule I created for giving certain USB-serial adapters a different name. If a USB-serial adapter with the serial number \u201c12345678\u201d is plugged in, I want a symlink of the form \u201cttyBlah#\u201d to be created to point to it, where # is an integer. So if I plug three of them in, I want them to be called ttyBlah0, ttyBlah1, and ttyBlah2. This seems straightforward, but there might be other USB-serial adapters plugged in so I can\u2019t just copy the kernel\u2019s device number. For example, let\u2019s pretend five USB-serial adapters are plugged in, named ttyUSB0 through ttyUSB4. ttyUSB0, ttyUSB1, and ttyUSB4 have the serial number \u201c12345678\u201d and ttyUSB2 and ttyUSB3 are other things that don\u2019t use that serial number. I want my symlinks to be called ttyBlah0, ttyBlah1, and ttyBlah2. Notice that I can\u2019t just blindly copy the number over because I want the third one to be called ttyBlah2, not ttyBlah4. It turns out udev can do this with a little bit of help.<\/p>\n\n\n\n<p>I know this idea of having multiple USB devices with the same serial number seems goofy, but I actually did this in the real world. I programmed several FTDI chips to have the same serial number. It\u2019s more convenient than getting a new USB device ID and not knowing if the Linux kernel is going to support it out of the box. The main reason I did it, though, was not Linux-related: it helps prevent Windows from creating a new COM port number each time a device with a different serial number is plugged in. In certain scenarios this behavior is undesirable.<\/p>\n\n\n\n<p>Let\u2019s get to the fun part. Here\u2019s my example rule:<\/p>\n\n\n\n<p>ACTION==&#8221;add&#8221;, KERNEL=&#8221;ttyUSB[0-9]*&#8221;, SUBSYSTEMS==&#8221;usb&#8221;, ATTRS{serial}==&#8221;12345678&#8243;, PROGRAM=&#8221;\/home\/doug\/symlink-number.sh ttyBlah&#8221;, SYMLINK+=&#8221;ttyBlah%c&#8221;<\/p>\n\n\n\n<p>This rule makes use of several new ideas. First of all, I have to match a device with a kernel name of ttyUSB followed by zero or more digits. This should match all USB serial ports. A parent device needs to be in the USB subsystem. This same parent device also needs to have a serial number of 12345678. The reason we\u2019re checking for a subsystem of USB is to ensure that we don\u2019t match against a device that has a PCI serial number (for example) of 12345678 instead. It\u2019s probably overkill since ttyUSB devices are only going to be USB devices anyway, but it\u2019s a good example of how the \u201cending in S\u201d keys work. Anyway, we\u2019re making sure we match against a ttyUSB* device which has a USB serial number of 12345678. Those three rules combined should match my three adapters that have that serial number.<\/p>\n\n\n\n<p>If they match, we have two assignments. The first assignment is to PROGRAM. If you assign a value to PROGRAM, the value is interpreted as a command to run. The command is executed and the program\u2019s output to stdout can be used in various ways in udev. This script (which we will see later) will print out the next available number to use for ttyBlah. Note that I passed a single argument to my script: ttyBlah. As you\u2019ll see, I made this script generic so it could be used for numbering various different symlinks.<\/p>\n\n\n\n<p>The second assignment uses the += operator, which as I said earlier adds a value to a list. The SYMLINK variable is a list of names of symlinks to create that will point to the matched device. We use += in case another rule has already set up a symlink for this same port.<\/p>\n\n\n\n<p>You\u2019ll notice that the symlink will be named ttyBlah%c. Several of the assigned values (one of which is SYMLINK) can be given names with printf-style formatters to add extra information. %c will be replaced with the stdout output from the program that was executed from the PROGRAM assignment. Another example of the available printf-style substitutions is %b which is the name of the parent device that was matched with the \u201cending in S\u201d keys.<\/p>\n\n\n\n<p>More &amp; Posted from: <a href=\"https:\/\/www.netspi.com\/blog\/technical-blog\/network-pentesting\/linux-hacking-case-studies-part-2-nfs\/\">https:\/\/www.netspi.com\/blog\/technical-blog\/network-pentesting\/linux-hacking-case-studies-part-2-nfs\/<\/a><\/p>\n\n\n\n<p>Additional link : <a href=\"https:\/\/clearpathrobotics.com\/blog\/2015\/01\/udev\/\">Do More with Udev<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp;have to admit: until recently,&nbsp;udev&nbsp;completely intimidated me. I needed udev rules for changing device permissions or creating symlinks with special names, but usually I had to use Google to find somebody else\u2019s rule. Sometimes a rule I found wouldn\u2019t work because the udev syntax changed\u2013for example, you need to use ATTR or ATTRS instead of&hellip;&nbsp;<a href=\"https:\/\/roglacup.com\/klaus62\/2026\/05\/11\/linux-udev-rulesdoug-brown\/\" class=\"\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Linux udev rules|Doug Brown<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":18646,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"[\"content\",\"tags\",\"comments\"]","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[17,16],"tags":[],"class_list":["post-18645","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-virtualization"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/roglacup.com\/klaus62\/wp-json\/wp\/v2\/posts\/18645","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/roglacup.com\/klaus62\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/roglacup.com\/klaus62\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/roglacup.com\/klaus62\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/roglacup.com\/klaus62\/wp-json\/wp\/v2\/comments?post=18645"}],"version-history":[{"count":1,"href":"https:\/\/roglacup.com\/klaus62\/wp-json\/wp\/v2\/posts\/18645\/revisions"}],"predecessor-version":[{"id":18647,"href":"https:\/\/roglacup.com\/klaus62\/wp-json\/wp\/v2\/posts\/18645\/revisions\/18647"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/roglacup.com\/klaus62\/wp-json\/wp\/v2\/media\/18646"}],"wp:attachment":[{"href":"https:\/\/roglacup.com\/klaus62\/wp-json\/wp\/v2\/media?parent=18645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/roglacup.com\/klaus62\/wp-json\/wp\/v2\/categories?post=18645"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/roglacup.com\/klaus62\/wp-json\/wp\/v2\/tags?post=18645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}