How to Add a User to a Group in Linux

Assume you are a system administrator of a big project, and you often receive complaints from the QA team that the development team is messing up the codebase and vice versa. Upon close inspection, you find out that two teams are a part of the same group, sharing the same codebase. So you ask your senior to help with the problem, and he advises you to make two groups for all the users of the development and QA teams. But, you have no clue how to add users to a group in Linux. Well, we are here to help solve this problem. In this article, we have explained how to create a new group, create new users, and then add a user to a group in Linux.

Before we demonstrate how to create groups, create users, and add new or existing users to these groups, we first need to learn about what is a user group in Linux. So let’s look at what are user groups and then move on to adding users to a group.

What is a User Group in Linux

Generally, an organization is divided into teams, with each team having a different function and every member of the organization is part of a team. Similarly, in a multi-user system, every user is part of a group having a different set of privileges like reading, writing, or executing permissions for a shared resource within the group. There are two types of groups in Linux:

Primary Group

Whenever you log in to a session, you are part of the primary group. Generally, the primary group bears the same name as the username in Linux. Any file or process created by you is part of this group and cannot be accessed by users of other groups by default. Information such as user id, group id, etc. about a primary group is stored in the file – /etc/passwd.

Secondary Group

Secondary groups or supplementary groups are useful when you need to share access to any files or processes with a particular group of users. Only the root user or users with sudo privileges can assign new permissions or add new users to the secondary groups.

A user can only be part of one primary group but can be part of multiple secondary groups. Now that you know the basics of user groups in Linux, let us now see how to create new users and add them to a group.

How to Add New Users in Linux

To create new users, use the useradd command as per the following syntax:

sudo useradd <options> username

Some of the options to pair up with the command are:

OptionsDescription
-dThe new user will be created using a new directory name as the value for the user’s login directory
-eThis is used to specify the date on which the user account will get deactivated
-uThis will create a new user with a specific user-id
creating new user with useradd command

The new user you create will only be a part of the primary group initially. Once you create a new user, assign them a new password using the passwd command, as shown below:

sudo passwd <username>

Here, you will be prompted to enter the new password and then asked to re-enter the password for confirmation. At this point, the user can now log in to the system with the new username and password you just created.

creating new password for the new user

How to Create a User Group in Linux

Now that you have created a new user, you can start creating groups and adding users to them. To create a new group, use the groupadd command. Be sure to give a unique name to the new group name, or it will clash with other existing group names. Here’s what the syntax looks like:

sudo groupadd <group_name>

creating new group

How to Add Users to Groups

Once you have created a group, you can now start adding users to it. Only a root user or a user with sudo access can add users to different groups.

Add Existing User to An Existing Group

To add an existing user to a group, use the usermod command whose syntax we have highlighted below:

sudo usermod -a <user_name> -G <group_name>

Here, the -a option stands for append (add at the end) and it adds the user to an existing group and the -G flag is used to specify the name of the group to which the user is being appended. There is no option for verbose output with this command, except it gives a warning if the user or the group does not exist.

adding new user to group

Add User to Multiple Groups

The usermod command in Linux can also be used to add a user to multiple groups with essentially the same options as a single group, as shown below. Be sure to specify the group names without any spaces.

sudo usermod -a <user_name> -G <group_name_1>,<group_name_2>,<group_name_3>

Create a New User and Assign a Group

You can add a new user to a group all with a single command om Linux. The useradd command lets you create a new user and then add a user to the specified groups. The syntax to add a new user using the useradd command is:

sudo useradd <username> -G <group_name_1>,<group_name_2>,<group_name_3>

creating new user and adding to groups in single command

View a User’s Groups

To view all the groups associated with a user, you can use either the groups command or the id command. Check you the syntax shown below:

groups <user_name>

id <user_name>

If the <user_name> is left blank, then it will show the group names for the current user only.

listing the group of the user test1

How to Remove a User from a Group

To remove a user from a group in Linux distros, you can use the gpasswd command, as per the syntax given below:

sudo gpasswd -d <user_name> <group_name>

In addition to removing a user from a group, the gpasswd command is also used for various administrative tasks such as defining group administrators or setting a group password, etc.

Removing user test from the group sudo

Delete a Group in Linux

To delete any secondary group in Linux, you can use the groupdel command:

sudo groupdel <groupname>

Delete a group in Linux

How to Change a User’s Primary Group

All the commands we have discussed till now apply to the secondary group and their users. To change a user’s primary group in Linux, use the following syntax:

sudo usermod –g <new_primary_group> <user_name>

You must be wondering what’s the difference between the -g and -G flags. The -g flag is used for primary groups, whereas the -G flag is used for secondary groups.

Changing the primary group for the user "test"

How to List All User Groups in Linux

A user which gets created at the time of installation is known as a system user and is part of many default groups in Linux. The information about every group in the system is stored in the file – /etc/group. To list all groups in the system, use the following syntax:

sudo cat /etc/group

Listing all user groups in the system

Managing groups is a useful task that could come in handy during various operations, especially if you are a system administrator. Here, we have shown you some easy Linux Terminal commands to create a new user, create new groups, add a new user to a group, and more. So, we hope this article solves your problems. If it doesn’t, let us know in the comments below, and we will help you out.

#Tags
comment Comments 1
Leave a Reply

Loading comments...