How to use groupadd and gpasswd command in Linux

Groups in Linux is as name suggest collection of user. Each group has its unique group ID (GID). groupadd and gpasswd command in Linux are used to manage groups. Groups are of two types i.e. primary group and supplementary group, each system user is member of 1 primary group and zero or more supplementary group. The group information is stored in /etc/group and the respective passwords are stored in the /etc/gshadow file.  All information stored in /etc. As you must be aware a file is assigned separate read, write, and execute permissions for owner, group and everyone else.

Types of Linux group

Linux groups are divided into 2 categories

Primary group

Primary Group is assigned to all files created by user. Usually Primary Group is same as user name. Each group must belong to one primary group.

Secondary group

These groups are used to give privileges’ to users, a user can be member of zero or more groups.

Use groupadd and gpasswd command in Linux

Creating a group with groupadd command in Linux

groupadd command creates new group with default setting. To run this command you must have root privileges.

Syntax

groupadd group_name

groupadd command creates group add its details to /etc/group file.

Creating group with specific group ID using groupadd command in Linux

groupadd command can group with specific group ID by using switch –g. If there is clash of group ID i.e. group ID already exists , the command will give error.

Syntax

groupadd –g group_ID group_name

Set password for a group with gpasswd command in Linux

To set or change password for a group, use command gpasswd followed by group name for which change is to be done.

Syntax

gpasswd group_name

You will be asked to enter new password and its confirmation.

Remove password for a group with gpasswd command in Linux

To remove password for a group, use command gpasswd with switch -r followed by group name for which change is to be done. To remove password, user must be administrator of group.

Syntax

gpasswd -r group_name

Adding user to a group with gpasswd command in Linux

You can add user to a group by using command gpasswd with switch –a.

Syntax

gpasswd –a username groupname

Removing user from group using gpasswd command in Linux

Similar to adding user you can remove a user from a group by using command gpasswd with switch –d.

Syntax

gpasswd –d username groupname

Removing a group using groupdel command in Linux

If you want to delete a group you can use groupdel command. This command deletes all entries for group from system files (/etc/group and /etc/gshadow).

Syntax

groupdel group_name

View or List a group using groups command in Linux

When working with groups you might want to view groups present on your system. groups command can be used for listing out group on your system.

Syntax

groups

Above command when passed without an username will show groups to which current user is member of.

When user name is given with group command, you will get details for that user i.e. to which group that user is member of.

Syntax

groups [username]

To view all groups present on system open /etc/group file, this file is list of all groups on your system.