139

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
Braiam
  • 4,709
  • 3
  • 26
  • 57
wowpatrick
  • 4,199
  • 7
  • 31
  • 40
  • Don't forget gui=1000. Also, what is the ownership/rights to /var/www. It should be owned by root. – skub Aug 08 '11 at 12:57
  • 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 Aug 08 '11 at 21:14
  • 2
    Your mount source is "dev"?? – James T Snell Aug 08 '11 at 21:51
  • @wowpatrick - your mount device should be something like /dev/sda1 it should not be 'dev'. – skub Aug 08 '11 at 22:53
  • 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 Aug 08 '11 at 23:16
  • 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 Aug 09 '11 at 00:08
  • 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 Aug 23 '14 at 00:00

1 Answers1

174

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.

p-na
  • 3
  • 2
wowpatrick
  • 4,199
  • 7
  • 31
  • 40
  • I was able to use the uid/gid option on ext4. – CMCDragonkai Dec 09 '15 at 09:45
  • This doesn't seem to work with mount --bind , i'm using a btrfs file system – Northstrider Mar 18 '17 at 16:48
  • 1
    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. – Dr. Mian Jan 09 '19 at 14:00
  • uid=1000,gid=1000 do not work on btfs filesystem on kde with debian 11 on kernel 5.10.0-8-amd64. The error says: mount: /store1: wrong fs type, bad option, bad superblock on /dev/sda1, missing codepage or helper program, orother error. The complete line from /etc/fstab is: "UUID=755d31e9-4ed0-4a41-9821-dd777f12e3dd /store1 btrfs uid=1000,gid=1000,noatime,nodiratime 0" – piotao Sep 11 '21 at 14:49