
A group is a collection of users that simplifies the management of permissions and access to the system resources. Each group has a set of permissions that applies to all the members within the group, simplifying the management of system access.
Adding users to groups allows you to organize access control efficiently and also defines user privileges across the system.
This article explains how to add users to a group in Linux.
Before you begin, you need to:
To manage users and groups, it is important to understand the available groups in the system and the membership of individual users. Follow the steps below to view groups and their associated users.
List all the available user groups.
The above command lists all available user groups including the group IDs (GIDs) and members.
Your output should be similar to the one below.
Within the above output:
group: The name of the groupx: The password field placeholder101: The unique Group ID (GID) assigned to this groupuser: The member of the group.View the groups associated with your actively logged-in user
The above command displays all groups that the logged-in user belongs to.
Your output should be similar to the one below.
View the groups associated with a specific user.
Replace username with the name of the user to view their group membership.
Your output should be similar to the one below.
View the user and group membership.
This command displays the information about a user, including their user’s ID (UID), default group (GID), and supplementary groups.
Your output should be similar to the one below.
Adding a user to a group allows the user to inherit the group permissions. This enables the user to access files, directories, and system resources based on the group privileges.
Add a user to an existing group.
groupname: Replace this with the name of the group to which you want to add the user.username: Replace this with the name of the user you want to add.-a (append) and -G (group) options ensure the user is added to the specified group while retaining membership in the existing groups.Verify the user’s group membership.
Your output should be similar to the one below.
If the -a option is omitted, then the user will be added to the specified group but removed from all other groups.
Caution: This command replaces the user’s existing group memberships with only the specified group.
Verify the user's group membership.
The above command lists the groups the user is a member of.
Your output should be similar to the one below.
You can add a user to multiple groups simultaneously, which is useful for granting access to multiple sets of resources and permissions in a single command.
Add an existing user to multiple groups
group1,group2,group3: Replace these with the names of the groups to which you want to add the user.username: Replace this with the name of the user you want to add.This command adds the user to all specified groups without affecting their membership in existing groups.
Verify the user’s group membership
This will display all the groups the user belongs to, including the newly added groups.
Your output should be similar to the one below.
In Linux, every user is assigned a default group, which is often the same name as the username. You can change this group to assign different permissions and access control to the user.
Change the default group for a user
newdefaultgroup: Replace this with the name of the group you want to set as the user’s default group.username: Replace this with the name of the user whose default group you want to change.-g option sets newdefaultgroup as the user’s default group while retaining membership in existing groups.When a user’s default group is changed, the specified group becomes the user’s new default group, which is applied when creating files or directories. This means new files created by the user will have the new default group as the group owner, while the user retains membership in their existing groups.
Verify that the default group has been changed.
This command displays the user’s (UID), default group (GID), supplementary groups. The default group will now be the newdefaultgroup you specified.
Your output should be similar to the one below.
Follow the steps below to remove a user from a group and revoke access to specific resources or permissions associated with the group.
Remove a user from a group.
username: Replace this with the name of the user you want to remove from the group.groupname: Replace this with the name of the group from which you want to remove the user.-d flag is used to delete the user from the specified group.This will revoke the user’s access to the group’s resources and permissions.
Verify the user has been removed from the group.
Your output should be similar to the one below.
Based on the aco,will show the user’s group membership, confirming that the user is no longer part of the groupname.
You have managed user groups in Linux, including adding users to one or more groups, changing a user's default group, and removing users from a group. These commands enable you to efficiently manage user access and permissions, ensuring a more organized and secure system.
0 Comments
Be the first to comment and share your perspective with the community.