Linux - Mount device with specific user rights

93

32

How can I mount a device with specific user rights on start up? I still have some problems figuring it out. I would like to mount the divide with uid=1000 and gid=1000. My current entry to the /etc/fstab/ file looks like this:

dev /var/www vboxsf rw, suid, dev, exec, auto, nouser, async, uid=1000

wowpatrick

Posted 2011-08-08T12:32:22.183

Reputation: 3 039

1

I've been messing around with this problem in vbox for a while now too. From what I've gathered, the correct solution (to the question you aren't asking) is to add your user into the vboxsf group, and then it doesn't matter who the owner of the files are - you will have permission to edit them. http://alcobrov.blogspot.com/2012/06/add-user-in-vboxsf-group-to-access.html

– stevemidgley – 2014-08-23T00:00:31.780

Don't forget gui=1000. Also, what is the ownership/rights to /var/www. It should be owned by root. – skub – 2011-08-08T12:57:47.133

1@skub: The owner of /var/www/ is root. dev /var/www vboxsf rw, suid, dev, exec, auto, nouser, async, uid=1000 gui=1000 didin't work so well (Ubuntu removed the entry after a failed restart). – wowpatrick – 2011-08-08T21:14:20.377

2Your mount source is "dev"?? – James T Snell – 2011-08-08T21:51:18.693

@wowpatrick - your mount device should be something like /dev/sda1 it should not be 'dev'. – skub – 2011-08-08T22:53:52.577

1@skub: It's a VirtualBox shared folder, so /dev is is right. I figured it out by now, sudo mount -t vboxsf -o umask=0022,gid=33,uid=33 dev /var/www works just fine. – wowpatrick – 2011-08-08T23:16:47.233

Depending on what the device is for, you may also need to add stuff like "nosuid" for extra security. See man mount for the generic mount options and man [fs name] for file system-specific ones. – billc.cn – 2011-08-09T00:08:35.527

Answers

125

To mount a device with certain rights, you can use the -o Option directive while mounting the device. To mount the device you described, run:

 mount -t deviceFileFormat -o umask=filePermissions,gid=ownerGroupID,uid=ownerID /device /mountpoint

For example mounting a VirtualBox shared folder to /var/www with www-data as owner would look like this:

mount -t vboxsf -o umask=0022,gid=33,uid=33 dev /var/www

If you want to mount the device on startup, you can add the following entry to your /etc/fstab file:

 /device /mountpoint deviceFileFormat umask=filePermissions,gid=ownerGroupID,uid=ownerUserID

Again, with the same example the entry to the /etc/fstab file would look like this:

dev /var/www vboxsf umask=0022,gid=33,uid=33

For filesystems that does not support mounting as a specific user (like ext4) the above will give the error

Unrecognized mount option "uid=33" or missing value

to change the owner of an ext4 mount simply run

chown username /mountpoint

after it has been mounted.

wowpatrick

Posted 2011-08-08T12:32:22.183

Reputation: 3 039

I was able to use the uid/gid option on ext4. – CMCDragonkai – 2015-12-09T09:45:25.437

This doesn't seem to work with mount --bind , i'm using a btrfs file system – meffect – 2017-03-18T16:48:04.790

Shouldn't the umask be umask=0077 instead of umask=0022 to give permission only owner to read or write? It seems that umask=0022 will give read permissions to others if I am reading it correctly. I want that only the user who mount the disk should have read or write permission. – Mian Asbat Ahmad – 2019-01-09T14:00:19.890

2

For a file-system like ext3 or ext4, after doing

    chown -R username:group /mountpoint

to change the owner of the currently existing files you can set the group id bit to have new files created with the specific group (doesn't work for the user id under Linux):

    find /mountpoint -type d -exec chmod g+ws {} \;

The Wikipedia entry on setuid and setgid is quite informative, see the section on directories.

js.

Posted 2011-08-08T12:32:22.183

Reputation: 225

6-o unfortunately doesn't work for ext4, as explained in the answer by @wowpatrick. – js. – 2014-08-18T20:55:43.687

15chowning the contents of the mounted drive to some other user is ridiculous. You have no idea what you might break for any applications on that drive. It may be fine if all of the contents belong to your user, but this is a very big no-no... – carlspring – 2015-04-12T01:56:22.227

The device in question is /dev/www, it is easy to control which applications have access to it (probably only a WWW server, which you can turn off during the operation). Even if the app using it is still running, it will keep running, since that is the point of the operation. – js. – 2015-04-13T07:40:05.830

19Changing the ownership of all the files on the device is very invasive. Since there is a -o option for mount, it is the better way. – Limited Atonement – 2013-04-29T15:16:32.760