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:
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 –
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:
The new user will be created using a new directory name as the value for the user’s login directory
This is used to specify the date on which the user account will get deactivated
This will create a new user with a specific user-id
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.
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>
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>
-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.
Add User to Multiple Groups
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>,
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>
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:
<user_name> is left blank, then it will show the group names for the current user only.
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.
Delete a Group in Linux
To delete any secondary group in Linux, you can use the
sudo groupdel <groupname>
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 flags. The
-g flag is used for primary groups, whereas the
-G flag is used for secondary groups.
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
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.