Ubuntu

How to Add a User Group in Ubuntu 22.04

In Linux, groups are the objects or entities that are used to manage and organize a large number of user accounts at once. The user account administration is simple and quick with groups. Many distinct tasks and functions are assigned to various Linux users. The key element of groups is the identification of a set of rights that can be shared by all group members, such as the access to a resource for writing, reading, or executing.

Two types of groups exist in Ubuntu and other Linux distributions: primary and secondary groups. The primary group is like a user name. The user’s primary group information is stored in a file located at “/etc/passwd” in your Ubuntu system. The secondary groups are important for allowing the group members’ access to specific files. They are intended to handle both the individual files and software applications. The members of a group inherit the group’s write, read, and execute rights.

On a Linux system, each user is a part of one primary group and may also be a part of one or more secondary groups. Only the root or users with the sudo privilege can add the users as group members.

We’ll talk about in this guide how many types of groups are available in the Linux distribution.  We will also go into further detail on how to add a user to the group in Ubuntu 22.04 LTS Jammy Jellyfish Linux distribution. 

Adding a New Group in Ubuntu 22.04

You can create or add a new group to your Ubuntu 22.04 system using the “addgroup” command. In the following example, we add a new group “mytest_group” in our system:

$ sudo addgroup mytest_group

Adding an Existing User to the Sudoer Group

You can either create a new user in your Ubuntu system or you can add an existing user to the sudoers group. You can use the following command to add an existing user to a sudoer group:

$ sudo usermod -aG sudo samreena

In the previous command, we added an existing user “samreena” to the sudo group. The “-a” option adds an existing user to the group without removing the current group.

Adding an Existing User to the Multiple Ubuntu Groups

To add an existing user to the multiple groups, you can use the “usermod” command along with “-aG” option and then enter the group names separated by commas.

$ sudo usermod -a -G first_group, second_group username

In the following example, we add an existing user “samreena” to the multiple groups such as sudo, office, and remote_users.

$ sudo usermod -aG sudo, office, remote_users samreena

Adding a New User and Add to Multiple Groups in Ubuntu

To add a new user to your Ubuntu 22.04, you can  use the “adduser” command:

$ sudo adduser samreena

Now, you can add this user “samreena” to multiple groups. You can add a new user to multiple groups by using this syntax:

$ sudo useradd -g group -G first_group,second_group username

The previous command adds a new user using the “useradd” command. And then add this user to multiple groups. This command adds a user to the primary and secondary groups.

For example, we add a new user “samrina”, and add that user to multiple groups. Here, the primary group is “office” and sudo, and the secondary groups is remote_users.

$ sudo useradd -g office -G sudo, remote_users samrina

How to Change the User’s Primary Group in Ubuntu 22.04

We can change the user’s primary group using the “usermod” command with the “-g” option. Use this syntax:

$ sudo usermod -g groupname username

For example, we want to change the user’s primary group to “remote” using this command:

$ sudo usermod -g remote_users samreena

How to Display the User Groups Details in Ubuntu 22.04

You can easily view all the user’s group information in Ubuntu 22.04. For example, if you want to display the information of the user belonging to which group, you can use the id command and type the username in the following way:

$ id username

We display the group details of user “samreena” in the following:

$ id samreena

As you can see in the previous output, the user belongs to multiple groups. Here, the user’s primary group is “remote_users” and is also belonging to other supplementary groups, as shown in the previous screenshot.

If you want to view all the supplementary groups of a user, use the “group” command.

$ groups samreena

Provide the username with the “groups” command. If you will not provide it, in this case, it displays the currently logged-in user’s group details:

$ groups

Display the Ubuntu Groups

Different groups run on an Ubuntu system. Therefore, if you want to display all the Ubuntu groups, you can try the “getent” command:

$ getent group

The previous command shows all the Ubuntu group information. Moreover, it displays the details about the users belonging to which group.

How to Delete a Group in Ubuntu 22.04

You can also remove the unwanted groups from your Ubuntu system. Use the “groupdel” command with the group name that you want to remove.

$ sudo groupdel groupname

How to Remove an Existing User from Ubuntu Group

To remove an existing user from a group, use the “gpasswd” command:

$ sudo gpasswd -d username groupname

For example, here, we want to remove the “samrena” from the “remote” user’s group.

$ sudo gpasswd -d samreena remote_users

Conclusion

We elaborated in this tutorial on how to add a user to a group in Ubuntu 22.04 Jammy Jellyfish distribution. Furthermore, we demonstrated how we can manage the Ubuntu system accounts and users using the groups. You can also execute the given guidelines and commands on the other Ubuntu distributions.

Similar Posts