Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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 have a USB key that contains my keepass2 password database and I'd like to perform some actions when it is plugged into my computer, namely:

  • Auto-mount it to some specific location
  • When the mounting is done properly, launching keepass2 on the password database file

Simple tasks I guess, but I can't find how to do that.

I'm using Ubuntu 12.10, and it auto-mounts the device as a "media usb-key" and tries to open the images on it (even though there are none).

What is the best way to do that and to disable the ubuntu auto-mounting (so it doesn't conflict) ?

share|improve this question
up vote 8 down vote accepted

When a new device appears, udev is notified. It normally creates a device file under /dev based on built-in rules¹. You can override these rules to change the device file location or run an arbitrary program. Here is a sample such udev rule:

KERNEL=="sd*", ATTRS{vendor}=="Yoyodine", ATTRS{serial}=="123456789", NAME="keepass/s%n", RUN+="/usr/local/sbin/keepass-drive-inserted /dev/%k%n"

The NAME= directive changes the location of the device file, I included it for illustration purposes but it is probably not useful for your use case. The ATTRS rules identify the device; run udevinfo -a -n /dev/sdz when the drive is available as /dev/sdz to see what attributes it has. Beware that you can only use ATTRS rules from a single section of the udevinfo input (in addition, you can use ATTR rules from the initial section). See Understand output of `udevadm info -a -n /dev/sdb` for more background. This rule goes into a file called something like /etc/udev/rules.d/local-storage-keypass.rules.

Put the commands you want to run in the script given in the RUN directive. Something like:

set -e
if [ -d /media/keypass-drive ]; then
  [ "$(df -P /media/keypass-drive | awk 'NR==2 {print $1}')" = "$(df -P /media | awk 'NR==2 {print $1}')" ]
  mkdir /media/keypass-drive
mount "$1" /media/keypass-drive
su ereon -c 'keypass2' &

If you're having trouble running a GUI program from a script triggered from udev, see Can I launch a graphical program on another user's desktop as root?

¹ Not on modern systems where /dev is on udevtmpfs.

share|improve this answer
Important note on this: When writing rules using the info from udevadm info -a -n /dev/sdX, you cannot match based on multiple ancestors. You can only match based on data from the device itself, and a single ancestor. This is covered in man 7 udev, but is a small comment easily overlooked. – Patrick Nov 6 '13 at 4:09

The best way of doing this would be writing your own udev rule(s) for this device. Unfortunately I can't give any tips on how to do this as I've never done so. So unless someone else has a more detailed answer, google for 'udev rules' and look in /lib/udev for examples. Your custom udev rules should go in /etc/udev/rules.d

share|improve this answer
Thanks for the info. This will at least make my searches more efficient. – ereOn Nov 12 '12 at 12:39

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.