In Linux, groups are objects that are used to organize and manage multiple user accounts at the same time. Groups offer easy and fast administration of user accounts. Different Linux users have many different tasks and roles. The key to groups is identifying a set of rights, such as write, read, or execute access to a specified resource, that can be shared by group users.
In this guide, we will discuss how many types of groups there are on an Ubuntu Linux system. In addition, we’ll also take a closer look at how to add an Ubuntu user to groups.
Groups in Ubuntu Linux
Ubuntu and many other Linux distributions have two different types of groups:
- Primary group
- Secondary group
As soon as the user creates a file, it is automatically added to the main group. The main group is like a username. All primary group data about users is stored in a file located in ‘/ etc / passwd’.
Secondary groups are important for granting access to specific files to group members. They are designed to administer individual files along with software applications. Group members inherit write, read, and execute permissions for that group.
Every Linux user is a member of one primary group, and in addition, this user can be included in one or more secondary groups. Users can be added as group members simply by root or by users with privileges sudo…
Create a group in Ubuntu
You can create a new group on an Ubuntu system by running the following command:
$ sudo groupadd test_group
In the above command, test_group is the name of the new group.
Adding an existing user to a group in Ubuntu
To add a user that is already created on your Ubuntu machine and you want to assign a group to that existing user, run the following command:
$ sudo usermod -a -G groupname username
For example, “sam-pc” exists on our system, and we want to add this user to the “test_group” group. To do this, change the above command to the following form:
$ sudo usermod -a -G test_group sam-pc
When you execute the above ‘usermod’ command, it does not show any output on the terminal screen when the command is successful. This means that the user has been successfully added to the group. When you add a user to a group, you need to add the “-a” parameter. If you do not add the -a option, the user will be removed or excluded from any groups not listed after the -G option.
Create a new user and add it to multiple groups in Ubuntu
If you want to create a new user and add him to a group with a single command, use the useradd command to add a new user, and then add that user to the primary and secondary groups using the following syntax:
$ sudo useradd -g group -G group1, group2 имя пользователя
In the following example, we have created a new user named Andreyex and assigned several groups. The primary group is “users” and the secondary groups are “sudo, sam-pc “.
$ sudo useradd -g users -G sudo,sam-pc Andreyex
Add existing Ubuntu user to multiple groups
To add an existing user to multiple groups on an Ubuntu system, you can use the usermod command followed by the -G option, enter the name of the groups to be separated by commas as shown below:
$ sudo usermod -a -G first_group, second_group username
Let’s take an example to add an existing user Andreyex to multiple groups like sam-pc. sudo, sambashare, run the following command:
$ sudo usermod -a -G sam-pc, sudo, sambashare Andreyex
Change primary user group in Ubuntu
By using the `usermode` command followed by the` -g` parameter, you can change the primary group of a user.
$ sudo usermod -g groupname username
In the following example, we have changed the primary group of the user Andreyex to test_group by running the below command:
$ sudo usermod -g test_group Andreyex
Displaying information about user groups in Ubuntu
You can display complete information about a user’s group, such as the user belonging to which group. To accomplish this task, use the following id command followed by the username:
$ id username
For example, we want to display all the group information of the user sam-pc.
$ id Andreyex
As you can see from the above output, the user belongs to different groups. Here, the primary group of a user is “users” as well as a member of other additional groups such as “sudo, sam-pc and sambashare ”, which are shown in the screenshot above.
When you use the ‘group’ command followed by the username, additional user groups are displayed.
$ groups sam-pc
If you do not pass the username to the ‘group’ command, then it will display the group information of the currently logged in user as follows:
Show all Ubuntu system groups
To view all system groups running on your Ubuntu system, use the following getent command:
$ getent group
The above command displays all system groups on the terminal screen and also shows which group the user account belongs to.
Removing an existing user from a group in Ubuntu
To remove or remove a user from a group, run the “gpasswd” command on a terminal, followed by the “-d” option.
$ sudo gpasswd -d username groupname
For example, we want to remove user Andreyex from sambashare group, change the above command to the following form to complete this task:
$ sudo gpasswd -d Andreyex sambashare
Delete group in Ubuntu
Use the following groupdel command along with groupname to remove an existing user group.
$ sudo groupdel groupname
In this tutorial, we have demonstrated how to add a user to a group on Ubuntu 20.04. We have seen how groups help us manage all system accounts and users. We have implemented various commands to better understand users and groups. The above command can run on other Linux distributions as well. Please let us know if you have any misunderstandings related to this article. Thanks!