Knowledge Base Hub

Browse through our helpful how-to guides to get the fastest solutions to your technical issues.

Home  >  How-Tos  >  How to List and Filter Users in Linux
Top Scroll

How to List and Filter Users in Linux

 3 min

In this KB article, you will learn how to list and filter users in the Linux system and what are the main differences between system and normal Linux users. The same commands can be run on any Linux distro, including Ubuntu, CentOS, Linux Mint, Debian and RHEL.

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

Local user information is saved in the /etc/passwd file. Every line describes login information for one user in this file. You can either use cat or less to open the file:

less /etc/passwd
Sample outputs

root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
….
..

vihan:x:11:11:Vihan Rege,,,:/home/cache/vihan:/bin/sh

As you can see from the output above, total seven fields exists and all fields are delimited by colon (:) symbol that have the following information.

  • User name
  • x means that the Encrypted 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 display only the username, you can use either awk or cut commands to print only the first field containing the username:

Type the awk or cut commands to list only usernames:

awk -F: ‘{ print $1}’ /etc/passwd

cut -d: -f1 /etc/passwd

Output

root
daemon
bin
sys
sync


vihan

Get a List of all Users using the getent Command

The getent command shows entries from databases configured in /etc/nsswitch.conf file as well as the passwd database which we can use to request a list of all users.

Type the following getent command to get a list of all Linux users:

getent passwd

You will see the output is the same as when showing the content of the /etc/passwd file. The getent will show all Linux users from both /etc/passwd file and LDAP database, if you are using LDAP for user authentication.

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

Check if a user exists in the Linux system

Now we learn how to list all users. We can easily filter the users’ list by piping the list to the grep command to verify, if the user exists in our Linux box.

As an example, we can use the following command to find out, if the user with the name Vihan exists in our Linux system.

getent passwd | grep vihan

Output

vihan:x:11:11:Vihan Rege,,,:/home/vihan:/bin/bash

The command above displays the user’s login information, if the user exists. If no output is displayed that means the user doesn’t exist.

We can also check user existence without using the grep command as follows:

getent passwd jack

Like before, the command will show the user’s login information, if the user exists.

You can run the following command to find out the numbers of user accounts you have on your system:

getent passwd | wc -l

Output

22

You can see the numbers of user accounts from the output as above in the Linux system (my Linux system has 22).

System and Normal Users

The system and normal (regular) users are technically the same. Normally, system users are created while installing the operating system and new packages. In some circumstances, you can create a system user that will be used by a few applications.

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

Each user has a numerical user ID named UID. If not defined while creating a new user with the useradd command, the UID will be automatically picked from the /etc/login.defs file depending on the UID_MIN and UID_MAX values.

You can run the following command to check the UID_MIN and UID_MIX values on your Linux system:

grep -E ‘^UID_MIN|^UID_MAX’ /etc/login.defs

Output

UID_MIN 1000
UID_MAX 20000

We can see in the above output that all normal users should have a UID between 1000 and 20000.

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

getent passwd {1000..20000}

Output

james:x:1000:1000:james,,,:/home/james:/bin/bash
vihan:x:1001:1001:,,,:/home/vihan:/bin/bash
ana:x:1002:1002:Ana Joe,,,:/home/ana:/bin/bash

Your system UID_MIN and UID_MIX values may be varied.

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

You can also print the usernames only by adding pipe 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

Feel free to contact us if you have any questions.

 

For our Knowledge Base visitors only
Get 10% OFF on Hosting
Special Offer!
30
MINS
59
SECS
Claim the discount before it’s too late. Use the coupon code:
STORYSAVER
Note: Copy the coupon code and apply it on checkout.