How to Add a User to a Group in Linux (2023 Guide).
Assume you’re a system administrator of a giant challenge, and also you typically obtain complaints from the QA crew that the event crew is messing up the codebase and vice versa. Upon shut inspection, you discover out that two teams are part of the identical group, sharing the identical codebase. So you ask your senior to assist with the issue, and he advises you to make two teams for all of the customers of the event and QA teams. But, you don’t have any clue how so as to add customers to a gaggle in Linux. Well, we’re right here to assist remedy this drawback. In this text, now we have defined how to create a brand new group, create new customers, after which add a person to a gaggle in Linux.
Adding Users to a Group in Linux (2023)
Before we exhibit how to create teams, create customers, and add new or current customers to those teams, we first have to study what’s a person group in Linux. So let’s have a look at what are person teams after which transfer on to including customers to a gaggle.
What is a User Group in Linux
Generally, a company is split into teams, with every crew having a unique operate and each member of the group is a part of a crew. Similarly, in a multi-user system, each person is a part of a gaggle having a unique set of privileges like studying, writing, or executing permissions for a shared useful resource inside the group. There are two varieties of teams in Linux:
Whenever you log in to a session, you might be a part of the first group. Generally, the first group bears the identical title because the username in Linux. Any file or course of created by you is a part of this group and can’t be accessed by customers of different teams by default. Information reminiscent of person id, group id, and many others. a couple of main group is saved within the file –
/and many others/passwd.
Secondary teams or supplementary teams are helpful when that you must share entry to any information or processes with a specific group of customers. Only the basis person or customers with sudo privileges can assign new permissions or add new customers to the secondary teams.
A person can solely be a part of one main group however might be a part of a number of secondary teams. Now that you realize the fundamentals of person teams in Linux, allow us to now see how to create new customers and add them to a gaggle.
How to Add New Users in Linux
To create new customers, use the
useradd command as per the next syntax:
sudo useradd <choices> username
Some of the choices to pair up with the command are:
||The new person might be created using a brand new listing title as the worth for the person’s login listing|
||This is used to specify the date on which the person account will get deactivated|
||This will create a brand new person with a selected user-id|
The new person you create will solely be part of the first group initially. Once you create a brand new person, assign them a brand new password using the
passwd command, as proven under:
sudo passwd <username>
Here, you’ll be prompted to enter the brand new password after which requested to re-enter the password for affirmation. At this level, the person can now log in to the system with the brand new username and password you simply created.
How to Create a User Group in Linux
Now that you’ve created a brand new person, you can begin creating teams and including customers to them. To create a brand new group, use the
groupadd command. Be positive to present a novel title to the brand new group title, or it is going to conflict with different current group names. Here’s what the syntax appears like:
sudo groupadd <group_name>
How to Add Users to Groups
Once you might have created a gaggle, now you can begin including customers to it. Only a root person or a person with sudo entry can add customers to completely different teams.
Add Existing User to An Existing Group
To add an current person to a gaggle, use the
usermod command whose syntax now we have highlighted under:
sudo usermod -a <user_name> -G <group_name>
-a choice stands for append (add on the finish) and it provides the person to an current group and the
-G flag is used to specify the title of the group to which the person is being appended. There is not any choice for verbose output with this command, besides it offers a warning if the person or the group doesn’t exist.
Add User to Multiple Groups
usermod command in Linux will also be used so as to add a person to a number of teams with basically the identical choices as a single group, as proven under. Be positive to specify the group names with none areas.
sudo usermod -a <user_name> -G <group_name_1>,
Create a New User and Assign a Group
You can add a brand new person to a gaggle all with a single command om Linux. The
useradd command helps you to create a brand new person after which add a person to the desired teams. The syntax so as to add a brand new person 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 of the teams related to a person, you should use both the
teams command or the
id command. Check you the syntax proven under:
<user_name> is left clean, then it is going to present the group names for the present person solely.
How to Remove a User from a Group
To take away a person from a gaggle in Linux distros, you should use the
gpasswd command, as per the syntax given under:
sudo gpasswd -d <user_name> <group_name>
In addition to eradicating a person from a gaggle, the
gpasswd command can also be used for varied administrative duties reminiscent of defining group directors or setting a gaggle password, and many others.
Delete a Group in Linux
To delete any secondary group in Linux, you should use the
sudo groupdel <groupname>
How to Change a User’s Primary Group
All the instructions now we have mentioned until now apply to the secondary group and their customers. To change a person’s main group in Linux, use the next syntax:
sudo usermod –g <new_primary_group> <user_name>
You should be questioning what’s the distinction between the
-G flags. The
-g flag is used for main teams, whereas the
-G flag is used for secondary teams.
How to List All User Groups in Linux
A person which will get created on the time of set up is called a system person and is a part of many default teams in Linux. The details about each group within the system is saved within the file – /and many others/group. To checklist all teams within the system, use the next syntax:
sudo cat /and many others/group
Manage Users and Groups in Linux Terminal
Managing teams is a helpful activity that would turn out to be useful throughout varied operations, particularly in case you are a system administrator. Here, now we have proven you some straightforward Linux Terminal instructions to create a brand new person, create new teams, add a brand new person to a gaggle, and more. So, we hope this text solves your issues. If it doesn’t, tell us within the feedback under, and we’ll enable you to out.
Check out more article on – How-To tutorial and latest highlights on – Technical News