In this tutorial, we learn how to add a user to a group in Linux. We also check how to create a group, add to a user to a secondary group, change user primary group, remove group and list groups.
There are two types of groups in Linux.
When a user is created in Linux it creates a primary group with same name and stored in /etc/passwd file. When a user creates a file, then the file's associated group will be the user's primary group.
Secondary group (supplementary group)
Secondary Group is any Group(s) you are a member of other than your Primary Group. It useful for file sharing then the file will be granted permissions to a set of users who are members of the group.
Remember that you need root or sudo user privilege to add a user to a group.
Add an Existing User to a Group (Secondary group)
We use usermod command to add an existing user to an existing group.
$ sudo usermod –a -G groupname username
Let's add the user 'tom' to the group 'developers' as following:
$ sudo usermod –a -G developers tom
Note: If you run without
usermod -G developers tom, will add the user 'tom' to the group 'developers' and will remove user from any other groups where currently a member.
Remove a user from a group
Gpasswd command is used to remove a user from a group using
$ sudo gpasswd -d username groupname
Lets remove the user 'tom' from the group 'developers' using the following command:
$ sudo gpasswd -d tom developers
Create a new Group
A new group can be created using groupadd command. Check the below syntax:
$ sudo groupadd groupname
The following command will create a new group named 'developers'.
$ sudo groupadd developers
Change a User’s Primary Group
A user's primary group can be changed with
-g option in usermod command. Before changing make sure the group exist on the system.
$ sudo usermod -g groupname username
Here we are changing user 'tom' to a another primary group called 'tomsgroup'.
$ sudo usermod -g tomsgroup tom
Add a User to Multiple Groups
A user can also be added to a multiple of groups using a single command.
$ sudo usermod –a -G groupname1,groupname2,groupname3 username
Add primary group to new user
Here we are creating a new user and assign a primary group to that user using single command.
$ sudo useradd -g groupname username
This is the command which creates a user and assigns a group to him. Here the group to which user has been assigned is primary group
The following command create a user 'tom' and assign him to a primary group named 'tomsgroup'
$ sudo useradd -g tomsgroup tom
Add secondary Group to new user
Here we are creating a new user and assign a secondary group to the user.
$ sudo useradd -G groupname username
So as we have realized here
g is used for assigning primary group and
G is used for assigning secondary group.
All Group(s) are found in the '/etc/group' file and that are created with the groupadd command. We can also display user’s groups using groups and id commands.
If a username is provided to this command, then that user’s groups will be listed.
$ groups raghu
raghu : raghu adm cdrom sudo dip plugdev lpadmin sambashare
By default it will print the currently logged in users groups.
Id command will display a user’s primary and secondary groups.
$ id raghu
uid=1000(raghu) gid=1000(raghu) groups=1000(raghu),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),109(lpadmin),124(sambashare)
In this tutorial, we have discussed how to add a user to a group. All these commands work on most Linux distributions like Ubuntu, CentOS, Mint and Debian. Thanks for reading the article and if you have any questions or feedback, feel free to leave a comment.