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'm currently working in a command line only environment.

When I plug my USB key in, I see a new device file in /dev:


If I simply sudo mount /dev/sdi1 /media/tmp, and umount it when I'm done, I have to repeat the process all over again. This alone could be accomplished with a little script but my key doesn't always show up as sdi.

Is there a way for me to have it always auto-mount and maybe reserve sdi for it?

Note: Also, there seems to be orphaned device files in /dev if I forget to unmount and just pull the stick out.

share|improve this question
up vote 6 down vote accepted

I use this Udev rule from the Arch Wiki:

KERNEL!="sd[a-z][0-9]", GOTO="media_by_label_auto_mount_end"

# Import FS infos
IMPORT{program}="/sbin/blkid -o udev -p %N"

# Get a label if present, otherwise specify one
ENV{ID_FS_LABEL}!="", ENV{dir_name}="%E{ID_FS_LABEL}"
ENV{ID_FS_LABEL}=="", ENV{dir_name}="usbhd-%k"

# Global mount options
ACTION=="add", ENV{mount_options}="relatime"
# Filesystem-specific mount options
ACTION=="add", ENV{ID_FS_TYPE}=="vfat|ntfs", ENV{mount_options}="$env{mount_options},utf8,gid=100,umask=002"

# Mount the device
ACTION=="add", RUN+="/bin/mkdir -p /media/%E{dir_name}", RUN+="/bin/mount -o $env{mount_options} /dev/%k /media/%E{dir_name}"

# Clean up after removal
ACTION=="remove", ENV{dir_name}!="", RUN+="/bin/umount -l /media/%E{dir_name}", RUN+="/bin/rmdir /media/%E{dir_name}"

# Exit

Just change the "sd[a-z][0-9]" in the first line to avoid clashes with your other drives...

share|improve this answer
So all I do is place this in /etc/udev/rules.d/automount_usb.rules? Do I need a reboot? – n0pe Sep 19 '11 at 19:57
Not according to the wiki: "Udev automatically detects changes to rule files, so changes take effect immediately without requiring udev to be restarted." – jasonwryan Sep 19 '11 at 20:40

On Debian and Ubuntu there is the package usbmount that should do exactly what you ask.

I am sure it is available in other Linux distros too.

share|improve this answer
My bad, I should have mentioned I was on OpenSuse. usbmount isn't in the repos but I'm sure I could fine the source. – n0pe Sep 19 '11 at 20:02
Source available on the usbmount home page :-) usbmount.alioth.debian.org, it's dependencies are listed at packages.debian.org/squeeze/usbmount – invert Sep 19 '11 at 20:50

There are a number of auto-mount solutions out there, but I'd especially recommend those based on udev - like uam for example.

Also, for normal user, command-line, on-demand mounting I'd recommend pmount (Policy based mounting programs that does not require any sudo).

share|improve this answer
Doesn't pmount require the obsolete hal? – enzotib Sep 19 '11 at 19:41
Definitely not. It is a wrapper around mount that allows users mount just the removable media. – rozcietrzewiacz Sep 19 '11 at 19:52

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.