Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Permissions and Users

Linux groups command

Reference for the Linux groups command: list the groups of the current or another user, how it differs from id -nG and getent group, and why changes need a new login.

groups prints the names of the groups that a user belongs to, separated by spaces. With no argument it lists the groups of the current process; with user names it lists each user's groups, one line per user prefixed by name :.

Groups are how Linux shares access without sharing an account. A file can be readable by staff, a socket usable by members of docker, and sudo enabled for the sudo or wheel group. groups answers the question "which of these doors can I open?".

Terminal
groups
groups alice bob

Try it

Bash Listing groups and checking membership

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Group database lookups

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
groups [USER]...
Name Description
USER One or more user names. Without any, the current user.

Output: alice sudo docker for the current user, or alice : alice sudo docker when you pass a name. The primary group comes first.

Returns: 0 on success; 1 if a user does not exist or a group ID has no name (groups: cannot find name for group ID N).

Usage notes

  • Login-time snapshot. Your shell gets its group list when you log in. After sudo usermod -aG docker alice, the group appears in groups alice immediately but not in a running session's groups until a new login (or newgrp docker).
  • Current process versus configuration. groups without arguments reports the process; groups alice reads the system databases, so the two can differ.
  • Equivalent to id -nG in modern coreutils. Use id -G when you need numbers, for instance in containers where names are missing.
  • Primary group. Each user has one primary group (from /etc/passwd, shown first); extra groups (from /etc/group) are supplementary. Files you create get the primary group unless the directory has setgid.
  • List all groups on the system with getent group or cut -d: -f1 /etc/group; list members of one group with getent group NAME.
  • Add and remove with usermod -aG GROUP USER (the -a matters: without it usermod -G replaces the list) and gpasswd -d USER GROUP.
  • Checking membership in scripts: id -nG | tr ' ' '\n' | grep -qx docker is exact; groups | grep docker also matches docker-users.
  • Directory services (LDAP, SSSD) supply groups that never appear in /etc/group; getent and groups include them, plain grep does not.
  • Security. Membership in powerful groups such as docker, sudo, disk or adm is close to root access; audit them regularly.