How to Add a User to a Group in Linux (2023 Guide)

1. Log in to your Linux system as a user with sudo or root privileges.

2. Open a terminal window.

3. Type the following command to list all the groups on your system:

$ cat /etc/group

4. To add a user to a group, use the usermod command. For example, to add the user “john” to the “developers” group, type the following command:

$ usermod -a -G developers john

5. To verify that the user has been added to the group, type the following command:

$ groups john

This will list all the groups that the user “john” belongs to.

6. To remove a user from a group, use the gpasswd command. For example, to remove the user “john” from the “developers” group, type the following command:

$ gpasswd -d john developers

7. To verify that the user has been removed from the group, type the following command:

$ groups john

This will list all the groups that the user “john” belongs to.

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.

Adding Users to a Group in Linux (2023)

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:

Options Description
-d The new user will be created using a new directory name as the value for the user’s login directory
-e This is used to specify the date on which the user account will get deactivated
-u This 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

Manage Users and Groups in Linux Terminal

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.

How to Add a User to a Group in Linux (2023 Guide)

Adding a user to a group in Linux is a simple process that can be done in a few steps. In this guide, we will show you how to add a user to a group in Linux in 2023.

Step 1: Check the Group Name

The first step is to check the group name. You can do this by running the command cat /etc/group. This will list all the groups on your system. Look for the group you want to add the user to and make a note of the group name.

Step 2: Add the User to the Group

Once you have the group name, you can add the user to the group by running the command usermod -a -G groupname username. Replace groupname with the name of the group you want to add the user to and username with the username of the user you want to add.

Step 3: Verify the User is Added to the Group

Once you have added the user to the group, you can verify that the user is added to the group by running the command groups username. This will list all the groups the user is a member of. If the group you added the user to is listed, then the user has been successfully added to the group.

Conclusion

In this guide, we showed you how to add a user to a group in Linux in 2023. We showed you how to check the group name, add the user to the group, and verify that the user is added to the group. We hope you found this guide helpful.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

Contact
San Vito Al Tagliamento 33078
Pordenone Italy
Item added to cart.
0 items - 0.00
Open chat
Scan the code
Hello 👋
Can we help you?