automatically mount connected devices via udev

    The task of automatically mounting flash drives is solved quite simply in KDE or GNOME - these environments can be configured so that they themselves mount everything, open the file manager and show the tray icon. But what if you only have a console or is, for example, awesome? Or you do not want to deal with a specific DE, but looking for a universal solution?

    There is a solution independent of DE - udev .
    Create a new file /etc/udev/rules.d/automount.rules with the following contents:
    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"

    We indicate to udev that a new rule has appeared:
    sudo udevadm control --reload-rules

    Result I
    insert a USB flash drive and I see the directory / mnt / KINGMAX_Flash_Disk_1, in which the contents of the USB flash drive. Unmount and remove - the directory is gone.

    Remarks
    1. KERNEL == “sd [cz] [0-9]” - this 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 regex with "c".
    2. mount -o uid = 1000 - the id of the user is protected, which will be the owner. If yours is not standard, then correct it (check id -u). Of course, you can act through groups and masks, but I chose the simplest solution.

    umount I
    unmounted the problem (I need superuser rights) as follows:
    1. sudo visudo
    2. add the line% wheel ALL = NOPASSWD: / bin / umount

    upd
    As darkk noted, ID_VENDOR = "; / bin / rm -rf /;" - a potential security hole, therefore it is better to play it safe at the expense of visibility:
    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"



    Also popular now: