How to List Groups in Linux and Check User Group Membership

Tested on Ubuntu 26.04 LTS (Resolute Raccoon)
Applies to Ubuntu, Debian, Kali Linux, Linux Mint, Pop!_OS, Raspberry Pi OS, elementary OS, Zorin OS, Parrot OS, MX Linux, RHEL, Rocky Linux, AlmaLinux, Oracle Linux, CentOS Stream, Fedora, Arch Linux
Privilege read-only (no elevated privileges)
Scope List groups in Linux with getent, groups, id, and /etc/group.
Related guides add user to group in Linux
list users in Linux
awk
grep
usermod

Linux groups control file permissions, sudo access, and shared resources such as the docker socket. You often need to list every group on the system, see which groups a user belongs to, or list members of one group such as sudo or wheel.

The commands below use getent, groups, and id — the same tools covered in the GNU id manual and NSS getent manual. For adding users to groups, see add user to group in Linux; for listing accounts, see list users in Linux.


Quick reference

Task Command
List all groups (recommended) getent group
List local groups only cat /etc/group
List group names only getent group | cut -d: -f1
Groups for current user groups or id -Gn
Groups for a user groups username or id -Gn username
Full identity (UID, GID, all groups) id username
Primary group name id -gn username
Primary group ID id -g username
All group IDs for a user id -G username
Supplementary members of a group getent group groupname
All members (primary + supplementary) See list all users in a group
GID from group name getent group name | cut -d: -f3
Name from GID getent group GID | cut -d: -f1
Count groups getent group | wc -l
Check if a group exists getent group groupname

List all groups on the system

Use getent (preferred)

getent group queries every group source configured in NSS (local files, systemd, SSSD, LDAP). Use it when you want the complete list, not only what is stored in /etc/group.

bash
getent group

Sample output (trimmed):

text
root:x:0:
daemon:x:1:
adm:x:4:syslog,golinuxcloud
sudo:x:27:golinuxcloud
docker:x:123:
golinuxcloud:x:1000:

Each line is name:password_placeholder:GID:members. An empty fourth field means no supplementary members are listed for that group.

To count how many groups NSS returns on this host:

bash
getent group | wc -l
text
83

Read /etc/group (local only)

cat /etc/group reads the local group file directly. It is faster to skim but can miss directory-backed groups that getent would still show.

bash
cat /etc/group

On a laptop with only local accounts, output usually matches getent group. On domain-joined hosts, getent often shows more.

When you only need names (for scripts or a quick checklist), print the first field:

bash
getent group | cut -d: -f1

Or with awk:

bash
getent group | awk -F: '{print $1}'

Both commands drop the GID and member list, leaving one group name per line.

Sort by GID or filter system groups

To audit GIDs or separate system groups from user-created groups, sort or filter on the third field:

Sort numerically by GID:

bash
getent group | sort -t: -k3 -n

List groups with GID below 1000 (typical system groups on many distributions):

bash
getent group | awk -F: '$3 < 1000 {print $1":"$3}'

Sample:

text
root:0
daemon:1
sudo:27

The name:GID format makes it easy to spot gaps or duplicate IDs.

List groups from Bash completion

Bash can tab-complete group names from its own list. This is a shell helper, not the system group database:

bash
compgen -g

This lists groups known to the shell; it is handy interactively but not a substitute for getent in scripts.


Check groups for a user

To see what groups an account belongs to, start with groups for a short name list or id when you also need numeric IDs.

groups — quick name list

groups prints the username followed by every group name (primary and supplementary). Omit the username to check the current shell user.

Current user:

bash
groups

Another user:

bash
groups golinuxcloud
text
golinuxcloud : golinuxcloud adm cdrom sudo dip plugdev users lpadmin

In this example, golinuxcloud is the primary group. To confirm the primary group reliably, use id -gn username.

id — UID, primary GID, and every group

id shows the user ID, primary group ID, and every group ID with names in parentheses when available.

bash
id golinuxcloud
text
uid=1000(golinuxcloud) gid=1000(golinuxcloud) groups=1000(golinuxcloud),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),100(users),115(lpadmin)

