How To List All Group in Linux ?


Groups in Linux are the collection of all the users of a system. These groups offer a convenient and simple way for the system users to share the directories with no issues. A Linux system can have many users who are divided into several groups. Admin must rank each member of these groups to assign the resource-driven permissions privileges. Users can mainly 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 a user's name.

Grants certain privileges to a set of users.

Users must belong to only one primary group.

Users can be members of zero or multiple secondary groups.

However, many Linux users get confused about how to list the available groups. In this short tutorial, we will explain simple ways to list all groups in Linux.

How To List All Group in Linux ?

You can display all groups in Linux using any of the following commands −

  • /etc/group/file

  • Compgen command

  • Getent Command

The /etc/group File

The /etc/group file controls the group membership and defines groups in the Linux systems. It is a simple path that contains information and a list of users belonging to each group. As you may know, the/etc/group is a plain text file, so you can open this file using any of the following commands and list all the groups −

cat /etc/group
less /etc/group
more /etc/group

Each line of this file provides the information of a group. You can extract all the data from this file through the below cut command −

cut -d: -f1 /etc/group

In the above command, we use the cut command to filter only the group name. Therefore, the output shows the list of all the groups.

The Getent Command

Using the getent command, you can display entries from the database configured in the /etc/nsswitch.conf file, including the group database. You can use these entries to query a list of all groups.

If you are working on the networked system, this command can read local groups from the/etc/group file and groups from networked services, such as LDAP. So you can use the getent command to list all groups by reading the group database.

getent group

The output of this command is the same as the content of the /etc/group file. When using LDAP for user authentication, getent displays all groups from both the LDAP database and /etc/group. If you print the first field of the group name, you can run the below command −

getent group | cut -d: -f1
Or 
getent group | awk -F: '{ print $1}'

The Compgen Command

The compgen command is a built-in command belonging to the bash family. You can display all active and registered groups through the following command −

compgen -g

Here we have used the -g option to display the groups. Now, let's discuss different approaches to all the mentioned commands to list all the groups in Linux.

List Groups of the User

You can find out the members of groups in two ways, the groups command and the id command.

  • Pass the username with the 'groups' command whose groups you want to find out.

:~$ groups prateek
prateek : prateek adm cdrom sudo dip plugdev lpadmin lxd sambashare

When you run the 'groups' command without user input, it prints the current user's groups.

:~$ groups
prateek adm cdrom sudo dip plugdev lpadmin lxd sambashare 
  • The 'id' command details about a specified user and its groups. For example, we will print all the groups of the user 'prateek' using the id command as follows −

:~$ id prateek
uid=1000(prateek) gid=1000(prateek) groups=1008(prateek),4(adm),24(cdrom),27(sudo), 30(dip),46(plugdev) 122(1padmin),134(lxd),135(sambashare)

The above command gives you the information related to the user UID (user id), GID (user's primary group), and groups (user's secondary group). If you want to print only the user's group name, you can filter it by using the -Gn option with the above command.

:~$ id -Gn prateek
prateek adm cdrom sudo dip plugdev lpadmin lxd sambashare

Like the 'groups' command, it provides the current user's details if you do not pass any user name with the id command.

:~$ id -Gn 
prateek adm cdrom sudo dip plugdev lpadmin lxd sambashare 
:~$ id
uid=1000(prateek) gid=1000(prateek) groups=1008(prateek),4(adm),24(cdrom),27(sudo), 30(dip),46(plugdev) 122(1padmin),134(lxd),135(sambashare)

Count of All the Linux Groups

You can find out the total count of Linux groups by using any of the following commands −

:~$ cat /etc/group | grep -c ""
76
:~$ getent group | grep -c ""
76
:~$ getent group | wc -l
76

Sort All Linux Group Names in Alphabetical Order

You can use the getent or /etc/group file command with the sort command to alphabetically sort the group names −

getent group | cut -d: -f1 | sort
cat /etc/group | cut -d: -f1 | sort

List All Members of a Group

This way, we can list all the group users using the getent command −

getent group <group_name>

For example, here we will find out the group's members named 'dip'. If a group with this name exists, the command will print their names as output, and vice versa. In case you receive, no output means no group with this name exists −

getent group dip

:~$ getent group dip
dip:x:30:prateek

Similarly, you can find out the members of other groups.

Conclusion

Different files in Linux have their groups and users. Sometimes it becomes essential for the user to know which group they belong to. In Linux, you can get complete information about users, group names, and active users with the help of some commands. Here we explained some examples to list all groups in Linux.

You can use all the above commands for any Linux distribution, including Linux Mint, CentOS, Debian, and RHEL. As you have seen above, we mainly used the /etc/group file and getent command to get all group details of Linux. We also use the cut, count, and sort commands to present better the output obtained from these commands.

Updated on: 18-May-2023

684 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements