How to Add a User to a Group in Ubuntu 22.04?


In Ubuntu (and other Linux systems), users are associated with different groups to perform a specific role. The groups have a set of defined authorizations on the system to perform. Whenever a user is created, the system administrator integrates that user into specific groups to share the roles/authorizations. Thus, user and group management is considered one of the major activities of an administrator.

Keeping this in view, this post will address various ways to add a user to a group in Ubuntu 22.04.

Types of Groups in Ubuntu

There are two types of groups in Ubuntu to which a user may belong.

  • Primary: When a new user is created on Ubuntu, it is instantly added to a group of the same name, and that group is referred to as the primary group. Thus, a user must have at least one primary group.
  • Secondary: A secondary group is associated with the user to assign/grant a specific set of permissions to the user.

Usually, the user is added to the secondary group (the manual addition carried out after the user’s creation). It is not recommended to change/alter the primary group of the user, as it is created at the time of creating the user and the primary group holds the basic authorizations/roles of the user.

How to Add an Existing User to an Existing Group in Ubuntu 22.04?

The usermod, adduser, and the gpasswd utilities add/append the existing user to the existing group (already present in the system). Each command has its functionality which is described in the respective sections:

 

Method 1: Using the usermod Command

The usermod command modifies the user details, i.e., username, password, shell, home directory, and group. The general syntax of the usermod command is as follows:

Syntax:

$ sudo usermod -a -G <groupname> <username>

The “-a” option appends the <username> in the <groupname>. The -G option represents the user being added to the secondary group.

The following command adds the user “linuxuser” to the group “geniegroup”:

$ sudo usermod -a -G genie linuxuser

Verify the addition using the “id” command:


Method 2: Using the adduser Command

The adduser command allows you to create a user, or a group, and also add a user to a group as well. The syntax of the command to add a user to an existing group is as follows:

Syntax:

$ sudo adduser <username> <groupname>

Let’s add a user “linuxuser” to the group named “linuxgenie”:
$ sudo adduser linuxuser linuxgenie

Note: The user has been added, if you want to verify, you can use the “id username” command.

Method 3: Using the gpasswd Command

The gpasswd is another useful utility to manage the user and the groups. It can be used to add the user to an existing group via the syntax:

Syntax:

$ sudo gpasswd -a <username> <group>

The option “-a” appends the <username> to the specified <group>.

Following the syntax, the command below adds the user “genie” to the group named “linuxgenie”:

$ sudo gpasswd -a genie linuxgenie

Verify the addition:


How to Add a User to Multiple Groups in Ubuntu 22.04?

The “usermod” is the command that adds/appends a single user to multiple groups simultaneously.

Syntax:

$ sudo usermod -a -G <group1>,<group2>,<group3> <username>

The following command adds the user named “linuxuser” to three groups simultaneously, i.e., “genie”, “ubuntu”, and “linux”:
$ sudo usermod -a -G genie,ubuntu,linux linuxuser

The addition of the user is verified using the “id username” command as well.

Note: The usermod command does not add multiple users to a single group.

How to Add Multiple Users to a Group in Ubuntu 22.04?

The gpasswd command with its “-M” option adds multiple users to a single group using the syntax:

Syntax:

$ sudo gpasswd -M <user1>,<user2> <group>

Using the syntax, the command provided below adds two users “genie1” and “genie2” to one group “geniegroup”:
$ sudo gpasswd -M genie1,genie2 geniegroup

How to Add a New User to a Group in Ubuntu 22.04?

Ubuntu supports the “useradd” command that creates the user and adds it to a group instantly. The variants of “useradd” commands are discussed here one by one:

Syntax Add a New User to the Secondary Group Only

$ sudo useradd -G <group> <username>

The command below creates the user “lgenie” and adds it to a group named “linuxgenie”:
$ sudo useradd -G linuxgenie lgenie

Similarly, if you want to add the new user to the primary group and primary/secondary. Follow the below-written syntaxes.

Syntax to Add a New User to the Primary Group Only

$ sudo useradd -g <groupname> <username>

For instance, the command below adds the user “tim” to a primary group “lgenie”:
$ sudo useradd -g lgenie tim

How to Add a User to a Group Using Shell Script?

The “gpasswd” command is used for adding multiple users to a group (the functionality not offered by the “usermod” by default) and the “usermod” command can add a single user to multiple groups (which cannot be carried out using “gpasswd”). Moreover, adding multiple users to multiple groups is also not supported in the general use of both commands.

The above-raised queries are possible but only through the bash scripting functionality. Let’s see how:

Prerequisites: Users/Groups must be Present

While following any of the given methods, ensure that you have the target user(s) and groups(s) available in the system. The following commands can be used:

$ id <username> #To_Check_the_Username
$ getent group | grep <groupname> #To_Check_the_GroupName

Add Multiple Users to a Group Using the usermod Command

The two users “ubuntu1” and “ubuntu2” will be added to a group named “ubuntugroup” by following the below process:

Add the users (ubuntu1 and ubuntu2 ) to a “txt” file on separate lines. A file named “users.txt” contains the users:

Let’s create a shell script using the nano command which will directly fall into the editing mode:


The script’s code is described as:

  • A for loop is used to exercise the variable “users” in the list of users.
  • Upon the successful looping, the “usermod” command will be executed.

#!/bin/bash

for users in `cat users.txt`
do
sudo usermod -a -G ubuntugroup $users
done