Here gid is the primary group. The groups= list includes primary and supplementary memberships.

Useful id flags when you want a narrower view:

Flag Shows
-gn Primary group name
-g Primary group ID
-Gn All group names
-G All group IDs

Examples:

bash
id -Gn golinuxcloud
id -g golinuxcloud
text
golinuxcloud adm cdrom sudo dip plugdev users lpadmin
1000

-g returns only the primary GID. Use full id username when you need both numeric IDs and names in one line.

Check if a user belongs to a specific group

To check whether a user belongs to a group:

bash
id -nG username | tr ' ' '\n' | grep -x groupname

Example:

bash
id -nG listgroups_demo | tr ' ' '\n' | grep -x docker
text
docker

If the command prints docker, the user is a member of that group. If it prints nothing, the user is not a member or the group is not visible through NSS.

The same pattern works for sudo, wheel, docker, or any other group name returned by id -nG.


List users in a specific group

getent group groupname is the usual starting point — the same approach recommended on Ask Ubuntu and Unix & Linux Stack Exchange for high-traffic “list members of a group” questions.

bash
getent group sudo
text
sudo:x:27:golinuxcloud

The line reads sudo group, GID 27, with golinuxcloud as a supplementary member. The fourth field lists supplementary members only.

To print just the usernames in that field:

bash
getent group sudo | cut -d: -f4
text
golinuxcloud

Or grep the local file when you only care about /etc/group:

bash
grep '^sudo:' /etc/group

List all users in a group, including primary members

getent group groupname shows only users listed as supplementary members in /etc/group. It does not show users whose primary group is that group (primary GID is stored in /etc/passwd, field 4).

To include both supplementary and primary members:

bash
groupname="sudo"
gid=$(getent group "$groupname" | cut -d: -f3)

echo "Supplementary members:"
getent group "$groupname" | cut -d: -f4 | tr ',' '\n'

echo "Primary members:"
getent passwd | awk -F: -v gid="$gid" '$4 == gid {print $1}'

On our test host:

text
Supplementary members:
golinuxcloud
Primary members:

No account uses sudo as a primary group here — only supplementary membership appears. For a group that is commonly a primary group, try the same script with groupname="golinuxcloud":

text
Supplementary members:

Primary members:
golinuxcloud

To print one combined list without blank lines or duplicates:

bash
groupname="sudo"
gid=$(getent group "$groupname" | cut -d: -f3)

{
  getent group "$groupname" | cut -d: -f4 | tr ',' '\n'
  getent passwd | awk -F: -v gid="$gid" '$4 == gid {print $1}'
} | sed '/^$/d' | sort -u

For sudo on our test host, that prints golinuxcloud.

For most admin checks on one account, id username is still the safest way to confirm every group that user belongs to.

Administrative groups — sudo vs wheel

Privileged access is granted through different default group names depending on the distribution. List the relevant admin group to see who has supplementary membership in that group:

Distribution style Admin group Command
Debian, Ubuntu sudo getent group sudo
RHEL, Rocky, AlmaLinux, Fedora wheel getent group wheel

Review both on migrated or mixed environments.


Primary vs supplementary groups

Every user has one primary group (default owner for new files) and zero or more supplementary groups (extra permissions).

Primary GID from passwd (field 4 is the numeric primary group):

bash
getent passwd golinuxcloud | cut -d: -f1,4
text
golinuxcloud:1000

Here 1000 matches the golinuxcloud group GID — that is this user's primary group.

Supplementary membership appears in /etc/group member fields and in id output. For example, golinuxcloud is also in sudo:

bash
getent group sudo
text
sudo:x:27:golinuxcloud

That is why getent group sudo alone is incomplete when you need every account tied to that GID — combine it with the getent passwd awk pattern above.

To change membership, use usermod or groupadd workflows from add user to group. After usermod -aG, the user must log out and back in (or run newgrp groupname) before groups shows the new list in an existing shell.


How /etc/group and NSS work

Each line in /etc/group has four colon-separated fields:

