In a typical GUI file manager, like PCManFM, I can mount a USB drive by simply clicking on its icon. Is there a similar command I can use which automatically creates a directory in the right place (/media/USER/label) before it mounts the drive? I use Debian 9.



udiskctl mount -b /dev/sdXy


udiskctl mount -b /dev/sdb1

mounts 1. Partition from /dev/sdb


udisks --mount /dev/sdXy
  • If you update your answer and use /dev/disk/by-label/LABEL instead the command will be even easier to use (and you will get more upvotes). – August Karlstrom Jul 11 '17 at 10:00

I think the best way in this situation is to use udev. This is ready, copy-paste solution:

Simply create a file /etc/udev/rules.d/11-media-by-label-auto-mount.rules with the following:

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  

Once you have created your script and saved it, reload udev with:

udevadm control --reload-rules

Now, when you connect the USB storage device, udev should automatically create a directory under /media with either the device ID, or device label (if you have assigned one), and mount the device. If you disconnect the device, the drive directory (in /media) will be automatically removed.

To get to know more about udev read this.

  • @Archemar edited. – mrc02_kr Jul 11 '17 at 10:11

That's not the neatest solution, because you are leaving on your machine an empty dir for the label of every device you're going to mount.

However, you could try something like this (warning: I haven't tested it):

mkdir -p /media/$USER/$MYDEVLABEL

Remember to add the necessary options to the mount command.

  • 1
    The corresponding unmount command should of course delete the directory. This is what e.g. PCManFM does. – August Karlstrom Jul 11 '17 at 9:49

Even if I like the pure udev answer, there is also a tool called usbmount that you can install by

apt-get install usbmount

which automount disks and is quite simple to configure through its configuration file.
I prefer no automount at all but some clients ask for it.

  • The package usbmount is not in Debian 9's standard repositories. – August Karlstrom Jul 11 '17 at 9:55
  • my bad, it's in wheezy, jessie and sid ... – M4rty Jul 11 '17 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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