Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to automatically run my own command any time a drive is inserted under Linux (including USB drives, for example). Obviously the simplest solution would be to regularly poll and check for changes to /proc/partitions or to /dev/disk, but I was hoping for something a little more elegant than that.


I know this is possible, because GNOME does this very thing; any time a new drive is inserted, GNOME runs it's own "autoplay" mechanism to take some action. ("Would you like to view the pictures on this SD card?")

I can come up with two uses for this: the first is in an Amazon EC2 cluster I manage, to detect and initialize newly attached EBS storage volumes automatically.

The other usage is on a Linux-based home file-server device; when I insert an SD card into the device, it should mount the card, move the pictures from the card onto the internal hard drive, unmount the card, and then beep to let me know it's done.

share|improve this question
up vote 11 down vote accepted

Udev rules are what you are looking for. From the man page itself:

... If udev receives a device event, it matches its configured rules against the available device attributes provided in sysfs to identify the device. Rules that match may provide additional device information or specify a device node name and multiple symlink names and instruct udev to run additional programs as part of the device event handling. ...

Emphasis mine.

The rules files are in /etc/udev/rules.d/ and their documentation is in udev(7). Oh, and they work for practically everything.

share|improve this answer
+1 good answer. Could use the link in prestomation's answer – David Pashley Jun 7 '09 at 7:20

I found a pretty good link here.

Basically you can place scripts in /etc/udev for all block devices or specific ones.

share|improve this answer
+1 for including a link – David Pashley Jun 7 '09 at 7:21

Autorun ( ) takes care of CDs, but I don't think something like that exists for other media. I'm really not of the opinion that it /should/, either.

You might be able to craft autorun into looking at dmesg on occasion, but chances are good that it will only work for your pet system.

What is your overall goal? Maybe there's a better way?

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.