1

I am wondering if there is a difference between using usermod or groupmod to add a user to another existing group. Example: we have a supplementary group named artists and want to add romeo to this group.

usermod -G artists romeo
groupmod -U romeo artists

I am using Redhat 9.

1
  • usermod -aG artists romeo For more information, man usermod Aug 10, 2022 at 12:51

2 Answers 2

3

From the man page for groupmod:

The groupmod command modifies the definition of the specified GROUP
by modifying the appropriate entry in the group database.

There isn't a -U option (shadow-utils 4.6 of 04/18/2022). The purpose of groupmod is to modify the characteristics of the group, not its members.

From the man page for usermod:

-G, --groups GROUP1[,GROUP2,...[,GROUPN]]]
  A list of supplementary groups which the user is also a member of ...

  If the user is currently a member of a group which is not listed, the user
  will be removed from the group. This behaviour can be changed via the -a
  option, which appends the user to the current supplementary group list.

So, if your user romeo is already a member of group paintworkers, for example, then after:

usermod -G artists romeo

he will ONLY be a member of supplementary group artists and not of paintworkers. If however you use:

usermod -aG artists romeo

he will be a member of both groups.

Update:

As mentioned above, this is true for shadow-utils 4.6 of 04/18/2022. However the OP has pointed out that shadow-utils 4.9 of 12/02/2021 does now document -U:

       -U, --users
           A list of usernames to add as members of the group.

so presumably either the usermod -aG or the groupmod -U form should work equally well with the later version of shadow-utils.

1
  • tnx for the answer. I made a mistake in my question information. I'm using redhat 9 and there is a -U option for groupmod command. To clarify both commands are working fine for me. I am just wondering if there is a diffrence. Aug 11, 2022 at 12:16
0

... seeing the forest through the trees

everything you are referring to ends up simply happening in /etc/group

/etc/group format

<groupname>:x:gid:user1,user2,user_n

x = password field, almost never used, would reference /etc/gshadow just like /etc/passwd references /etc/shadow.

gid = group id, a unique group id. Convention is < 100 system stuff don't use, < 1000 for admin stuff, > 1000 whatever. don't quote me on this though

for any groupname, simply add all user accounts to be in that group, separated by a comma. I don't think I've ever used usermod or groupmod regarding what supplemental groups a user account is in, I just edit /etc/group directly is seems simpler, faster, easier to see exactly what all is going on.

Maybe the only point is there's a primary group for any given user account, which is specified by gid in /etc/passwd in column 4 for each account. But all supplemental groups to which any user account is a member of are specified in /etc/group as to who is a member of what. However you use usermod or groupmod ends up simply changing these things.

usermod -G will add a given user account name to the end of the list, in /etc/group for the group names specified. If your adding one user to one new group, it's easier to just manually edit /etc/group otherwise for 5+ groups to do at one time this command might be a little faster than doing it manually in /etc/group.

groupmod -U I see as not a valid choice (in RHEL 7)

groupmod --help
Usage: groupmod [options] GROUP

Options:
  -g, --gid GID                 change the group ID to GID
  -h, --help                    display this help message and exit
  -n, --new-name NEW_GROUP      change the name to NEW_GROUP
  -o, --non-unique              allow to use a duplicate (non-unique) GID
  -p, --password PASSWORD       change the password to this (encrypted)
                            PASSWORD
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       prefix directory where are located the /etc/* files
2
  • To be honest, I usually edit directly, but it is not considered best practice, particularly for those new to the game. Mess up the editing and you can cause trouble whereas the utilities guard against the sort of accidental slip someone unfamiliar might make. They also take care of getting the relevant shadow files right.
    – user516667
    Aug 10, 2022 at 20:12
  • tnx for the answer. I made a mistake in my question information. I'm using redhat 9 and there is a -U option for groupmod command. To clarify both commands are working fine for me. I am just wondering if there is a diffrence. Aug 11, 2022 at 12:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .