Usermod -aG vs gpasswd -a as a best practice
They both can do the same, it's just with usermod
you can do it wrong if you don't pay enough attention.
In this wiki from Arch Linux (it is the same for other distros), it's explained:
To add a user to other groups use (additional_groups is a comma-separated list):
# usermod -aG additional_groups username
Warning: If the
-a
option is omitted in theusermod
command above, the user is removed from all groups not listed in additional_groups (i.e. the user will be member only of those groups listed in additional_groups).Alternatively,
gpasswd
may be used. Though the username can only be added (or removed) from one group at a time.# gpasswd --add username group
![ZZ9](https://i.stack.imgur.com/VQ3Nf.jpg?s=256&g=1)
ZZ9
Updated on September 18, 2022Comments
-
ZZ9 almost 2 years
I am aware that
usermod -g
is not best practice from many posts like this however they normally explain theusermod
should not be used as it changes the primary group of a user. These posts don't seem to take into account the-aG
switch which just appends the group.Is this still poor practice or no different than using
gpasswd -a
?-
Michael Hampton about 9 yearsDid you happen to look at the date of that message?
-
ZZ9 about 9 yearsYeah but theres tons like it and someone recently said something along the lines of it to me so I thought best to ask.
-
Mr-Programs over 5 yearsI want to add to your question and put adduser command, ive seen it here scontent-mad1-1.xx.fbcdn.net/v/t1.0-9/…
-