7 Ways to List All Groups In Linux

In this amazing tutorial, we will learn how to list all groups in Linux. This tutorial is useful for those who want to check all groups on a Linux system as well as we will learn about group membership, group permissions, and some basics of group management.

In order to list all the users and groups in Linux, we can use many commands like groups, getent, cat, grep and etc.,

So in this tutorial, we are going to show you many different ways to list groups in Linux.

List all the groups in Linux

Introduction

In Linux, groups are the organized way to manage user accounts. Users in Linux can be members of one or more groups. Each group can have a set of permissions that can limit the accessibilities of users. Basically, groups determine what kind of action a user can perform.

Suppose, you are a part of a group that does not have permission to edit the files on the Linux system then you can not edit the file too.

Let’s learn about groups

Groups Command: List All The Groups

You can find the list of all the groups you are a member of, using groups command. It is very simple and easy to use.

It is also possible to find groups of other users using the groups command.

groups
List of groups

List the Groups of Other Users

As mentioned, It is also possible to list the groups another user is associated with. You can do it by passing a user name as an argument to the groups command.

Here is the syntax:

groups user_name

Example: Suppose you have a user “john” you want to get the list of all the groups the user “john” is a member of, run the following command-

groups john

Output:

john : john adm cdrom sudo dip plugdev lpadmin lxd sambashare

Listing the groups of multiple users

Using the groups command you can also list the groups of multiple users. Only, you will have to specify the multiple usernames separated by spaces.

Let’s have a look at the syntax:

groups username_1 username_2 username_3... so on

Here, Replace username_1, username_2, and username_3 with actual usernames.

List All The Groups Using Getent Commad With “Group” Database

We can use getent command with the group database to get the list of all the groups along with other information like group id and username which are the members of the groups.

Here is the syntax of getent command:

getent groups

Output:

dips@HP-EliteBook:~$ getent group
root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:syslog,dips
tty:x:5:
disk:x:6:
lp:x:7:
mail:x:8:
news:x:9:
uucp:x:10:
..
..
..
so on

Get the List of All of The Groups By Reading Data from etc/group

We can get the list of groups from “/etc/group” file. You can do so by using the cat command. First, let’s have a look at the example then we will discuss what kind of info we get.

Syntax:

cat /etc/group

Output:

Get the list from the /etc/group file

List All Group Names in Alphabetical Order Using Getent Command

Using the getent command we can get the list of groups in alphabetical order also. In order to do so will use and pipe and sort command to sort the output.

Have a look at the syntax:

getent group | sort

Once you run this command you will get the list of group names sorted alphabetically.

List All the Group Names Only

You can get all the group names only using the cut command. The cut command with some options like -d: -f1: can be used for extracting the specific group field from the “/etc/group” file.

cut -d: -f1 /etc/group

In the output, you will get the list of the only group names of your system.

group names only

Also, we can sort the output in alphabetical order using the pipe and sort command with the grep and cut command.

Here is an example:

getent group | cut -d: -f1 | sort

After running this command you will get the list of group names in alphabetical order.

Grep Command for Listing Groups

The grep command can also be used for listing some specifical groups from the “/etc/grep” file. Let’s have a look at the syntax:

grep username /etc/group

Here, replace the username with the actual user name. Like, In the example below we are replacing it with “dips” which is a user in our system.

Example:

grep dips /etc/group

Output:

adm:x:4:syslog,dips
cdrom:x:24:dips
sudo:x:27:dips
dip:x:30:dips
plugdev:x:46:dips
lpadmin:x:122:dips
lxd:x:134:dips
dips:x:1000:
sambashare:x:135:dips

Listing Group Names Only Using awk Command

The awk command can also be used for listing the groups. Using the awk command we can extract the exact field we want from the text files. There are other uses of the awk command is searching, filtering, and formatting the date of a file.

In order to list group names only from the “/etc/group” file we can run the following command:

awk -F: '{print $1}' /etc/group

Output:

Explanation:

Here, awk command will read the file “/etc/group” and will print the first field name from the all lines. And the first field of the “/etc/group” will always be the group name so it will print the same.

Conclusion

In this tutorial, we have shown how to list the groups in Linux. For the explanation, we have shared a total of 7 methods to list group names. Also, we have covered how to format them for listing in order.

Leave a Comment