System and users
groups
Print the groups the current user, or each named user, belongs to.
groups [USER...]
groups lists group names. With no argument it describes the current process (the groups
your shell actually holds); with user names it looks each one up in the account database and
prefixes the answer with user :.
It is a shorthand for id -Gn, and id is the better choice in scripts because it can print
numbers and has no prefix to strip. Reach for groups at the prompt, when you want to check
why you cannot open a file owned by some group, or whether adding yourself to a group has taken
effect yet. To go the other way and see who is in a group, use getent group NAME.
Examples
List your own groups
groups
dev
The sandbox user has only its primary group, dev.
List another user's groups
groups root
root : root
With a name, the output is prefixed with user : so the answers stay readable when you ask about several users.
Ask about several users at once
groups dev root www-data
dev : dev root : root www-data : www-data
One line per user, each in the user : groups form.
Get the same list from id
id -Gn
dev
id -Gn prints exactly what bare groups prints, and id -G gives the numeric IDs.
See who is in a group
getent group sudo users
sudo:x:27: users:x:100:
getent group prints the group database entries: name, password placeholder, GID, then the
comma-separated member list. Both groups are empty in this sandbox, so the last field is
blank. A user's primary group membership never appears in that list; it is recorded in
/etc/passwd.
Check membership in a script
if groups | tr ' ' '\n' | grep -qx docker; then
echo "can use docker"
else
echo "not in the docker group: ask an admin, then log in again" >&2
exit 1
fi
not in the docker group: ask an admin, then log in again
Splitting the list onto separate lines and matching a whole line with grep -x avoids false
matches such as docker inside dockerusers.
Ask about a user that does not exist
groups nobody-here
groups: ‘nobody-here’: no such user
An unknown name is reported on stderr and the exit status is 1.
Common pitfalls
Adding yourself to a group and expecting
groupsto show it in the same session. Baregroupsreports the running shell, which keeps the list it got at login;groups $USERreads the database and shows the change at once. Log out and in again, or runnewgrp GROUP.Reading
getent group Gas the complete member list. Users whose primary group is G are not listed there.
GNU vs BSD and macOS
The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).
On macOS,
groupsexists but group membership also comes from Directory Services;id -Gnanddscl . -read /Groups/NAME GroupMembershipare the usual tools there.
Options worth knowing
A selection, described in our own words. For the full list, see groups(1) or groups --help.
| Option | What it does |
|---|---|
--help | Print usage and exit. |
--version | Print the coreutils version and exit. |