1

I'm running both Linux Mint (17.2) and Ubuntu (18.04) on two different machines. When I plug in an external hard drive or USB flash drive, the drive is automatically mounted. A directory is created (for example, /media/ben/FlashDrive/) with the user and group set to "ben:ben" and the permissions set such that the owner can read/write, and nobody else can read/write.

I would like to change the group and group permissions for all future USB flash drives and external hard drives.

In other words, if I plug in a USB flash drive or external hard drive, I would like it to be automatically mounted to the same automatically-determined mount point as before, with the user set to the current system user, but with the group set to some specific group (i.e. "users") and the group permissions set to read-only (instead of no access).

The idea is that, regardless of who inserts the USB flash drive, any valid user of the system can read the drive, but without having to manually configure each drive.

All the instructions I've found so far require editing /etc/fstab, and they require specifying the UUID or device ID of the specific drive, hard-coding the mount-point, and hard-coding both the user name and group. This means that /etc/fstab needs to be updated separately for each device, and it isn't flexible to multiple users.

Is this possible?

0

If you can think of a suitable match criteria you can write a udev rule that then uses the GROUP:= action to set the group of the just added device. see this link for a discussion. https://www.tundraware.com/TechnicalNotes/Deconstructing-Linux-udev-Rules/Deconstructing_Linux_udev_Rules.pdf

It is not clear to me, but I think that udev, the kernel etc. don't monitor /media/xxxx/device for changes, only the /dev and /sys directories. So you might have more work to do. Another approach is to use inotifywait, to watch the /media directory, and when a new mount point is created then change the group with chgrp, or the permissions with chmod.

Here is an example of the inotifywait cmd. In this case /media/greg19 already exists because I already have a usb stick plugged in. First I remove the stick, then I plug it back in. Then I remove another stick with two partitions on it, which was also already plugged in.

inotifywait -m -e create -e delete /media /media/greg19

Setting up watches.
Watches established.
/media/greg19/ DELETE,ISDIR BLUELEXAR
/media/greg19/ CREATE,ISDIR BLUELEXAR
/media/greg19/ DELETE,ISDIR BLUELEXAR
/media/greg19/ DELETE,ISDIR GREENUSB2
/media/greg19/ DELETE,ISDIR GREENUSB1

for some reason, even if I spec recursive mode, it only outputs the first event of creating the userdir under /media it does not output the event creating the mountpoint for the actual stick.

inotifywait -m -r -e create -e delete /media

Setting up watches. Beware: since -r was given, this may take a while! Watches established.

/media/ CREATE,ISDIR greg19

But you can still sudo chgrp the userdir. There is a problem however in changing the group of the mountpoint for the device, even as sudo. I think this is because the device is vfat device and there is no concept of group for those FS's. So the best you can do for that is to change the permissions to 0777 with chmod.

So now all you need is a shell script that runs forever in the background with inotifywait watching the /media directory and having the while loop in the shell script read from the output of inotifywait.

The last option is to create a (usesr) systemd service for doing what you want but I am not sure how to do that. pgmer6809

| improve this answer | |

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.