General Linux

How To List Users in Linux

How To List Users in Linux

Introduction

Do you wan to List / Find users in a Linux system?. Once you have a running server with a given number of users logging into it. Sometimes it becomes necessary to get to know all the users that were created in the server for auditing purposes or just for the sake of scheduled house-keeping. In Linux, there are very interesting ways and utilities that you can ride on to get your users.

This tutorial will show you how to list users in Linux systems.

Get a List of All Users using the /etc/passwd File

Local user information is stored in the /etc/passwd file. Each line in this file represents login information for one user. To open the file you can either use cat or less or more:

# cat /etc/passwd

OR use pagers such as more/less command as follows to view /etc/passwd file:

# less /etc/passwd
# more /etc/passwd

As you can see from the output above, each line 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)

How To List Users in Linux

How to only List user names on Linux

If you want to display only the username you can use either awk or cut commands to print only the first field containing the username:

# awk -F: '{ print $1}' /etc/passwd
# cut -d: -f1 /etc/passwd
output:
root
daemon
bin
sys
sync
...
...
sshd
vagrant
jack
anne

Get a List of all Users using the getent Command

The getent command displays entries from databases configured in /etc/nsswitch.conf file including the passwd database which we can use to query a list of all users.

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

# getent passwd

As you can see the output is 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

How To List Users in Linux

Check whether a user exists in the Linux system

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 name jack exists in our Linux system we can use the following command:

# getent passwd | grep jack

If the user exists the command above will print the user’s login information. If there is no output that means the user doesn’t exist.

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

# getent passwd jack

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

33

As you can see from the output above my Linux system has 33 user accounts.

List Linux users with compgen

If you just want to list all the usernames without any additional information, you can use the compgen command with -u option.

# compgen -u
output:
root
daemon
bin
sys
sync
...
...
sshd
vagrant
jack
anne

System and Normal Users

There is no real technical difference between the system and regular (normal) users. Typically system users are created when installing the OS and new packages. In some cases, you can create a system user that will be used by some application.

Normal users are the users created by the root or another user with sudo privileges. Usually, a normal user has a real login shell and a home directory.

Each user has a numeric user ID called UID. If not specified when creating a new user with the useradd command, the UID will be automatically selected from the /etc/login.defs file depending on the UID_MIN and UID_MIN values.

To check the UID_MIN and UID_MIN values on your system you can use the following command:

# grep -E '^UID_MIN|^UID_MAX' /etc/login.defs
out put:
UID_MIN          1000
UID_MAX         60000

From the output above, we can see that all normal users should have a
UID between 1000 and 60000. Knowing the minimal and maximal value allow
us to query a list of all normal users in our system.

The command below will list all normal users in our Linux system:

# getent passwd {1000..60000}

out put :
vagrant:x:1000:1000:vagrant,,,:/home/vagrant:/bin/bash
jack:x:1001:1001:,,,:/home/jack:/bin/bash
anne:x:1002:1002:Anne Stone,,,:/home/anne:/bin/bash
patrick:x:1003:1003:Patrick Star,,,:/home/patrick:/usr/sbin/nologin

Your system UID_MIN and UID_MIN values may be different so the more generic version of the command above would be:

# eval getent passwd {$(awk '/^UID_MIN/ {print $2}' /etc/login.defs)..$(awk '/^UID_MAX/ {print $2}' /etc/login.defs)}

If you want to print only the usernames just pipe the output to the cut command:

# eval getent passwd {$(awk '/^UID_MIN/ {print $2}' /etc/login.defs)..$(awk '/^UID_MAX/ {print $2}' /etc/login.defs)} | cut -d: -f1

Conclusion

In this tutorial, you learned how to list and filter users in your Linux system and what are the main differences between system and normal Linux users.

The same commands apply for any Linux distribution, including Ubuntu, CentOS, RHEL, Debian and Linux Mint.

 

About the author

Amy Gorrell

Amy is a content writer at ERROR HAT. She has years of experience in business and marketing. She also loves technology-related topics. Apart from writing, love to read and watch movies.

Add Comment

Click here to post a comment