How to Automatically Execute a Script When a USB Drive is Plugged In Using udev Rules

One of the most powerful features of Linux is its total control over hardware events. If you run a headless server, a Raspberry Pi, or a backup NAS, you can configure the operating system to listen for a specific hardware event—like plugging in a specific external hard drive—and instantly trigger a bash script in response.

This allows you to create incredibly satisfying automated workflows, such as plugging in a USB thumb drive and having Linux automatically sync your backup files to it, run a virus scan on it, or pull specific configuration files from it, all without you ever needing to log into the terminal.

This automation is achieved by writing a custom rule for the udev (userspace /dev) device manager.

Step 1: Identify Your USB Drive

First, we need to find the unique Vendor ID and Product ID of your USB drive so Linux knows exactly which device to listen for.

  1. Open your terminal.
  2. Plug the USB drive into your Linux machine.
  3. Run the command: lsusb
  4. Look for your drive in the list. The output will look something like this:
    Bus 002 Device 004: ID 0781:5581 SanDisk Corp. Ultra
  5. Write down the ID block (e.g., 0781:5581). The first four characters are the Vendor ID (idVendor), and the second four are the Product ID (idProduct).

Step 2: Create the udev Rule

Now we tell the device manager what to do when it sees that specific ID.

  1. Navigate to the rules directory:
    cd /etc/udev/rules.d/
  2. Create a new rules file using a text editor (requires root privileges). The file should start with a high number (like 99) so it runs after standard system rules:
    sudo nano 99-usb-autobackup.rules
  3. Paste the following code into the file, replacing the IDs with the ones you found in Step 1, and the script path with your actual script:
    ACTION=="add", SUBSYSTEM=="usb", ATTRS{idVendor}=="0781", ATTRS{idProduct}=="5581", RUN+="/usr/local/bin/my_backup_script.sh"
  4. Save the file and exit the editor (in nano, press Ctrl+O, Enter, then Ctrl+X).

Step 3: Prepare the Script

The script that udev runs must meet a few strict criteria to work correctly.

  1. It must be executable: Ensure you run sudo chmod +x /usr/local/bin/my_backup_script.sh on your script.
  2. Use absolute paths: Because udev runs the script in a sterile environment as the root user, standard environmental variables like $PATH or $HOME do not exist. Your script must use absolute, full paths for every command (e.g., use /usr/bin/rsync instead of just rsync).
  3. Do not block udev: If your script takes longer than a few seconds to run (like a massive file transfer), udev will forcibly kill it. To prevent this, your script should immediately fork its workload into the background using & or nohup.

Step 4: Reload udev Rules

Finally, tell the device manager to read the new configuration file you just created.

sudo udevadm control --reload-rules
sudo udevadm trigger

Your automation is now live. Unplug the USB drive and plug it back in. The udev daemon will instantly recognize the hardware IDs and execute your script in the background.

RELATED POSTS

  • How to Configure Linux cgroups v2 to Restrict Process Resource Usage
  • How to Use the Linux iostat Command to Diagnose Hard Drive I/O Bottlenecks
  • How to Use the Linux screen Command to Multiplex Terminals
  • How to Use the Linux kdump Utility to Mathematically Capture Kernel Panic Cores
  • How to Show the Calendar in Linux Using the cal Command
  • Get the best tech tips delivered straight to your inbox.

    Join thousands of readers mastering Apple, Google, Microsoft, and Linux.