Make the script executable using the command:


Execute the script using the command:


Ensure the users have been added to the group via the command:

$ getent group | grep ubuntugroup

Add a User to Multiple Groups Using the gpasswd Command

Here, the user named “genieuser” will be added to three groups “group1”, “group2”, and “group3” simultaneously.

First, create a text file and add the groups in that file on separate lines:

 

Then, create a bash script:

$ sudo nano geniegroups.sh

In our case, the script’s code is defined as:

  • A for loop is used to loop through groups in the file.
  • After each successful loop, the gpasswd command will be executed to add “genieuser” to that specific passed group.

#!/bin/bash

for groups in `cat groups.txt`
do
sudo gpasswd -a genieuser $groups
done


Make the script executable and execute it using the command:

$ sudo chmod a+x geniegroups.sh && ./geniegroups.sh

Note: You can use the “id username” command to verify the addition of the user into the groups.

Adding Multiple Users to Multiple Groups

Three users “genie1”, “genie2”, and “genie3” are used here to be added to three groups “group1”, “group2”, and “group3”. Let’s do it:

Here, two text files are required, i.e., explicitly for users and groups:

Create/Edit a shell script using the command:


The script is described as:

  • Two for loops are used. One to loop over the usernames in “musers.txt” and the other to loop over “mgroups.txt”.
  • The second loop is executed if the first loop is successful. After that, the interpreter reads the “usermod” command and adds the user to all the groups one by one. The looping continues until all the users are added.

#!/bin/sh

for users in `cat musers.txt`
do
for groups in `cat mgroups.txt`
do
sudo usermod -a -G $groups $users
done
done


Use the below command to make the script executable and initiate the execution:

$ sudo chmod a+x genie.sh && ./genie.sh

Note: The user’s addition to all these groups can be verified using the “id <username>” command.

Alternative to the Shell Scripting

The users can execute the for loop in the terminal directly as well. The syntax of the for loop to be executed in the terminal is as follows:

$ for users in {user1,user2,user3}; do for groups in {group1,group2,group3}; do sudo usermod -a -G $groups $users; done done

How to Manually Add a User to a Group in Ubuntu 22.04? Through /etc/group

In Ubuntu, the group’s information is stored in the file at location “/etc/group”. The administrators can add the user to the groups by editing the file.

The groups’ related information is stored in the file “/etc/group”. First, open the “/etc/group” file in an editable mode (using any editor, nano, vi, or gedit):


Trace the group in which you want to add the user. Add the user after the other usernames and ensure to separate it with a comma (,), as we did here to add “genie1”, “adnan” and “linuxuser”.

Here, the information related to groups is mentioned in the file where the first entry represents the group name, and the second shows the password is encrypted in “/etc/shadow”. The numeric entry shows the group ID. After the numeric entry, the usernames are listed that belong to the group.

Note: This method must be at the least priority as a single mistake may lead to the malfunction of the “/etc/group” file.

How to Delete/Remove a User From a Group in Ubuntu 22.04?

The “deluser” and the “gpasswd” commands delete the user from the specific/targeted group. Here’s their usage:

Method 1: Using the deluser Command

The syntax of the “deluser” command to delete a user from a group is:

$ sudo deluser <username> <group>

Let’s say, the user “genieuser” is to be deleted from the group named “linux”:
$ sudo deluser linuxuser genie

Method 2: Using the gpasswd Command

The gpasswd command is used with the “-d or –delete” flag alongside the username to delete the user from the group:

$ sudo gpasswd -d <username> <group>

The following gpasswd command will delete the user “genieuser” from the group named “geniegroup” via the command:
$ sudo gpasswd -d genieuser geniegroup

Note: You can use the “id username” command to verify the removal.

How to Delete a Group From Ubuntu 22.04?

A group contains multiple users. The groupdel command does not allow deletion of a primary group (if it is associated with the user). Thus, it is recommended to first check that the group does not have a user of the same name (as it refers to the primary group).


Use the below command to delete the specific group from Ubuntu:
$ sudo groupdel geniegroup

Troubleshooting Tip: How to Add a User to a sudo Group in Ubuntu 22.04?

Have you ever encountered the error “The user is not in the sudoers list: This error will be reported”? This error occurs when you try to run the sudo with a user that does not have the sudo rights.

For instance, the user named “ubuntu” does not have the sudo rights and it throws the error at the time of using sudo:

Let’s see how it can be fixed:

Login as a root user and add the user to the sudo group, using the syntax:

# usermod -aG sudo <username>

Let’s say the username is “genie”:

Now, open the “/etc/sudoers” file using any terminal-based editor and append the following line at the end of the file:

<username> ALL=(ALL) NOPASSWD:ALL

Now, use any sudo command to verify whether the user has been added to the group or not:

Bottom Line

A user on Ubuntu 22.04 is added to groups using the commands: adduser, usermod, gpasswd, and useradd. The adduser, usermod, and gpasswd commands are applicable only if the user and group both already exist. Whereas the useradd command allows you to add a new user to the existing groups, i.e., either primary or secondary. Each command has its specific functionality while adding a user to a group.

Above all, if you want to add multiple users to multiple groups at one time, you have to exercise shell scripting or the four loop in the terminal.

All in all, you have learned how a user is added to a group in Ubuntu 22.04.

 

Print Friendly, PDF & Email
Categories