text
group_name:password_placeholder:GID:member_list

Example:

text
sudo:x:27:golinuxcloud
Field Meaning
Group name Human-readable name (sudo, docker, …)
Password Usually x (shadow handles real passwords)
GID Numeric group ID
Members Comma-separated supplementary usernames

The primary group for a user is stored in the fourth field of /etc/passwd, not in the member list of /etc/group. That is why grep username /etc/group can miss a group the user still belongs to.

Linux resolves groups through the Name Service Switch (NSS). Check your configuration:

bash
grep '^group:' /etc/nsswitch.conf

On our test host:

text
group:          files systemd sss

getent group reads this stack — local files, then systemd, then sss (SSSD) when configured — so it is the reliable way to list groups on LDAP or Active Directory-backed systems, not only cat /etc/group.


Look up group name from GID (and reverse)

getent group accepts either a group name or a numeric GID, which is useful in scripts and log files that only show numbers.

Resolve a GID from a group name (third field):

bash
getent group sudo | cut -d: -f3
text
27

Look up the full record when you have a GID from ls -n, logs, or id -G:

bash
getent group 27
text
sudo:x:27:golinuxcloud

Print only the group name from that GID:

bash
getent group 27 | cut -d: -f1
text
sudo

Groups from LDAP, SSSD, or FreeIPA

When nsswitch.conf lists ldap, sss, or similar after files, getent group returns directory groups alongside local entries. If cat /etc/group looks incomplete but getent group shows expected names, NSS is doing its job.

If directory groups are missing:

  1. Confirm group: line in /etc/nsswitch.conf.
  2. Check SSSD or LDAP client services (systemctl status sssd on many setups).
  3. Test NSS with getent group known_directory_group.

Do not edit /etc/group by hand to mirror LDAP membership — manage groups in the directory or with add user to group on the local system when appropriate.


Troubleshooting

Problem What to check
getent group shows more than cat /etc/group Expected when NSS includes SSSD, LDAP, or systemd extras
getent group name omits expected users They may have that group as primary GID — use getent passwd + awk on field 4
grep user /etc/group misses a group User may have that group as primary GID in /etc/passwd
getent group name prints nothing Group does not exist in any NSS source
groups: command not found Use id -Gn username or getent group groupname. On minimal systems, install GNU coreutils or use the available BusyBox user-management tools.
id -g groupname fails -g expects a username, not a group name — use getent group name for GID

References


Summary

Use getent group to list all groups, groups or id -Gn to see what groups a user belongs to, and getent group groupname for supplementary members. When forum-style questions ask for every user in a group, add getent passwd with awk on the primary GID field — getent group alone misses primary members. Prefer getent over cat /etc/group when LDAP or SSSD is in play.


Frequently Asked Questions

1. How do I list all groups in Linux?

Run getent group to list every group from all NSS sources (local files, systemd, SSSD, LDAP). For local groups only, use cat /etc/group. For names only, run getent group | cut -d: -f1.

2. What is the difference between getent group and cat /etc/group?

cat /etc/group reads the local file only. getent group queries the group database defined in /etc/nsswitch.conf, so it can include LDAP, SSSD, and other remote sources. Prefer getent on systems with centralized authentication.

3. How do I check which groups a user belongs to?

Run groups username or id username. For names only, use id -Gn username. Both show primary and supplementary groups without reading /etc/group line by line.

4. How do I list users in a specific group?

Run getent group groupname for supplementary members in the fourth field. To include users whose primary GID matches the group, also run getent passwd and awk on field 4 with that GID. For one user, id username is the safest check.

5. How do I find a group ID from a group name (or the reverse)?

Run getent group groupname and read the third field for the GID, or getent group GID to resolve a numeric ID to a name. Example: getent group sudo | cut -d: -f3.
Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with more than 15 years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, networking, and security, delivering robust and efficient solutions for diverse projects.

  • Go (programming language)
  • Python (programming language)
  • DevOps
  • Computer Security
  • Cloud Computing
  • Kubernetes
  • Linux
  • Ansible (software)