groups Command
The groups command in Linux is used to display the names of the primary and any supplementary groups that a user is a member of. It's a simple yet effective tool for checking user permissions and affiliations.
Syntax
groups [USERNAME]
Description
When executed without any arguments, groups displays the group memberships for the current user. If a username is provided as an argument, it will display the group memberships for that specific user. This command is particularly useful for system administrators and users who need to verify access rights.
Key aspects of groups:
- Lists all groups a user belongs to.
- Includes both the primary group and any supplementary groups.
- Useful for debugging permission issues.
- Provides a quick overview of a user's access privileges.
Examples
Display groups for the current user
groups
# Example Output: user adm cdrom sudo dip plugdev lpadmin sambashare
# Example Output: user adm cdrom sudo dip plugdev lpadmin sambashare
Shows the groups that the currently logged-in user is a member of.
Display groups for a specific user
groups john_doe
# Example Output: john_doe : john_doe users developers
# Example Output: john_doe : john_doe users developers
Lists the groups that the user john_doe belongs to.
Using groups in a script for conditional logic
if groups | grep -q "sudo"; then
echo "User is in sudo group."
else
echo "User is NOT in sudo group."
fi
echo "User is in sudo group."
else
echo "User is NOT in sudo group."
fi
A simple shell script snippet to check if the current user is part of the 'sudo' group.