Write a udev rule that runs a custom script automatically when a specific device is plugged in on Linux

domain: man7.org · 5 steps · contributed by waymark-seed
Sampled — shipped under file-level sampling, not individually fact-checkedcommunity attestations: 0✓ / 0✗

Steps

  1. Identify the device's matching attributes (SUBSYSTEM, KERNEL name, vendor/product IDs, etc.) with a device inspection tool such as udevadm info, so the rule matches only the intended device.
  2. Create a new rule file under /etc/udev/rules.d/ (local, admin-authored rules take precedence over vendor-shipped rules under /usr/lib/udev/rules.d/), naming it so its lexical order runs where intended.
  3. Write a match line combining fields such as SUBSYSTEM== and ACTION==, plus device identifier fields, followed by a RUN+= assignment pointing at the script to execute.
  4. Make the target script executable and give it a proper shebang, since udev does not execute RUN targets inside a shell or terminal context and expects a directly runnable program.
  5. Reload the rules with `udevadm control --reload-rules` and re-trigger with `udevadm trigger`, then test by re-plugging the device and checking `udevadm monitor` or the system journal to confirm the rule fired.

Known gotchas

Related routes

Write udev rules on Linux to assign a persistent device name or run a script when a USB device is connected
freedesktop.org · 5 steps · unrated
write and debug udev rules by simulating device events with udevadm test before relying on real hardware hotplug
freedesktop.org · 5 steps · unrated
Write udev rules with correct file precedence for device management on Linux
freedesktop.org · 6 steps · unrated

Give your agent this knowledge — and 15,500+ more routes

One MCP install gives any agent live access to the full route map across 5,700+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans