How can I mount a filesystem, mapping userids?

I can successfully mount an ext4 partition, the problem is that all the files on the partition are owned by the user with userid 1000. On one machine, my userid is 1000, but on another it’s 1010. My username is the same on both machines, but I realise that the filesystem stores userids, not usernames.

I could correct the file ownership with something like the following:

find /mnt/example -exec chown -h 1010 {} ;

But then I would have to correct the file ownerships again back to 1000 when I mount this external drive on another machine.

What I would like is to give mount an option saying map userid 1000 to 1010, so that I don’t have to actually modify any files. Is there a way to do this?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Take a look at the bindfs package. bindfs is a FUSE filesystem that allows for various manipulations of file permissions, file ownership etc. on top of existing file systems.

You are looking specifically for the –map option of bindfs:

--map=user1/user2:@group1/@group2:..., -o map=...
    Given a mapping user1/user2, all files owned by user1 are shown as owned by user2. When user2 creates files, they are chowned to user1 in the underlying directory. When files are chowned to user2, they are chowned to user1 in the underlying directory. Works similarly for groups.

    A single user or group may appear no more than once on the left and once on the right of a slash in the list of mappings. Currently, the options --force-user, --force-group, --mirror, --create-for-*, --chown-* and --chgrp-* override the corresponding behavior of this option.

    Requires mounting as root.

So to map your files with user id 1001 in /mnt/wrong to /mnt/correct with user id 1234, run this command:

sudo bindfs --map=1001/1234 /mnt/wrong /mnt/correct

Method 2

You can use bindfs. It can bind the filesystem to some other mountpoint with different uid/gid. However I think I would just change the uid so it is the same on both systems.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x