I have a Debian sid system (Wheezy) (and same question for arch as well), without any desktop environment (and no Xorg at all).

I can mount my SD-cards, USB sticks, external HDD manually with mount / umount with the suitable entries in /etc/fstab but this is compelling, and to restrictive: if I want them to be mounted in /media/<LABEL> each device with a different <LABEL> needs its own entry, and each <LABEL> directory needs to be created / removed manually in /media/ as well).

So, what is the best way to mount them automatically in /media/<LABEL> at insertion (and to unmount them from the filesystem as soon as they are unplugged)?

The ideal solution would:

  1. detect when a removable media is plugged-in (added in /dev/ as sdax, sdbx, ... by udev)
  2. create a directory in /media/ according to its label (label of the removable media)
  3. mount it in the directory /media/<LABEL> in RW mode (if it's filesystem is supported)
  4. detect if the media has been unplugged
  5. if then, unmout it from the filesystem
  6. remove the corresponding directory from /media/

(the devices should be mounted in synchronous mode oviously, to avoid any data loss when hot unplugged because of caching)

I found some info about autofs, HAL, udisks, udisks2, etc., but it's unclear which one is deprecated or preferred, and anyway, I haven't figured out how to configure them easily on my system to do that, up to now ...

Minimalism, elegance, KISS, *nix-minded, no crazy unbearable XML policies files, and up-to-date highly appreciated.

edit: trying to make my question more clear

  • 1
    I posted my questions on unix.stackexchange.com/questions/44454, maybe I'll have some more answers ... I'm not sure whether it should be deleted from here or not?
    – cedbeu
    Aug 1, 2012 at 8:46
  • you did same post in diferent sites, with same answer in both self answered... Aug 21, 2015 at 12:36

3 Answers 3


Ok, it's been a long time, but I'll still answer my question with the best option I found as of now.

The best way is to create a udev rule, associated with some scripts (that will create / remove directories and mount / unmount removable devices), and attached to partition udev device event type.

1 - Creating add / remove scripts

Add this script storage-automount.sh in /lib/udev/ and set it to executable (sudo chmod +x /lib/udev/storage-automount.sh):


# set the mountpoint name according to partition or device name
if [ -z $mount_point ]; then

# if a plugdev group exist, retrieve its gid set & it as owner of mountpoint
plugdev_gid="$(grep plugdev /etc/group|cut -f3 -d:)"
if [ -z $plugdev_gid ]; then
    chown root:plugdev $mount_point

# create the mountpoint directory in /media/ (if not empty)
if [ -n $mount_point ]; then
    mkdir -p /media/$mount_point
    # other options (breaks POSIX): noatime,nodiratime,nosuid,nodev
    mount -t $ID_FS_TYPE \
      -o rw,flush,user,uid=0$gid,umask=002,dmask=002,fmask=002 \
      $DEVNAME /media/$mount_point

Add this script storage-autounmount.sh in /lib/udev/ and set it to executable (sudo chmod +x /lib/udev/storage-autounmount.sh):


# set the mountpoint name according to partition or device name
if [ -z $mount_point ]; then

# remove the mountpoint directory from /media/ (if not empty)
if [ -n $mount_point ]; then
    umount -l /media/$mount_point
    rm -R /media/$mount_point

2 - Creating the udev rule to attach those scripts to events

And finally, add a udev rule in /etc/udev/rules.d, for instance 85-storage-automount.rules:

ENV{DEVTYPE}=="partition", RUN+="/lib/udev/storage-automount.sh", ENV{REMOVE_CMD}="/lib/udev/storage-autounmount.sh"

And that's it.

Now, when you plug a storage device in, a directory will be created in /media/ according to the partition name (I don't remember but I think it's working with NTFS partitions as well) and your partition will be mounted into it. It's R/W for users if you have a plugdev group on your system. Also, the devices are mounted in synchronous mode in order to limit the risks of data loss in case of hot unplugging.

When the device is removed, it's unmounted and the directory is removed from /media.

Also, the tool to monitor the udev events is udevadm monitor, with options like --env or --property:

$ udevadm monitor --env

This is tested and working fine on both Debian and Arch, but probably works on all distributions that rely on udev.


You might want to check your /dev/disk/by-uuid directory while those specific drives are plugged-in. Using ls -l it will show you the devices (e.g. /dev/sdb1) along with their uuids. Having found the matches, update your /etc/fstab accordingly, replacing /dev/sd* with the corresponding /dev/disk/by-uuid/* entry.

For details, see my answer to How do I prevent USB flash drive from getting a new Linux device name after wake from sleep?.

  • 1
    Actually, as I said, the devices are plugged in, I know their UUID/LABEL through either blkid or the /dev/disk/by-uuid directory. And I can mount them manually as well. Now, I already have them in /etc/fstab, actually eiter by label or by UUID, both work correctly. That doesn't really help though, since they are mounted at boot time only (as long as I do not specify noauto). But if I remove them and if I reinsert them while the system is running, they are not automatically unmounted / mouted in the directories I specified in fstab... This is my problem.
    – cedbeu
    Jul 31, 2012 at 12:17
  • Looks like something weird -- as I use it for years for removables (e.g. SD-cards from my cameras, mobile devices as MP3 player, smartphones, tablets... And they all mount correctly at the designated mountpoint. If you remove them: do you unmount before, and mount again when re-inserting? You should do so, and then it should work.
    – Izzy
    Jul 31, 2012 at 12:22
  • 1
    If I unmount them and mount them manually, everything works fine obviously. But my problem is that I want them to unmount (sync option in fstab) and mount them automatically (and as user as well as root) when I remove / insert them. That's why I guess either AutoFS, HAL, udisks, udisks2 (or hotplug, maybe ...?) or something similar is involved... But don't know what I'm missing exactly.
    – cedbeu
    Jul 31, 2012 at 12:27
  • You probably miss that you still must unmount them manually -- or how shall the system know you're about to do that? Obvioulsy, once you unplugged it cannot be sync'd anymore, and neither cleanly unmounted. So the system assumes some drive-error -- but many times only discards the "old drive" after you plugged in the "new drive" again. So on auto-mount, the old mount point is still occupied -- which makes the "new" drive mount elsewhere.
    – Izzy
    Jul 31, 2012 at 12:39
  • The hot plug / unplug system like hal (amongst others mentionned) should take care of unmounting the device of the fs if it's not plugged anymore. The sync option in /etc/fstab is used in this case for syncing the files of the device without caching, though avoiding data loss when hot-unplugging (w/o unmounting manually before).
    – cedbeu
    Jul 31, 2012 at 13:31

Thanks for your hints. I simplified your mount and unmount scripts like this:



udisks --mount $DEVNAME



udisks --unmount $DEVNAME

My /etc/udev/rules.d/ file looks like this:

# ==1: mount filesystem to a shared directory (/media/VolumeName)
# ==0: mount filesystem to a private directory (/run/media/$USER/VolumeName)
# See udisks(8)
ENV{ID_FS_USAGE}=="filesystem|other|crypto",       ENV{UDISKS_FILESYSTEM_SHARED}="1"
ENV{DEVTYPE}=="partition", RUN+="/lib/udev/storage-automount.sh",     ENV{REMOVE_CMD}="/lib/udev/storage-autounmount.sh"

It's simple and should be a typical use case.

  • 8 years later, this is not working anymore, as we might have to use udisksctl instead of udisks, and udisksctl seems too slow, that it can't yet mount, when udev` asks for it.
    – Daniel
    Apr 3, 2023 at 16:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .