Home / Linux/Unix/BSD / Linux List Groups

Linux List Groups

In Linux, users are those who use and operate the system. Groups are the aggregate of users.

Groups make it easy for administrators to manage which users should have permission and access to certain information.

These include the privileges to read, write, and execute. Apart from an organized system, security is a big factor to consider when configuring users and groups on a Linux system.

For more knowledge on how this information can benefit you, check out this tutorial on how to list users and groups in Linux.

SAMPLE

Linux Groups

Linux users can belong to either of the two types of groups:

Primary group – this group is recorded under the /etc/passwd file upon creating an account by the user.

It is also known as the login group. Users can only belong to one primary group. The group name is usually the same as the user’s name.

Secondary group – these show up in the /etc/group file. The secondary group is where you can assign certain permissions to a group of users.

A user can be a member of as many secondary groups as possible, or to none at all.

How do I list all groups a user is a member of?

If you want to find out the extent of a user’s authority in a Linux program, you can do so by finding out which groups they belong to. You can check this in two ways:

Use the groups command

The groups command is the command to list user groups information. Without an argument, the groups command will execute and print a Linux list of groups a user belongs to:

groups

The first name that appears is the user’s primary group derived from a directory:

hardy developers adm mail proxy cdrom syslog lpadmin plugdev sambashare sudo 

Add the username to the command line as an argument to get a list of all groups they belong in:

groups linuxuser

It should look like this, with the first group being its primary:

linuxwiz : linuxuser adm mail

Use the id command

The id command, which stands for identification, is more specific.

It prints out the list of all groups a user is a member of in a certain category. If you do not encode a username, the information on the current user will be displayed. For example:

id linuxuser

The command will show the user identifier (UID), the user’s primary group identifier (GID), and the user’s secondary groups (groups):

uid=1001(linuxuser) gid=1001(linuxuser) groups=1001(linuxuser),4(adm),8(mail)

To print only the names without the identification numbers, use the -n option. To be more specific, -g option prints only the primary group. -G prints all.

Use this command to list all groups a current user belongs in:

id -nG

It should output:

hardy adm mail proxy developers plugdev sudo

How do I find the Linux list of all users in a group?

This time, it’s the other way around. You’ll want to find out who has access to certain information as defined in a group. Here’s how you can find that Linux list of users:

Use getent group command

Include the group name in your arguments to your getent group command to get a list of all users belonging in it:

getent group developers

If the group exists, it should display a list of users. If it doesn’t, no output will be expected:

developers:x:126:hardy,max,aster

Where “developers” is the group name, x is the password, and 126 is the UID.

How do you list all users?

For a list of all users in a Linux system, you can find them on the /etc/passwd file. Just refer to any of these three commands:

Use the cut command

Type in the cat command and the cut command to isolate specific usernames on the first column of your file:

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

Use the awk command

Awk is widely used on Linux and Unix operating systems for easier data extraction.

You can use the cat command and the awk command similarly as the cut command:

cat /etc/passwd | awk -F: '{print $1}'

Use the getent command

The getent command would be the easiest way to get a hold of your Linux list of users:

getent passwd

The getent command gets its data from the Name Service Switch libraries. This utility allows you to retrieve user information from different sources of data.

The entire list of data sources available can be read from the nsswitch.conf file located at /etc.

You can also opt for the cut and awk with the getent function:

getent passwd | cut -d: -f1

getent passwd | awk -F: '{print $1}'

Use who or users command

If you specifically want to get the usernames in a group that is connected to your Linux host, this is what you should type in:

who

or for lesser details, you may use the users command:

users
devconnected david

How do you list all groups?

To view the Linux list of groups, you will need to find them in the /etc/group file. Here are two commands you can use to get information on a group:

Use the /etc/group file

Each line in the file represents one group. Here’s how you can achieve that:

cat /etc/group

less /etc/group

more /etc/group

If you want group information on a specific group, use the grep function:

cat /etc/group | grep <group>

Use gentent command

Another option for Linux group data is to use the getent command, which provides entries from databases configured in /etc/nsswitch.conf file.

To get the Linux list of every group in the database, type the following command:

getent group

You should get the same output as with the /etc/group file. If you are using LDAP for user authentication, the command should display every group belonging to both /etc/group file and/or LDAP database.

If you have a target group, you type in a key to the gentent group function:

getent group sudo

You can also use awk or cut to display only the first field of the name of the group:

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

Conclusion

And that was it! Pretty simple, right?

We hope this tutorial on how to list Linux users and groups have been of great help.

These commands can surely ease your way coursing through a career as a computer programmer. If you’re still a beginner, don’t be too hard on yourself. Check out more of our tutorials on Linux!

For any questions, feel free to leave us a message in the comments section below.