1

I'm user user and my primary group is user. If I change my primary group to primary_group, all of the files owned by me are suddenly owned by primary_group.

This was unexpected to me. If group user owns a file and I don't change file ownership, I don't expect the group ownership to change.

What other common pitfalls with regards to this are there?


Edited

I changed the primary group by using usermod -g primary_group user. I checked and this changed the integer identifier, it's not simply a naming issue.

  • This question should explain how the primary group was changed. pw groupmod -n and pw usermod -g are two quite different things. – JdeBP Mar 19 at 18:34
  • @JdeBP I didn't realise. I used usermod. – Not an artist Mar 20 at 9:30
1

File ownership (both user and group) are based on number and not on name

If you run ls -n on a file then you'll see numbers represented:

% ls -n $HOME/.profile
-rwx------ 1 500 500 2547 Mar  4 15:18 /home/sweh/.profile

Notice it says "500" for Uid, and "500" for Gid. These are the real owner and group owners for the file.

These numbers match the numbers in /etc/passwd and /etc/group. The name you see from ls is done by a lookup on the number.

So if you change the group name in /etc/group then the results from ls will show the new name.

  • I did the test by looking at the numbers and the result is the same. My question would still stand had I used numbers instead of names. – Not an artist Mar 20 at 9:38
  • If you change your GID in /etc/passwd (eg change if from 1000 to 2000) then the file will remain unchanged. – Stephen Harris Mar 20 at 13:09
1

The usermod -g command from GNU coreutils will also change the gid of the files from the user's home directory, as documented in its manpage:

-g, --gid GROUP
    The group name or number of the user's new initial login group. The
    group must exist.

    Any file from the user's home directory owned by the previous
    primary group of the user will be owned by this new group.

    The group ownership of files outside of the user's home directory
    must be fixed manually.

strace-ing the usermod -g group user command shows that it's simply calling lchown(2) for each file in turn. This feature is not present in other usermod implementations (including the pw usermod from FreeBSD).

So that's rather a pitfall of the usermod -g command on Linux -- simply changing the primary group of a user will not magically change the gid of all files owned by that user.

  • Hmm. This is very helpful, it helps understand what exactly is going on. Thank you. My question is, though, about other pitfalls. If you are arguing that this is a quirk of usermod and it's unlikely that there are other pitfalls, can you please make that a little more explicit so it is obvious that you are answering the question? – Not an artist Mar 20 at 10:15
  • Nobody can seriously guarantee you that there are "no other pitfalls" when changing users and groups, so I'm not going to that, sorry. – mosvy Mar 20 at 10:20

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.