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.

<img alt="Linux Terminal" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/word-image-1034.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="265" src="data:image/svg xml,” width=”797″>

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

<img alt="Add user to group" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/word-image-1035.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="108" loading="lazy" src="data:image/svg xml,” width=”892″>

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

<img alt="Add user to multiple groups at once" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/word-image-1036.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="156" loading="lazy" src="data:image/svg xml,” width=”887″>

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

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/word-image-1037.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="133" loading="lazy" src="data:image/svg xml,” width=”889″>

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

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/word-image-1038.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="90" loading="lazy" src="data:image/svg xml,” width=”892″>

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.

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/word-image-1039.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="108" loading="lazy" src="data:image/svg xml,” width=”893″>

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:

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/word-image-1040.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="169" loading="lazy" src="data:image/svg xml,” width=”889″>

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.

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/word-image-1041.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="149" loading="lazy" src="data:image/svg xml,” width=”893″>

If you want to display only user’s group then, using the group command you can do this.

$ groups karim_buzdar

<img data-ezsrc="https://kirelos.com/wp-content/uploads/2022/11/echo/word-image-1042.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="151" loading="lazy" src="data:image/svg xml,” width=”892″>

If you will forget to enter the user name with the group command then it will display the current user’s group details.