How to Create Group and Add User in Ubuntu? [2 Cases]

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

Process flow chart of creating a group and adding users in Ubuntu.

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 -
Explanation
  • sudo: Grants root privileges.
  • su –: Switches to the root user.

Switching to the root user.➌ 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.

I am adding a new group named "newgrp" in Ubuntu using the addgroup command.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
Explanation
  • addgroup: Creates/Manages a group.
  • sowad: User to be added to the newly created group.
  • newgrp: Name of the new group.

I am adding an existing user to the newly created group.In the above image, you can see that I have successfully added the desired user to the new group.



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 -
Explanation
  • sudo: Grants root privileges.
  • su –: Switches to the root user.

Switching to the root user.➌ After that run the following command to create a new group.

addgroup mygrp
Explanation
  • addgroup: Creates/Manages a new group.
  • mygrp: Name of the new group.

I am adding a new group named "mygrp" in Ubuntu using the addgroup command.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
Explanation
  • 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
Explanation
  • passwdChanges password.
  • elon: Name of the user.

I am creating a new user with the useradd command in Ubuntu and adding it to the newly created group.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.



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
Explanation
  • getent: Looks into the administrative database that is given as its argument.
  • group: Contains group information.
  • mygrp: Name of the group.

➌ Press ENTER.Verifying the members of the new group.In the above image, you can see that the newly created group “mygrp” with its members actually exists on the system.

Output Analysis

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
Explanation
  • 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.Adding multiple users to a group in Ubuntu.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
Explanation
  • 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.Removing a specific user from a group.In the above image, you can see that I have successfully removed the user named “user2” from the group named “mygroup”.



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
Explanation
  • sudo: Grants administrative privileges.
  • groupdel: Deletes a group.
  • mygroup: Name of the group.

➌ Press ENTER.Deleting a group in Ubuntu using the groupdel command.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?

In a multi-user system, all the users might not enjoy the same privileges over the system. In some instances, a few users might share some content among themselves in the system. This concept of sharing is easily maintained in Linux-based systems by the notion of Group.

What is the difference between primary and secondary groups in Linux?

The primary group is the main group of a user of Linux. By default, while creating a new user, it is assigned to the primary group. On the other hand, a user can belong to one or many more groups other than the primary group, these are called secondary groups.

How to create a group in Ubuntu?

You can easily create a group in Ubuntu by using any of the following two commands the addgroup command or the groupadd command.

How to add a user in Ubuntu?

You can add a new user in Ubuntu from both CLI (Command line Interface) & GUI (Graphical User Interface). Moreover, you can use two different commands i.e. the adduser command and the useradd command to create a user in Ubuntu.

How do I add a user to a group?

You can add a user to a group by using the usermod command however you might need to use some options to meet your need.

How to add a user to the root group in Ubuntu?

If you are a root user or have sudo privileges, you can add a user to the root group by using the usermod command with the -aG option. For example, to add a user named jhon the command will be “sudo usermod -aG sudo jhon”.

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.

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.

What is UID & GID in Ubuntu?

In Ubuntu, all users and groups are assigned unique numbers. These unique numbers for users are called user identifiers (UIDs) and for groups are called group identifiers (GIDs).

Relaed Articles

Rate this post
Md. Ashakul Islam Sowad

Md. Ashakul Islam Sowad

Hi, I am Md. Ashakul Islam Sowad from Dhaka, Bangladesh. I have completed my undergraduate degree in Biomedical Engineering from the Bangladesh University of Engineering and Technology (BUET). I love to watch football and play video games in my free time. Here, I am working as a Linux Content Developer Executive. Furthermore, as a Linux enthusiast, I am always learning new things about Linux-based systems and I’ll be sharing them here. Read Full Bio

We will be happy to hear your thoughts

Leave a reply

LinuxSimply
Logo