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?".
groups groups alice bob
Try it
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.
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
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 ingroups aliceimmediately but not in a running session'sgroupsuntil a new login (ornewgrp docker). - Current process versus configuration.
groupswithout arguments reports the process;groups alicereads the system databases, so the two can differ. - Equivalent to
id -nGin modern coreutils. Useid -Gwhen 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 grouporcut -d: -f1 /etc/group; list members of one group withgetent group NAME. - Add and remove with
usermod -aG GROUP USER(the-amatters: without itusermod -Greplaces the list) andgpasswd -d USER GROUP. - Checking membership in scripts:
id -nG | tr ' ' '\n' | grep -qx dockeris exact;groups | grep dockeralso matchesdocker-users. - Directory services (LDAP, SSSD) supply groups that never appear in
/etc/group;getentandgroupsinclude them, plaingrepdoes not. - Security. Membership in powerful groups such as
docker,sudo,diskoradmis close to root access; audit them regularly.