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 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:

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

[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 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. Now to do the same follow the below procedures:

  1. At first, press CTRL+ALT+T to open Ubuntu terminal.
  2. 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.
  3. 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.

  4. 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.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:

  1. At first, open the Ubuntu terminal.
  2. Then, switch to the root user by executing the following command and providing the root password:
    sudo su -

    Switching to the root user.

  3. Create a new group by running the following command:
    addgroup mygrp

    Here mygrp is the 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.

  4. 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.
  5. Set a password for the newly created user by executing the following command:
    passwd elon

    Here the passwd command sets/changes the password.

    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.



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:

  1. Open the Ubuntu terminal.
  2. 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.

    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:

  1. Open the Ubuntu terminal.
  2. 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.
  3. 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:

  1. Open the Ubuntu terminal.
  2. 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.

    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, open the terminal and run the following command:

sudo groupdel mygroup

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?

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?

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. Basically, primary groups represent the main affiliations, while secondary groups give you extra access privileges.

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

5/5 - (2 votes)
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

Leave a Comment