How to share a disk drive among several PCs?

I have a hard disk drive with EXT4 filesystem, this drive is mobile and sometimes it is connected to one PC and other times to another PC. My problem is about permissions set for whole content of that drive, because one PC doesn’t recognize its content and I need to change permissions, and the same situation when it is connected to another PC.

I wish to connect this drive to any PC and its content can be immediately recognized and accessed like NTFS drives with Windows PCs

After doing a little reading it seems that while NTFS supports permissions, it just doesn’t use them on windows or at the very least it only does things internally. You can’t turn off permissions on an ext4 filesystem, so you could try using ACL’s (Access Control Lists).

There are several ways to solve this problem. One way is to sync your UIDs/GIDs between the systems.

Alternatively, if it is just data, and you don’t need permissions you could use exfat instead of ext4.

1 Like

Exfat is not viable. Question: is it possible to have the same account in every Linux system host which is owner of that external drive, So it can be recognized automatically?

Yes. That was the forst option. Sync your uids.

The username doesnt matter. All that matters ia the uid.

Any tutorial about how to do it?

It’s not trivial to do. Here are some things to consider:

To find out the UID of your user, use this command:

id username

where you would substitute username with your user’s name.

It will give you a UID and GID, as well as a list of groups your user belongs to.

Do this on all of the computers and note it. You want those two numbers to be the same across all of your computers.

Now, to change the UID and GID of your user and his/her group, you have to do it in two steps. The first is to change the group’s GID, then change the user’s GID and UID. You can use the following two commands:

groupmod -g new_GID groupname
usermod -u new_UID -g new_GID username

where you would substitute new_GID with the new GID, and groupname with the name of the group for which you change the GID, and new_UID with the new UID, new_GID with the new GID (the same one as in the previous command), and username with the your user’s name.

You can verify that you’ve done it correctly using the id command, given above.

However, using these commands on a running system is not a good idea, because there are two undesirable side effects:

  • The UID and GID for processes that are currently running will not change.
  • The UID and GID for the files on your system will not change.

There are also other things to keep in mind:

  • If there are other users in the group for which you’re changing the GID, their group membership will not be updated, you need to do this manually.
  • If the UID or GID is already used, you will need to change that one first to something else.

It is best to do any of this in a chroot, or in a TTY, logged in as root and logged out with any other user. You will have to manually change the UID and GID for all the user owned files on your computer. Doing so requires extreme caution, because if your mess up, it’s very difficult to reverse it.

You could do it with the find command, but its syntax is not very intuitive. You also need to keep in mind any symbolic links, which might be broken.

These are the commands:

find / -uid old_UID -exec chown -v -h new_UID '{}' \;
find / -gid old_GID -exec chgrp -v -h new_GID '{}' \;

where you would substitute old_UID, new_UID, old_GID and new_GID with the appropriate numbers. You may want to do a dry run without the -exec option (and everything following it).

That should be it, but before you do it, make sure you understand these commands (read the manuals), and have somebody review these steps, in case I forgot something.

As always, keep a good backup in case something goes wrong.

2 Likes

@d-air1 is right, but since kernel 5.12, the kernel supports user mapping. I think it was introduced to mount in 5.39 of util-linux, so it’s really recent – not older than a few weeks.

Now, you can use these options:

   X-mount.owner=username|UID, X-mount.group=group|GID
       Set mountpoint's ownership after mounting. Names resolved in the target mount namespace, see -N.

   X-mount.mode=mode
       Set mountpoint's mode after mounting.

   X-mount.idmap=id-type:id-mount:id-host:id-range [id-type:id-mount:id-host:id-range], X-mount.idmap=file
       Use this option to create an idmapped mount. An idmapped mount allows to change ownership of all files located under a mount
       according to the ID-mapping associated with a user namespace. The ownership change is tied to the lifetime and localized to
       the relevant mount. The relevant ID-mapping can be specified in two ways:

       •   A user can specify the ID-mapping directly.

           The ID-mapping must be specified using the syntax id-type:id-mount:id-host:id-range. Specifying u as the id-type prefix
           creates a UID-mapping, g creates a GID-mapping and omitting id-type or specifying b creates both a UID- and GID-mapping.
           The id-mount parameter indicates the starting ID in the new mount. The id-host parameter indicates the starting ID in the
           filesystem. The id-range parameter indicates how many IDs are to be mapped. It is possible to specify multiple
           ID-mappings. The individual ID-mappings must be separated by spaces.

           For example, the ID-mapping X-mount.idmap=u:1000:0:1 g:1001:1:2 5000:1000:2 creates an idmapped mount where UID 0 is
           mapped to UID 1000, GID 1 is mapped to GUID 1001, GID 2 is mapped to GID 1002, UID and GID 1000 are mapped to 5000, and
           UID and GID 1001 are mapped to 5001 in the mount.

           When an ID-mapping is specified directly a new user namespace will be allocated with the requested ID-mapping. The newly
           created user namespace will be attached to the mount.

       •   A user can specify a user namespace file.

           The user namespace will then be attached to the mount and the ID-mapping of the user namespace will become the ID-mapping
           of the mount.

           For example, X-mount.idmap=/proc/PID/ns/user will attach the user namespace of the process PID to the mount.

Since this is so new, you won’t find much on the internet about it. But if you are willing to be a pioneer and write a nice howto, countless future generations of linux users will be thankful, me included.

2 Likes