Group Management in Linux
Last Updated :
21 Jul, 2023
There are 2 categories of groups in the Linux operating system i.e. Primary and Secondary groups. The Primary Group is a group that is automatically generated while creating a user with a unique user ID simultaneously a group with an ID the same as the user ID is created and the user gets added to the group and becomes the first and only member of the group. This group is called the primary group. A secondary group is a group that can be created separately with the help of commands and we can then add users to it by changing the group ID of users.
How to Create/Add Users in Linux
Creating a Secondary Group
The below command created a group with the name provided. The group while creating gets a group ID and we can get to know everything about the group as its name, ID, and the users present in it in the file "/etc/group".
groupadd group_name
Example:
groupadd Group1

Setting the Password for the Group
Below command is used to set the password of the group. After executing the command, we have to enter the new password which we want to assign to the group. The password has to be given twice for confirmation purposes.
gpasswd group_name
Example:
gpasswd Group1

Command to Display the Group Password File:
To access information about groups and their passwords, you can view the password file, /etc/gshadow. However, keep in mind that this file is not intended for regular viewing. To gather more comprehensive information about groups.
cat /etc/gshadow

Adding a User to an Existing Group
To add a user to an existing group, you can utilize the usermod command. By specifying the group name, you can add a user to the desired group. However, note that when a user is added to a new group, they are automatically removed from their previous groups.
usermod -G group_name username
usermod -G group1 John_Doe

Note:
If we add a user to a group then it automatically gets removed from the previous groups, we can prevent this by the command given below.
Command to Add User to Group Without Removing from Existing Groups
This command is used to add a user to a new group while preventing him from getting removed from his existing groups.
usermod -aG *group_name *username
Example:
usermod -aG group1 John_Doe

Command to Add Multiple Users to a Group at once
To add multiple users to a group simultaneously, you can utilize the gpasswd command with the -M option. This command allows you to specify a list of usernames separated by commas.
gpasswd -M *username1, *username2, *username3 ...., *usernamen *group_name
Example:
gpasswd -M Person1, Person2, Person3 Group1

Deleting a User from a Group
Below command is used to delete a user from a group. The user is then removed from the group though it is still a valid user in the system but it is no longer a part of the group. The user remains part of the groups which it was in and if it was part of no other group then it will be part of its primary group.
gpasswd -d *username1 *group_name
Example:
gpasswd -d Person1 Group1

Command to Delete a Group
To delete a group from the system, use the groupdel command. This action removes the group while retaining the users who were members of the group. They will revert to their primary groups if they are not part of any other groups.
groupdel *group_name
Example:
groupdel Group1

Conclusion
In this article we have discussed group management in Linux which involves creating and managing primary and secondary groups, setting group passwords, adding and removing users, and deleting groups. We also discussed effectively utilizing these commands and techniques, administrators can organize user accounts, assign appropriate access permissions, and ensure a secure and efficient Linux system.
Similar Reads
File Management in Linux
In Linux, most of the operations are performed on files. And to handle these files Linux has directories also known as folders which are maintained in a tree-like structure. Though, these directories are also a type of file themselves. Linux has 3 types of files: Regular Files: It is the common file
4 min read
User Management in Linux
A user is an entity, in a Linux operating system, that can manipulate files and perform several other operations. Each user is assigned an ID that is unique for each user in the operating system. In this post, we will learn about users and commands which are used to get information about the users.
4 min read
How to Rename a Group in Linux?
Renaming a group in a Linux system is a straightforward but essential administrative task. If you're reorganizing your user management structure or enhancing security measures, this quick guide will walk you through the simple steps to rename a group. Linux provides a powerful command, 'groupmod' to
4 min read
How to Manage Directories in Linux?
Directories in Linux or any operating system help to organize the data and files, which is then easier to find and manage. In this article, you will learn everything you will need to manage directories in Linux. We will try to cover every topic from creating, and copying to deleting the directories
6 min read
What Is GNOME in Linux?
GNOME Linux, a pivotal desktop environment in the Linux landscape, stands as the epitome of user-centric design and functionality. Developed under the acronym GNOME, which stands for GNU Network Object Model Environment, this desktop environment has earned its stripes for delivering a seamless, eleg
7 min read
How to Remove Users from Groups in Linux?
Groups in Linux are an important part of organizing the system's access control. Creating separate groups for separate types of roles of users allows the administrator to manage the access control of the Linux system efficiently. It is an essential skill to understand how to add, remove, and update
4 min read
Groups in Linux System Administration
Each group in a Linux system is uniquely identified by a group identification number or GID. All the information listing groups in a system are stored in /etc/group file. The hashed passwords for groups are stored in /etc/gshadow file. Every user has a primary user group and zero or more supplementa
2 min read
How to Remove All Users From a Group in Linux?
A group is a collection object in the Linux operating system, which associates certain properties and access control to all its members. It is an efficient way for a system administrator to manage Access Control for its users. Groups are an important tool for a system administrator to maintain many
6 min read
How to Add User to a Group in Linux
A group in Linux is a way to put users with similar access and permissions in a collection. By using groups, an administrator can define access control and permissions for all the users belonging to that group. Without groups, the administrator would have to define roles for individual users however
7 min read
What Is Linux
The Linux opeÂrating system is a collection of open-source software programs designed to function similarly to Unix systeÂms. Linus Torvalds, a Finnish software engineeÂr, initially launched the Linux kerneÂl, which serves as the core component, on SeptembeÂr 17, 1991. This kernel acts as a vital
12 min read