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”.
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.
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.
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
usermod -aG group1 John_Doe
6. Command to Add Multiple Users to a Group at once:
gpasswd -M *username1, *username2, *username3 ...., *usernamen *group_name
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
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.