Group Management in Linux
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 ID 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. The 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.
1. Command to Make a group (Secondary Group): Below command created a group with the name as provided. The group while creation 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

2. Command to Set 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

3. Command to Display the Group Password File: The below command gives us the password file as output. The file is present in a form such that no information about the file is open for the viewers. Instead of this try: “cat /etc/group” to get more information about the groups.
cat /etc/gshadow

4. Command to Add a User to an Existing Group: Below command is used to add a user to an existing group. The users which may be present in any primary or secondary group will exit the other groups and will become the part of this group.
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.
5. 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

6. Command to Add Multiple Users to a Group at once:
gpasswd -M *username1, *username2, *username3 ...., *usernamen *group_name
Example:
gpasswd -M Person1, Person2, Person3 Group1

7. Command to Delete 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 more 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

8. Command to Delete a Group: Below Command is used to delete the group. The users present in the group will not be deleted. They will remain as they were, but now they will no more be part of this group as the group will be deleted.
groupdel *group_name
Example:
groupdel Group1

Recommended Posts:
- How to find time taken by a command/program on Linux Shell?
- Terminal Mail and Linux Security Mail
- Linux Virtualization - Chroot Jail
- Linux Virtualization : Linux Containers (lxc)
- Linux Kernel Module Programming: Hello World Program
- Formatted text in Linux Terminal using Python
- Some useful Linux Hacks
- Daily life Linux Commands
- Basic Linux Commands for day to day life
- Initializing and Cache Mechanism in Linux Kernel
- How to add "graphics.h" C/C++ library to gcc compiler in Linux
- Working with Magic numbers in Linux
- Reset a lost Linux administrative password and Explanation
- dup() and dup2() Linux system call
- Encrypt/Decrypt Files in Linux using Ccrypt
- Linux File Hierarchy Structure
- mindepth and maxdepth in Linux find() command for limiting search to a specific directory.
- Network configuration and trouble shooting commands in Linux
- Netstat command in Linux
- Program to show that Linux provides time sharing environment to processes
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


