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 enjoys 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.
Key Takeaways
- Creating a new group and a new user.
- Adding a user to the new group.
- Getting familiar with removing users from a group.
- Deleting a group in Ubuntu.
Requirements
To work on the system i.e. add or modify a new group or a user, you need to be a root user or have some sort of sudo/administrative privileges.
Process Flow Chart
Distro Used Throughout the Tutorial: Ubuntu 22.04.1 LTS
2 Cases to Create Group and Add 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. And 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 for being more user-friendly. Then, to add an existing user to the newly created group I’ll also use the addgroup command.
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. Now to do the same follow the below procedures.
Steps to Follow >
➊ 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
- 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
- addgroup: Creates/Manages a group.
- sowad: User to be added to the newly created group.
- newgrp: Name of the new 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 do so follow the below procedures.
Steps to Follow >
➊ 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 -
- sudo: Grants root privileges.
- su –: Switches to the root user.
➌ After that run the following command to create a new group.
addgroup mygrp
- addgroup: Creates/Manages a new group.
- mygrp: 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, run the following command to add a new user and simultaneously add that to the desired new group.
useradd -G mygrp elon
- useradd: Creates a new user.
- -G: Adds a user to a specific group.
- mygrp: Name of the group.
- elon: Name of the user.
➎ After that execute the following command to set a password for the newly created user.
passwd elon
- passwd: Changes password.
- elon: Name of the user.
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]
Complementary Information
Besides, knowing about creating new groups and adding users to them, you will find the below information helpful.
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. You can do so by following the given steps.
Steps to Follow >
➊ Open the Ubuntu terminal.
➋ Then, type the following command into the command prompt.
getent group mygrp
- getent: Looks into the administrative database that is given as its argument.
- group: Contains group information.
- mygrp: Name of the group.
➌ Press ENTER.In the above image, you can see that the newly created group “mygrp” with its members actually exists on the system.
In 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 do so follow the below process.
Steps to Follow >
➊ Open the Ubuntu terminal.
➋ Then, type the following command into the command prompt.
sudo gpasswd -M user1,user2,user3 mygroup
- sudo: Grants administrative privileges.
- 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.
- mygroup: Name of 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.
Steps to Follow >
➊ Open the Ubuntu terminal.
➋ Then, type the following command into the command prompt.
sudo gpasswd -d user2 mygroup
- sudo: Grants administrative privileges.
- gpasswd: Manages the /etc/group and /etc/gshadow
- -d: Removes a user from the group.
- user2: Name of the user.
- mygroup: Name of the group.
➌ Press ENTER.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 follow the below process.
Steps to Follow >
➊ Open the Ubuntu terminal.
➋ Then, type the following command into the command prompt.
sudo groupdel mygroup
- sudo: Grants administrative privileges.
- groupdel: Deletes a group.
- mygroup: Name of the group.
➌ Press ENTER.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?
What is the difference between primary and secondary groups in Linux?
How to create a group in Ubuntu?
How to add a user in Ubuntu?
How do I add a user to a group?
How to add a user to the root group in Ubuntu?
Can I add a user to multiple groups in Ubuntu?
Can I add multiple users to a group in Ubuntu?
What is UID & GID in Ubuntu?
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]