0
0
Linux CLIscripting~5 mins

groups and group management in Linux CLI - Commands & Configuration

Choose your learning style9 modes available
Introduction
Groups help organize users on a Linux system so they can share files and permissions easily. Managing groups lets you control who can access what without changing each user individually.
When you want to give a team shared access to certain files or folders.
When you need to add a new user to an existing group for project collaboration.
When you want to create a new group for a specific department or role.
When you want to check which users belong to a particular group.
When you want to remove a user from a group to restrict access.
Commands
This command creates a new group named 'developers' so you can add users to it later.
Terminal
sudo groupadd developers
Expected OutputExpected
No output (command runs silently)
Adds the user 'alice' to the 'developers' group without removing her from other groups. The '-aG' flags mean append to groups.
Terminal
sudo usermod -aG developers alice
Expected OutputExpected
No output (command runs silently)
-aG - Append user to supplementary groups without removing from others
Shows all groups that the user 'alice' belongs to, so you can verify she was added correctly.
Terminal
groups alice
Expected OutputExpected
alice : alice developers
Displays the 'developers' group entry including its members, useful to see all users in that group.
Terminal
getent group developers
Expected OutputExpected
developers:x:1001:alice
Removes the user 'alice' from the 'developers' group to revoke her access.
Terminal
sudo gpasswd -d alice developers
Expected OutputExpected
Removing user alice from group developers
Key Concept

If you remember nothing else from this pattern, remember: use 'sudo usermod -aG groupname username' to safely add users to groups without removing existing group memberships.

Common Mistakes
Using 'sudo usermod -G developers alice' without the '-a' flag
This replaces all of alice's groups with only 'developers', removing her from other groups unintentionally.
Always include the '-a' flag with '-G' like 'sudo usermod -aG developers alice' to add without removing.
Trying to add a user to a group that does not exist
The command fails because the group must exist before adding users to it.
Create the group first using 'sudo groupadd groupname' before adding users.
Checking group membership with 'groups' command without specifying username
It shows groups for the current user, not the intended user.
Specify the username like 'groups alice' to see that user's groups.
Summary
Create groups with 'sudo groupadd groupname' to organize users.
Add users to groups safely using 'sudo usermod -aG groupname username'.
Verify group membership with 'groups username' or 'getent group groupname'.
Remove users from groups using 'sudo gpasswd -d username groupname'.