How To List Users and Groups in Linux

In this guide, we want to teach you How To List Users and Groups in Linux.

Users and groups are used on GNU/Linux for access control—that is, to control access to the system’s files, directories, and peripherals.

A user is anyone who uses a computer. Users may be grouped together into a “group”, and users may be added to an existing group to utilize the privileged access it grants.

How To List Users and Groups in Linux

There are several ways to list users and groups on Linux with the Linux Commands. Let’s see what are they.

List Users with /etc/passwd File in Linux

The /etc/passwd file is used to keep track of every registered user that has access to a system. Each line in this file represents login information for one user. To see the information on this file, you can use the cat command:

cat /etc/passwd

You will get something similar to this:

Output
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
....
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
orca:x:1000:1000::/home/orca:/bin/bash
olivia:x:1001:1001::/home/olivia:/bin/bash

Also, you can use the “less” or the “more” command in order to navigate within the username list.

$ less /etc/passwd
$ more /etc/passwd

Each line in the file has seven fields delimited by colons that contain the following information:

  • User name.
  • Encrypted password (x means that the password is stored in the /etc/shadow file).
  • User ID number (UID).
  • User’s group ID number (GID).
  • Full name of the user (GECOS).
  • User home directory.
  • Login shell (defaults to /bin/bash).

If you want to show only the usernames, you can use the cut command as shown below:

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

You will see:

Output
root
bin
daemon
adm
...
postfix
chrony
orca
olivia

Or you can use the awk command instead of the cut command to list the usernames in Linux:

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

You will get similar output from the cut command.

List Users with the getent Command

The easiest way to list users in Linux is to use the “getent” command with the “passwd” argument and specify an optional user that you want to list on your system.

getent is a Linux command that helps the user to get the entries in a number of important text files called databases. This includes the passwd and the group of databases that stores the user information.

To get a list of all Linux users, run the following command:

getent passwd

The output will the same as when displaying the content of the /etc/passwd file.

If you are using LDAP for user authentication, the getent will display all Linux users from both /etc/passwd file and LDAP database.

You can also use awk or cut to print only the first field containing the username:

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

Now that we know how to list all users, to check whether a user exists in our Linux box we, can simply filter the users’ list by piping the list to the grep command.

For example, to find out if a user with the name orca exists in our Linux system we can use the following command:

getent passwd | grep orca
Output
orca:x:1000:1000::/home/orca:/bin/bash

We can also check whether a user exists without using the grep command as shown below:

getent passwd orca

Same as before, if the user exists, the command will display the user’s login information.

If you want to find out how many users accounts you have on your system, pipe the getent passwd output to the wc command:

getent passwd | wc -l
Output
21

Note: To list all the connected users on your Linux system, you can use the command below:

who

This will provide you a list of users currently connected on your host along with the shell they are using and when they connected.

List Groups with /etc/group File

It is the same as the listing users.

To see the information on this file, you can use the cat command:

cat /etc/group

You will get something similar to this:

Output
root:x:0:
bin:x:1:
daemon:x:2:
sys:x:3:
adm:x:4:
...
chrony:x:996:
orca:x:1000:
olivia:x:1001:

As you can see, inspecting the /etc/group gives you a complete and sometimes too detailed listing of all the groups on your system.

If you want to show only the group names, you can use the cut command as shown below:

cat /etc/group | cut -d: -f1
Output
root
bin
daemon
sys
adm
tty
disk
lp
mem
kmem
wheel
...
postfix
chrony
orca
olivia

Or you can use the awk command:

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

List Groups with the genet Command

To get a list of all Linux groups, run the following command:

getent group

The output will the same as when displaying the content of the /etc/group file.

Similar to the passwd database, you can choose to “target” one specific group by providing a key to the getent function. For example:

getent group sudo

Conclusion

At this point, you learn to list users and groups in Linux and filter them in your system.

Hope you enjoy it.

You may be interested in these articles:

How To Check Memory Usage on Linux

Use Bash wait Command on Linux

Configure Automatic Kernel Updates on Linux

How To Check the RAID configuration on Linux

Fix the Linux Time change After Reboot

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

POPULAR TAGS

Most Popular