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
Linux Commands Cheat Sheet Linux, often associated with being a complex operating system primarily used by developers, may not necessarily fit that description entirely. While it can initially appear challenging for beginners, once you immerse yourself in the Linux world, you may find it difficult to return to your previous W
13 min read
Linux/Unix Tutorial Linux is one of the most widely used open-source operating systems. It's fast, secure, stable, and powers everything from smartphones and servers to cloud platforms and IoT devices. Linux is especially popular among developers, system administrators, and DevOps professionals.Linux is:A Unix-like OS
10 min read
25 Basic Linux Commands For Beginners [2025] While performing a task, we all need shortcuts. Shortcuts help us to complete a task quickly. Linux comes with such commands which are one to two words, using that commands, you can perform several operations in no time. As a beginner, you must be aware of those basic Linux commands to complete an o
13 min read
grep command in Unix/Linux The grep command is one of the most useful tools in Linux and Unix systems. It is used to search for specific words, phrases, or patterns inside text files, and shows the matching lines on your screen. Syntax of grep Command in Unix/LinuxThe basic syntax of the `grep` command is as follows:grep [opt
6 min read
Sed Command in Linux/Unix With Examples The SED command (short for Stream Editor) is one of the most powerful tools for text processing in Linux and Unix systems. It's commonly used for tasks like search and replace, text transformation, and stream editing.With SED, you can manipulate text files without opening them in an editor. This mak
8 min read
AWK command in Unix/Linux with examples Awk is a scripting language used for manipulating data and generating reports. The awk command programming language requires no compiling and allows the user to use variables, numeric functions, string functions, and logical operators. Awk is a utility that enables a programmer to write tiny but eff
8 min read
Introduction to Linux Shell and Shell Scripting If we are using any major operating system, we are indirectly interacting with the shell. While running Ubuntu, Linux Mint, or any other Linux distribution, we are interacting with the shell by using the terminal. In this article we will discuss Linux shells and shell scripting so before understandi
8 min read
How to Find a File in Linux | Find Command The find command in Linux is used to search for files and directories based on name, type, size, date, or other conditions. It scans the specified directory and its sub directories to locate files matching the given criteria.find command uses are:Search based on modification time (e.g., files edited
9 min read
ZIP command in Linux with examples In Linux, the zip command compresses one or more files or directories into a single.zip archive file. This saves disk space, keeps data organized, and makes it simple to share or backup files. It's among the most used compression utilities, particularly when sharing large files via email or storing
6 min read
What is Linux Operating System The Linux Operating System is a type of operating system that is similar to Unix, and it is built upon the Linux Kernel. The Linux Kernel is like the brain of the operating system because it manages how the computer interacts with its hardware and resources. It makes sure everything works smoothly a
13 min read