+1 (506) 500-5957 sales@monovm.com

How to Add Users in a Group in Linux

Today we will discuss Linux user groups and how to add users in a group in Linux.

Posted: 20 May, 21 Updated: 05 Mar, 23 by Antoniy Yushkevych 5 Min

List of content you will read in this article:

In Linux, multiple users can use the same system at the same time. In order to manage these users, Linux assists you with a mechanism known as Groups. In simple terms, Groups are a collection of several users. 

This article will help you to achieve a decent grip over the concept of groups and how to implement them.

Prerequisites

  • A system with Linux
  • Terminal window access

There are various users with several distinct roles and responsibilities. There are certain restrictions associated with obtaining access to files and folders. With pre-set permissions, Groups in Linux help you to categorize the users. Instead of granting access to the individual user accounts, you can simply add the user to the group and grant permissions. Easy, right?

Additionally, there are two categories of groups, namely, the primary group and the secondary group. Here is an elaboration regarding both of them.

  • Primary group: A primary group is automatically created when a user with a unique user ID is created. 
  • Secondary group: A secondary group is created by using commands. You can change the group IDs of the users and add them to this group later. 

Read further to understand how to create a group first.

Using the following command:

sudo groupadd newgroup

You can allocate any name to the group you create. 

Moving further, the next up is, how to add a user to the group.

We have bifurcated the topic into sub-parts. Read on.

How to add existing users to the existing group?

First method:

Use the adduser command, here is the syntax:

sudo adduser username newgroup

Second method:

To use the useradd command as shown below:

sudo useradd –G newgroup username

Third method:

Use the usermod command:

sudo usermod –a –G groupname username

There are two options, the -append option and the -group option. They are used by the usermod command to append the users with a group. Without the append option, you may be discarded from other groups. 

How do you create a user and then add it to a group?

Follow this simple command to create a user and immediately add him to an already existing group:

sudo useradd –G groupname newuser

Now, simply add a password by using the following command:

sudo passwd newuser

That’s it! 

How to add a user to various groups at the same time?

You can complete this task with the usermod command. Write the name of various groups in one command as mentioned below:

sudo usermod –a –G newgroup, newgroup1, newgroup2, username

How to change the User’s Primary Group?

Until now you have studied the commands related to the secondary groups. As you have learned above, that the primary group is automatically generated while creating a user. Therefore, the username is the same as the primary group.

If you are hunting for a way to change the primary group, follow this command:

sudo usermod -g newgroup username

Here, -g signifies the primary group. Previously, we used -G, which is used for the secondary groups. Additionally, as the user can only inhibit one primary group, therefore, the initial username that you had set would no longer be primary.

How to remove a user?

Use the gpasswd tool, if you want to eliminate a user from a particular group. This is how you use it:

sudo gpasswd -d username newgroup

Furthermore, you can use the gpasswd tool to perform several administrative operations. For instance, to set a password, to access the group resources or to define group administrators. 

How can you list groups?

Follow this command in order to view a list of groups on your screen:

sudo nano /etc/groups

Moving on, if you want to delete a group, use the below-mentioned command.

How do you delete a group?

sudo groupdel newgroup

If you want to view which group belongs to which user, type this command:

groups username

There is a different approach to achieve the same task too. You can use the user ID and group ID to know the name of the group to which a user belongs!

Use this command:

id username

In case you do not write the name of the username, the command automatically displays the current logged-in user.

Moving on, there are many group names assigned to the groups by default in Linux. Here is a list for you:

adm: The user can keep a check on the Linux system logs.

Ipadmin: The user can easily carry out printer configuration.

cdrom: The optical drive can be easily mounted

wheel: You can easily grant sudo-like privileges.

plugdev: External storage devices can be easily accessed by the user.

This article provided basic knowledge about the Linux groups. We hope that the mentioned commands to add or remove users in a group helped you to sort out your queries.

Keep practicing the commands to build a firm grip over the topic.

Antoniy Yushkevych

Master of word when it comes to technology, internet and privacy. I'm also your usual guy that always aims for the best result and takes a skateboard to work. If you need me, you will find me at the office's Counter-Strike championships on Fridays or at a.yushkevych@monovm.com