Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How To List All Group in Linux ?
Groups in Linux are collections of users that provide a convenient way to manage permissions and resource access. A Linux system can have many users organized into multiple groups, where administrators assign specific privileges to control file and directory access.
Users can belong to two types of groups
| Primary / Login Group | Secondary / Supplementary Group |
|---|---|
| User-created files are assigned to this group. The primary group usually has the same name as the user. | Grants additional privileges to a set of users for specific tasks or resources. |
| Users must belong to exactly one primary group. | Users can be members of zero or multiple secondary groups. |
This tutorial explains simple methods to list all groups in Linux systems using various command-line approaches.
Methods to List All Groups
You can display all groups in Linux using any of the following methods
/etc/groupfile commandsgetentcommandcompgencommand
Using the /etc/group File
The /etc/group file stores group information and membership details. It's a plain text file that you can view using standard text display commands
cat /etc/group less /etc/group more /etc/group
To extract only group names, use the cut command to filter the first field
cut -d: -f1 /etc/group
Using the Getent Command
The getent command displays entries from system databases configured in /etc/nsswitch.conf, including local and networked groups (such as LDAP)
getent group
To display only group names
getent group | cut -d: -f1
# Or using awk
getent group | awk -F: '{ print $1}'
Using the Compgen Command
The compgen command is a bash built-in that displays all registered groups
compgen -g
List Groups for a Specific User
You can find which groups a user belongs to using the groups or id commands.
Using the Groups Command
groups username # Example groups prateek
prateek : prateek adm cdrom sudo dip plugdev lpadmin lxd sambashare
Running groups without a username shows the current user's groups
groups
Using the ID Command
The id command provides detailed information about user ID, primary group, and secondary groups
id username # Example id prateek
uid=1000(prateek) gid=1000(prateek) groups=1000(prateek),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),122(lpadmin),134(lxd),135(sambashare)
To display only group names, use the -Gn option
id -Gn prateek
Additional Group Operations
Count Total Groups
To count the total number of groups
getent group | wc -l # Or cat /etc/group | wc -l
Sort Groups Alphabetically
To display groups in alphabetical order
getent group | cut -d: -f1 | sort cat /etc/group | cut -d: -f1 | sort
List Members of a Specific Group
To find all members of a particular group
getent group group_name # Example getent group dip
dip:x:30:prateek
The output format is group_name:password:GID:member_list.
Conclusion
Linux provides multiple methods to list and manage groups effectively. The /etc/group file, getent, and compgen commands offer different approaches to view group information. These commands work across all Linux distributions and can be combined with text processing tools like cut, sort, and wc for enhanced output formatting and analysis.
