groups Command Cheat Sheet
The groups command prints the names of the primary and supplementary groups for each given username, or the current process if no names are given.
Synopsis
groups [OPTION]... [USERNAME]...
Basic Usage
Check Your Own Groups
groups
faruk sudo docker adm cdrom
Check Another User
groups alice
Check Multiple Users
groups alice bob root
alice : alice sudo
bob : bob devs
root : root
How it Works
groups reads directly from /etc/group and potentially other databases (like LDAP/NIS via nsswitch).
- Primary Group: Defined in
/etc/passwd(the 4th field). - Supplementary Groups: Defined in
/etc/group(users listed in the last field).
Comparison: groups vs id
groups is simple and human-readable. id provides numeric IDs and more detail.
| Command | Output Style | Detail Level |
|---|---|---|
groups |
Names only | Low (Simple) |
id -Gn |
Names only | Low (Same as groups) |
id |
uid=1000(u) gid=1000(g) groups=1000(g),27(sudo) |
High (IDs + Names) |
Scripting & Automation
Check if User is in a Group
Since groups output is just text, use grep to check membership.
if groups $USER | grep -q "\bdocker\b"; then
echo "User is in docker group"
else
echo "User is NOT in docker group"
fi
\b (word boundary) is important to avoid matching "dockers" or "dockeruser".
List All Groups in the System
groups only shows groups with members (or for specific users). To see every defined group:
cut -d: -f1 /etc/group | sort
getent group | cut -d: -f1
Common Issues
"I added myself to a group but groups doesn't show it!"
Group membership is applied at login time. If you add a user to a group (e.g., usermod -aG docker $USER), the change is NOT reflected in currently running shells.
Solutions:
1. Log out and log back in. (Best)
2. Use newgrp:
newgrp docker
sg:
sg docker -c "docker ps"
Exit Status
| Code | Meaning |
|---|---|
| 0 | Success |
| 1 | Username not found |
Notes
- Obsolete systems might limit the number of groups a user can be in (historically 16 or 32). Modern Linux kernels support 65536+.
- If
groupsshowscannot find name for group ID ..., it means you belong to a GID that has no corresponding entry in/etc/group.