Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers 3

up vote 6 down vote accepted

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
share|improve this answer
    
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 '13 at 15:21
1  
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 '13 at 17:34
add comment

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 ./
share|improve this answer
add comment

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
share|improve this answer
1  
bindfs has other modes of operation, see: bindfs.org/docs/bindfs.1.html –  Lekensteyn Oct 5 '13 at 17:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.