The task of automatic mounting of flash drives is solved quite simply in KDE or GNOME - these environments can be configured so that they will mount everything themselves, open the file manager and show the tray icon. But what if you only have a console or is it worth it, for example, awesome? Or you do not want to deal with a specific DE, but look for a universal solution?
Independent of DE solution is -
udev .
Create a new /etc/udev/rules.d/automount.rules file with the following content:
ACTION == "add" KERNEL == "sd [cz] [0-9]" RUN + = "/ bin / mkdir -p / mnt /% E {ID_VENDOR} _% E {ID_MODEL} _% n"
ACTION == "add" KERNEL == "sd [cz] [0-9]" RUN + = "/ bin / mount -o uid = 1000 / dev /% k / mnt /% E {ID_VENDOR} _% E {ID_MODEL } _% n "
ACTION == "remove" KERNEL == "sd [cz] [0-9]" RUN + = "/ bin / rmdir / mnt /% E {ID_VENDOR} _% E {ID_MODEL} _% n"
Let us indicate to udev that a new rule has appeared:
sudo udevadm control --reload-rules
ResultI insert a flash drive and see the directory / mnt / KINGMAX_Flash_Disk_1, in which the contents of the flash drive. Unmount and take out - the catalog is gone.
')
Remarks1. KERNEL == "sd [cz] [0-9]" means that it will work on all devices of the form / dev / sdc1 / dev / sdc2, / dev / sdg7. I have 2 hard drives: sda and sdb, so I started the regex with "c".
2. mount -o uid = 1000 - the id of the user who will be the owner th is sewn up. If yours is not standard, then correct it (checking id -u). Of course, you can act through groups and masks, but I chose the simplest solution.
umountThe problem of unmounting (need superuser rights) I decided this:
1. sudo visudo
2. add the line% wheel ALL = NOPASSWD: / bin / umount
updAs darkk noted, ID_VENDOR = "; / bin / rm -rf /;" - a potential security hole, so it is better to be safe at the expense of clarity:
ACTION == "add" KERNEL == "sd [cz] [0-9]" RUN + = "/ bin / mkdir -p / mnt /% k"
ACTION == "add" KERNEL == "sd [cz] [0-9]" RUN + = "/ bin / mount -o uid = 1000 / dev /% k / mnt /% k"
ACTION == "remove" KERNEL == "sd [cz] [0-9]" RUN + = "/ bin / rmdir / mnt /% k"