When we create a new user on a Linux operating system, at that time user creation some set of privileges are granted to each user. These user rights include some set of permission, such as Read, Write, and Execute. Therefore, based on the assigned privileges user can perform some set of functions or tasks on a system. Sometimes, multiple users may have the same privileges on an operating system. In this situation, the groups can be used on the Linux system. Linux group is defined as an organization of user accounts that have the same privileges. So, the main purpose of the group is to create and assign a set of privileges to a group instead of granting permission to individual users. Add relevant users to a particular group and grant privileges to a whole group. In this article, we will study in detail how to add a user to a specific group in Debian.
Linux Groups
In the Linux operating system, we deal with two types of groups:
- The primary group
- Secondary group
The primary group user’s information is stored in the /etc/passwd file. It is important to note that a user belongs to one or zero primary groups, whereas, he can belong to more than one secondary group. Only the administrator or root with ‘sudo’ access user can add users to a particular group.
Add a user to a group
First, you need to open the terminal on your system. Therefore, click on ‘Activities’ and search the terminal in the search bar. Click on the terminal from the following displayed results.
To add an existing user in a single group, use the following command the terminal:
$ sudo usermod -a -G Group-name User-name
Example:
For example, you want to add a user named ‘kbuzdar’ in a ‘debian10’ group. Then, replace the Group-name with debian10 and User-name with kbuzdar in the above command as follows:
$ sudo usermod -a -G debian10 kbuzdar
Add a user into multiple groups
You can easily add one user to multiple groups. To do this, use the following command syntax:
$ sudo usermod -a -G Group1, Group2 User-name
Example:
For example, you want to add a user named ‘kbuzdar’ in multiple groups ‘bluetooth, sudo, debial10’. Then, the above command will change into the following shape:
$ sudo usermod -a -G bluetooth, sudo, debian10 kbuzdar
You can also create a user and add him to multiple groups by using the following command:
$ sudo useradd -g group -G Group1,Group2 User-name
For example, we have created a user named ‘karim_buzdar’ and added into three different groups debian10 and bluetooth, sudo. You can add users into primary and secondary groups respectively.
$ sudo useradd -g debian10 -G sudo, bluetooth karim_buzdar
Create and delete a group
You can easily create a new group by using the following command:
$ sudo groupadd Group-name
For example, you want to create a new group with the name ‘docker’. Then, you will replace the ‘Group-name’ with ‘docker’ in the above command as follows:
$ sudo groupadd docker
Similarly, using the following command you can remove or delete a group by using the following command:
$ sudo groupdel Group-name
Replace the Group-name with docker, if you want to remove this group from your system.
Remove user from group
You can also remove a user from the user’s group by using the following command:
$ sudo gpasswd –d User-name Group-name
For example, we want to remove the user named ‘kbuzdar’ from the group name ‘bluetooth’. For this purpose, use the command as follows:
Display user group information
You can display the complete details about the user with the group to which the user belongs by using the following command:
$ id User-name
For example, we want to know the complete information of a user named ‘karim_buzdar’. The command will be changed into the following shape:
$ id karim_buzdar
You can see the highlighted information of a user’s group.
If you want to display only user’s group then, using the group command you can do this.
$ groups karim_buzdar
If you will forget to enter the user name with the group command then it will display the current user’s group details.