How to List All User Groups on Linux

User groups on Linux help you define a set of permissions that you can then impose on other users. Unix and Linux come with some pre-configured user groups, and as an administrator, it’s easy to create additional groups to further categorize and manage users.

But before creating a new group, you’d want to know more about the existing ones. Luckily, there are several ways to list all user groups present on Linux, and you can even view the list of groups a specific user is a part of. Let’s get started.

MAKEUSEOF VIDEO OF THE DAY

SCROLL TO CONTINUE WITH CONTENT

Using the /etc/group File

The /etc/group file contains information on all local user groups configured on a Linux machine. With the /etc/group file, you can view group names, passwords, group IDs, and members associated with each group.

View the contents of the file using the cat command:

 cat /etc/group 

list user groups etc group

The output might be confusing at first. Where are the group names? And what are these “x”s and colons in the output?

The first column (the text before the first colon) is what you’re looking for. You can view a prettified version of the file and display only the group names using the cut command:

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

prettify etc group contents output linux

This simple list is both easier on the eyes and perfect for use in scripts.

You can also count the total number of local groups on your machine using wc:

 cat /etc/group | wc -l 

To make things interesting, create a new group using the groupadd command and then view the total number of user groups on your system. As obvious, the count will increase by one and you’ll be able to see the group name listed in the output.

List Groups Using the getent Command

getent, short for “get entries,” is a Linux command for viewing the contents of system information files, also known as databases, on Linux. /etc/group, /etc/passwd, and /etc/shadow files are good examples of such databases.

Using the getent command to view user group information on Linux is straightforward. All you need to do is type getent followed by the file you want to view. In this case, it’s the group file.

 getent group 

getent group list user groups on linux

The output of “getent group” will be slightly different from the cat /etc/group command. This is because getent pulls group information from other similar databases on your system (LDAP, for example).

Use the cut command to parse the output and display only group names:

 getent group | cut -d: -f1 

The getent command is versatile. You can list the names of all users on Linux by getting all the entries from the /etc/passwd file and then parsing the output for user names.

View Group List for a Specific User Using groups

It’s hard to visually match user names with their groups using the previous methods. If you only want to list groups a particular user is a part of, consider using the groups command instead.

The basic syntax for the command is:

 groups username 

If you don’t specify a username, the output will display all groups for the current user. But for the sake of clarity, it’s best to supply a username as an argument.

To get a list of groups for a user named “testuser”, run:

 groups testuser 

check groups of a user on linux

Groups Simplify User Access Control on Linux

Technically, the root user is the owner of the entire system and has permissions no other user possesses. Groups are a way for the superuser to categorize users, grant them authorizations, and impose restrictions to prevent them from performing undesirable operations.

Instead of granting permissions to each user, you can create a group and add all the users to it. Then, all you need to do is manage the permissions for the said group and the rules will be imposed upon the members automatically. This is one of the many ways to manage users on Linux and other Unix-related operating systems.

Alternate Text Gọi ngay