21

I want to mount the device /dev/sda3 to the directory /foo/bar/baz. After mounting the directory should have the uid of user johndoe. So I did:

sudo -u johndoe mkdir /foo/bar/baz
stat -c %U /foo/bar/baz
johndoe

and added the following line to my /etc/fstab:

/dev/sda3 /foo/bar/baz ext4 noexec,noatime,auto,owner,nodev,nosuid,user 0 1

When I do now sudo -u johndoe mount /dev/sda3 the command stat -c %U /foo/bar/baz results in root rather than johndoe. What is the best way to mount this ext4-filesystem with uid johndoe set?

1

3 Answers 3

24

bindfs is the answer. It will take an already mounted file system and provide a view of it with whichever uid you'd like:

sudo apt-get install bindfs
mkdir ~/myUIDdiskFoo
sudo bindfs -u $(id -u) -g $(id -g) /media/diskFoo ~/myUIDdiskFoo
4
  • 2
    bindfs has other modes of operation, see: bindfs.org/docs/bindfs.1.html
    – Lekensteyn
    Oct 5, 2013 at 17:34
  • 2
    Ugh, it uses FUSE.
    – Navin
    Jun 20, 2017 at 16:27
  • 1
    I wanted to mount a 9p virtual file system with different user permissions than the host OS.. Was banging my head against the wall trying to make it work. Bindfs really got me out of a bind. Great little tool!
    – Boann
    Oct 4, 2018 at 18:18
  • 1
    It is also possible to create an fstab entry, for example: /run/media/your_username/diskFoo /mnt/diskFoo fuse.bindfs force-user=your_username,force-group=your_username 0 0 Then, after mounting normally, also run: sudo mount /mnt/diskFoo Then in /mnt/diskFoo all files will be owned by your_username Also, it may be possible to do the later via autofs, but I did not tried that yet. Please share if you know how to do it.
    – Ashark
    Oct 29, 2020 at 21:06
23

It's not possible to force an owner on a disk with an ext4 filesystem. Only filesystems which do not support Linux permissions like fat have an attribute for ownership/groupship: uid=value and gid=value. See the manual page on mount.

You should change the owner on the mounted filesystem as in:

sudo chown johndoe /foo/bar/baz

If you need to change the permissions recursively:

sudo chown -R johndoe /foo/bar/baz

...and if the group needs to be changed to johndoe as well:

sudo chown -R johndoe: /foo/bar/baz
3
  • 2
    Recursively changing ownership is generally a bad idea if you can help it. Once you've wiped out the "correct" ownership, there's virtually no easy way to get it back. IMO bindfs is the correct way to handle this (see my answer).
    – Catskul
    Oct 5, 2013 at 15:21
  • 3
    bindfs sounds like another layer of complexity. My answer is best suitable for an external hard disk (for backup) which was used in a previous installation with a different user ID. It should not be used in situations where multiple user ids need to be kept (root filesystem), but in that case bindfs with -u should also not be used because it circumvents access restrictions from the different user ids. --map should be used if you want to preserve access restrictions.
    – Lekensteyn
    Oct 5, 2013 at 17:34
  • 1
    +1 for the "it's only possible on permission-less FSs" - I've been trying to use uid and gid, which wouldn't work. Dec 11, 2015 at 20:36
7

Assuming the group set for the files has appropriate permissions, you should be able to just find out it's Group ID (GID) and add yourself to it.


To find out the group id of a file, use ls -n. Example output:

drwxr-xr-x 1 1000 1000  550 Jul  9 11:08 Desktop/

You want the fourth field for GID, which in my case is the second 1000.

sudo addgroup --gid GID foobar; sudo usermod `whoami` -aG foobar

Once you run that you should have full rein where ever the file attributes for group allow it.

As noted in the other answer relating to user permissions, if you need to fix the group on some files, run one of these:

sudo chown -R :foobar ./
sudo chgrp -R  foobar ./
1
  • 3
    Note if you want to switch the user or group from A to B without changing the ownership of files that are not A; you can use sudo chown --from A B -R ./ for users and sudo chown --from :A :B ./ for groups Oct 6, 2017 at 3:13

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.