List groups in Ubuntu
How To

List Groups in Ubuntu Command Line

Sagar Sharma
Abhishek
Sagar Sharma, Abhishek

Table of Contents

Linux, from its core, is built to have multiple users.

And separating users into different groups is pretty convenient for managing permissions.

A user can be a member of more than one group. This is why the groups are divided into the following two categories:

  • Primary group: Also known as the login or default group. If the user creates a file, the primary group takes the group ownership. Usually, the primary group has the same name as the user.
  • Secondary group: Also known as a supplementary group. It is used to grant specific permissions to users. For example, in Ubuntu, only members of the sudo groups can run commands with sudo.

How do you know which group a user belongs to? Let me show you that.

How to check the groups of a user

Here are various scenarios for listing user groups in Ubuntu.

List groups of the current user

To list the groups of the current user, all you have to do is execute a single command without any additional flags:

groups
list groups of the curent user

Yep, all you have to do is execute the groups command!

List groups of the other users

If you want to find out the groups to which the specific user belongs, all you have to do is append the username to the groups command:

groups username

For your reference, here, I checked the groups where the user root and sagar belong:

groups root
groups sagar
list groups of the specific user

I could have used both users in single command as well.

💡
You can get the primary group of a user using the command: id -gn user_name

List all the groups in Ubuntu

You just learned how to see which groups a user belongs to. How about listing all the available groups? That can be done too.

The /etc/group file contains all the necessary information about the local group.

So you can read the contents of that file and easily list the groups:

cat /etc/group
use groups file to list groups in ubuntu

Similarly, if you want to find a specific group, you can use the grep command to filter the output:

cat /etc/group | grep sudo
find speciofic group in linux

In case you are wondering about the meaning of the output, here's what that means:

  • sudo is the name of the group.
  • x indicates that shadow passwords are being used and can be found at /etc/gshadow.
  • 27 is the group ID.
  • sagar is the list of the group users. You can have multiple of them.

And if they seem irrelevant, you can use the cut command and print the group names only:

cut -d: -f1 /etc/group
use the cut command to trim output in ubuntu terminal

Another option is to use the getent command

How to list groups using the getent command

The getent command is used to get entries from the database, including passwd and group database.

This means it can be used for our purpose too!

To get a list of groups, all you need to do is append group to the getent command:

getent group
use getent command to list groups in ubuntu terminal

List every member of a specific group

If you want a list of every user belonging to a specific group, you can use the getent command in the following syntax:

getent group [group_name]

Let's say I want to list every member of the sudo group, then I will be using the following command:

getent group sudo
list each member of user group in ubuntu

Want to know how you can add a user to a group?

Cannot see the group you are looking for? How about creating that group?

How to Add New Groups in Ubuntu
Need to create a new group? Here’s how to create groups in Ubuntu using the groupadd command.

If you want to add a user to an existing group and have no idea how to? We have a dedicated guide to solve this issue:

How to Add User to a Group in Ubuntu Command Line
Here’s how you can assign a new group to an existing user or change its primary user group in Ubuntu. Also learn about creating new users with given groups.

Or remove a user from a certain group.

How to Remove User from Group in Ubuntu Command Line
Added a user to the wrong group? You can remove the user from a given group using either the deluser or usermod commands.

I hope you will find this guide helpful and if you have any queries, let me know in the comments.



Sagar Sharma

Sagar Sharma

A software engineer who loves to tinker with hardware till it gets crashed. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.