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?

qbi
  • 19,515

3 Answers3

25

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
Catskul
  • 762
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
Lekensteyn
  • 178,446
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 ./
Eliah Kagan
  • 119,640
Chinoto
  • 81