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.

Updated: 07 Aug, 23 by Antoniy Yushkevych 10 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

In this part, we are going to tell you what is the meaning of group user in Linux and add users in group to Linux. 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: 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 user group linux command line and add user in group linux command.

In the previous part, you learned how to add a user in a group in Linux. By reading this section, you will understand how create user and group in Linux. 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.

After creating the group and user, you should learn to add a user to a group in Linux, which we will tell you about in this training section. We have bifurcated the topic into sub-parts. The security of the group depends a lot on its host. For more information in this field, see the article How to Secure Linux Server? 

How to add a user to a group in Linux?

In this section, we will teach you different ways to Linux add user to group by id and without it.

First method:

We will describe the first method of how to add a user to a group in the Linux command in this section. Use the add user command; here is the syntax: 

sudo adduser username newgroup

Second method:

This section shows the second way to add user to group in the Linux command line. To use the useradd command as shown below:

sudo useradd –G newgroup username

Third method:

You need to know how to add a user to another group. In this part, we will teach you the third method to add user to another group in Linux. Use the usermod command to add a user in a group in linux:

sudo usermod –a –G groupname username

There are two options, the -append option and the -group option. They are used by the user mode command to append the users with a group. Without the append option, you may be discarded from other groups. In this section, you learned one way how to add member to a group in Linux 

After learning Linux add user to group, you should learn how to create a user.

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

In this section, we will go through the steps on how to create a user, add user in admin group Linux and add multiple users to a group in Linux. Follow this simple command to create a user and immediately add him to an already existing group:

sudo useradd –G groupname newuser

Now, add a password by using the following command:

sudo passwd newuser

That’s it!  In this part, we went through the steps to create user and group in linux. 

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

How to Linux add user to group command line you learned in the previous part; In this part, we will teach you how to add multiple users in a group in Linux. 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, the initial username 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

If you do not write the username's name, the command automatically displays the current logged-in user.

Moving on, many group names are 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.

🌐 Unleash the true potential of your website with our cutting-edge Linux VPS hosting solutions! Scale effortlessly and reach new heights in the digital world. 📈🌟💻

In Linux, you can display the list of user groups using the "cat" or "less" command to read the "/etc/group" file. This file contains information about all the user groups on the system.

To display the user groups using the "cat" command, open a terminal and type:

Cat/etc/group

This will output the content of the "/etc/group" file, which includes the list of user groups, their group ID (GID), and the list of users that belong to each group. If the "/etc/group" file is too long to fit on a single screen, you can view the "less" command page by page.

Type the following command in the terminal:

Less/etc/group

You can navigate through the file using the arrow keys or press the "q" key to exit when you're done. Viewing the "/etc/group" file may require administrative privileges. You may need to use the "sudo" command before the "cat" or "less" command to gain the necessary permissions:

sudo cat /etc/group

or

sudo less /etc/group

Remember that some Linux distributions may store user group information in different files or locations, but the "/etc/group" file is the most common.

You can use the group's command to view the groups a user account is assigned to in Linux. Open a terminal and type the following command, replacing "username" with the name of the user account you want to check:

groups username

For example, to view the groups for the user "john":

groups john

The output will list all the groups the specified user is a member of. Spaces will separate each group name.

Please note that if you are not logged in as the user or do not have sufficient permissions, you may need to use the sudo command to run the groups command with elevated privileges:

Sudo groups username

This will prompt you to enter a user's password with administrative rights on the system. After entering the password, you will see the list of groups for the specified user account.

In Linux, you can view all groups on the system by examining the contents of the "/etc/group" file. This file contains information about all the user groups defined in the system, their respective Group IDs (GID), and the list of users that belong to each group.

To view all groups on the system, open a terminal and read the "/etc/group" file using the cat or less command. Here's how you can do it using the cat command:

cat /etc/group

Using the less command:

Less/etc/group

The output will display the content of the "/etc/group" file, which lists all the groups on the system. Each line in the file represents a single group and follows a specific format:

makefileCopy code

group_name password:GID:user_list

group_name: The name of the group.

Password

 Usually represented by "x" (an encrypted password placeholder, as group passwords are typically stored in the "/etc/shadow" file).

To change the password of your group, you can refer to the article How to Change Linux Password?

GID

The Group ID is a unique numerical identifier for the group.

user_list: A comma-separated list of user accounts that are group members.

The user_list may be empty if the group has no members.

Viewing the "/etc/group" file might require administrative privileges. If you encounter a permissions issue, you can use the sudo command to gain elevated privileges:

sudo cat /etc/group

or

sudo less /etc/group

This will prompt you to enter a user's password with administrative rights on the system. After entering the password, you can see the complete list of all groups on the system.

This article provided basic knowledge about Linux groups. We hope the mentioned commands to add or remove users in a group helped you sort out your queries. Keep practising the commands to build a firm grip on the topic.

People also read: 

Antoniy Yushkevych

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