In a multi-user system like Ubuntu, to manage security among the users efficiently the notion group is used. To put it simply, a group is a collection of users who enjoy the same privileges over some contents in the system. Therefore, while working on Ubuntu it is important to know about creating groups and adding users to them. In this article, I will discuss how easily you can create group and add user in Ubuntu.
Process flow chart to create group and add user in Ubuntu:
[Distro Used Throughout the Tutorial: Ubuntu 22.04.1 LTS]
2 Cases to Create a Group and Add a User in Ubuntu
For your convenience, I have divided this article into 2 scenarios. In the first case, I will show you how to create a group and add an existing user to that group. In the latter case, I will create a new group as well, however now I’ll create a new user and add it to the new group simultaneously.
Case 01: Create a Group and Add an Existing User in Ubuntu
You can easily create a new group in Ubuntu by using any of the addgroup
or groupadd commands. For instance, I’ll use the addgroup
command to be more user-friendly.
In this section, I’ll create a group named “newgrp” using the addgroup command and then add an existing user named “sowad” to that newly created group. To create a group and add an existing user in ubuntu, follow the below procedures:
- At first, press CTRL+ALT+T to open Ubuntu terminal.
- Then, switch to the root user by executing the following command and providing the root password:
sudo su -
- After that run the following command to create a new group:
addgroup newgrp
EXPLANATION- addgroup: Creates/Manages a new group.
- newgrp: Name of the new group.
In the above image, you can see that I have created the desired “newgrp” and also notice that the addgroup command assigns a GID (Group Identifier) by default.
- Next, execute the below command to add the desired user to the new group:
addgroup sowad newgrp
Here sowad is the user to be added to the newly created group.In the above image, you can see that I have successfully added the desired user to the new group.
Now you can follow this guide to verify the group.
- How to Create a Jenkins User on Ubuntu? [2 Methods]
- How to Create MySQL User in Ubuntu? [2 Cases]
- Create User Without Home Directory in Ubuntu in Just 3 Steps
- How to Create a User in Docker Container Without Docker File?
Case 02: Create a Group and Add a New User in Ubuntu
While creating a new user you can also assign a group to the user by utilizing the useradd
command with the option -G
. However, the group must exist in the system.
In this section first I’ll create a new group named “mygrp” using the addgroup
command and then add a new user using the useradd
command to that new group. To create a group and add a new user in ubuntu, follow the steps below:
- First, open the Ubuntu terminal.
- Then, switch to the root user by executing the following command and providing the root password:
sudo su -
- Create a new group by running the following command:
addgroup mygrp
Here mygrp is the name of the new group.
In the above image, you can see that I have created the desired “mygrp” and also notice that the addgroup command assigns a GID (Group Identifier) by default.
- Now, add a new user and simultaneously add that to the desired new group:
useradd -G mygrp elon
EXPLANATION- useradd: Creates a new user.
- -G: Adds a user to a specific group.
- elon: Name of the user.
- Set a password for the newly created user by executing the following command:
passwd elon
Here the passwd command sets/changes the password.
In the above image, you can see that I have successfully created a new user and assigned it to the desired new group named “mygrp” and set a password for the new user.
Now you can follow this guide to verify the group.
- How to Create an FTP User in Ubuntu? [Step-by-Step]
- Create a New SFTP User in Ubuntu with a New SSH Key
- How to Create a Root User in Ubuntu [Step-by-Step]
- Create FTP User for Specific Directory in Ubuntu [4 Steps]
Check If a Group and its Members Exist in Ubuntu
To check whether your desired new group and its members exist in the system, use the following command syntax. Here, I will check whether the newly created group named “mygrp” and its member “elon” exist on my system.
To check if a group and its memeber exist in ubuntu, follow the given steps:
- Open the Ubuntu terminal.
- Then, type the following command into the command prompt and press ENTER:
getent group mygrp
EXPLANATION- getent: Looks into the administrative database that is given as its argument.
- group: Contains group information.
In the above image, you can see that the newly created group “mygrp” with its members actually exists on the system.
OUTPUT ANALYSISIn the above image, each field in the output separated by colon(:) represents:- mygrp: Name of the group.
- x: Secured password field.
- 1003: GID of the group.
- elon: Member user of the group.
Add Multiple Users to a Group in Ubuntu
To add multiple users to a specific group, you have to use the gpasswd
command and the -M
option in Ubuntu. Here, I will add 3 users named “user1”, “user2” and “user3” to the group named “mygroup”.
To add multiple users to a group in ubuntu, follow the below process:
- Open the Ubuntu terminal.
- Then, type the following command into the command prompt:
sudo gpasswd -M user1,user2,user3 mygroup
EXPLANATION- gpasswd: Manages the /etc/group and /etc/gshadow
- -M: Adds multiple users to the group.
- user1,user2,user3: Name of the users to be added to the group.
- Press ENTER.In the above image, you can see that I have successfully added the three users to the group named “mygroup”.
Remove User from Group in Ubuntu
You can easily remove a specific user from a group by using the gpasswd
command in Ubuntu. Here, I will remove “user2” from the group named “mygroup”.
To do so follow the below process:
- Open the Ubuntu terminal.
- Then, type the following command and press ENTER:
sudo gpasswd -d user2 mygroup
EXPLANATION- -d: Removes a user from the group.
- user2: Name of the user.
In the above image, you can see that I have successfully removed the user named “user2” from the group named “mygroup”.
- How to Create Multiple Users in Linux? [2 Methods]
- Create a User in Ubuntu as Read-Only Access to Log Folder
- How to Create User Account in Ubuntu with Public Key
- Create A User in Ubuntu that Runs a Script and Logs Out [4 Steps]
Delete a Group in Ubuntu
You can easily delete a group in Ubuntu by using the groupdel
command. Here, I will remove the group named “mygroup”.
To do so, open the terminal and run the following command:
sudo groupdel mygroup
In the above image, you can see that I have successfully deleted the group named mygroup.
Conclusion
In this article, I have discussed 2 scenarios to create a group and add users to that group in Ubuntu. In addition to that I have also shared some complementary information that I hope will help you in your experience with Linux.
People Also Ask
What does group signify in Linux?
A group is a group of user accounts in Linux. It simplifies permission management by allowing users to belong to more than one group. File/directory permissions are also set to allow access to certain groups. This improves security and makes it easier to organize users based on their roles or duties in a Linux environment.
What is the difference between primary and secondary groups in Linux?
Primary groups are the main group that’s associated with a user, and they’re set up when you create them. Secondary groups give you extra privileges, so you can belong to more than one group. Primary groups give you the default permissions, and administrators can set up secondary groups by using usermod
commands.
How to create a group in Ubuntu?
You can easily create a group in Ubuntu by using the sudo addgroup <group_name>
command. You can use your preferred name in the <group_name> portion.
How to add a user to the root group in Ubuntu?
To add a user to the root group in Ubuntu, use the sudo usermod -aG sudo <user_name>
command. Replace <user_name> with the actual username of the user you want to add to the root group.
Can I add a user to multiple groups in Ubuntu?
Yes, you can add a user to as many as groups you want to. There are no limitations on the number of groups a user can join in Ubuntu. To add a user to multiple groups in Ubuntu use the sudo usermod -aG group1,group2,group3 <user_name>
command. Replace group1, group2, and group3 with the actual group names, and <user_name> with the target user’s username.
Can I add multiple users to a group in Ubuntu?
Yes, you can add multiple users to the same group in Ubuntu. The various users of the same group will share all of the contents of the group. To add multiple users to a group in Ubuntu use the sudo usermod -aG <group_name> user1 user2 user3
command. Replace <group_name> with the actual name of the group, and user1, user2 and user3 with the usernames you want to add.
What is UID & GID in Ubuntu?
UID is a unique number assigned to each user in Ubuntu, while GID is a number assigned to each group in Ubuntu. They are used to manage users and groups, manage file ownership, and manage permissions in Linux.
Relaed Articles
- How to Create User and Add to Sudo in Ubuntu? [2 Methods]
- 2 Ways to Create a User With Access to a Drive in Ubuntu
- How to Create a New User in Ubuntu by GUI? [Step-by-Step]
- 2 Ways to Create User Account in Ubuntu Using Terminal
- How to Create Home Directory for Existing User in Ubuntu
- Create User with UID and GID in Ubuntu? [3 Scenarios]
- How to Create a Sudo User in Ubuntu? [Step-by-Step]
FUNDAMENTALS A Complete Guide for Beginners