Skip to content

How to check the list of users in Linux?

  • by
  • 5 min read

Keeping track of users on your machine can be crucial, especially if you’re a server admin. Thanks to some pretty useful terminal commands, Linux makes managing users on your system a breeze.

In this article, we’re taking a look at how you can check a list of users on your Linux machine. 

Also read: How to install Kali Linux on Virtualbox?


How to check the list of Linux users using the /etc/passwd file?

All local user information is stored in the /etc/passwd file. You can use either cat or less to open the file.

cat /etc/passwd
How to check the list of users in linux? | candid. Technology

Each line in the file has seven fields seperated by colons that contain seperate information about a particular user in the following oder.

  • User name
  • Encrypted password (if you see an it means the password is stored in the /etc/shadow file.)
  • UID (User ID) number.
  • GID (Group ID) number,
  • Fill name of the user.
  • User’s home directory.
  • Login shell (Bash by default).

You can filter out this information using either the awk or cut command. In the below examples, we’re using these commands to only print out the username of each user.

awk -F: '{ print $1}' /etc/passwd
cut -d: -f1 /etc/passwd

The result will look something like this.

How to check the list of users in linux? | candid. Technology

Also read: How to remove a Directory in Linux?


How to list all Linux users using the getent command?

The getent command displays all entries from the databases configured in the /etc/nsswitch/conf file. This includes the /etc/passwd file data as well. 

getent passwd
How to check the list of users in linux? | candid. Technology

Just as we did with viewing the /etc/passwd file, we can use the awk or cut commands to fetch particular information from this list. 

getent passwd | awk -F: '{ print $1}'
getent passwd | cut -d: -f1
How to check the list of users in linux? | candid. Technology

Note that if you’re using LDAP for user authentication, getent will fetch all Linux users from both the /etc/passwd file and the LDAP database. 

Also read: Mint vs Ubuntu: Linux distro comparison


How to check whether a user exists in a Linux system?

Now that we can get an entire list of all users on our system, finding out whether or not a particular user exists on your system is a matter of a simple grep query. 

getent passwd | grep codery
How to check the list of users in linux? | candid. Technology

As you can see, the aforementioned command queries the passwd file and finds the matching username, indicating that the user exists on the system. 

You can even omit the grep command altogether.

getent passwd codery
How to check the list of users in linux? | candid. Technology

To get the number of users on your Linux machine, use the following command.

getent passwd | wc -l
How to check the list of users in linux? | candid. Technology

Also read: How to use SCP command to transfer files in Linux?


System Users vs Normal users

Now, if you’re wondering why there are so many users on your Linux machine where you use only one or two accounts, the answer is because Linux makes a bunch of accounts when installing itself.

Technically speaking, there are no differences between the system and normal users. System users are during the Linux installation process, while normal users are the ones you might create later. Usually, a normal user will have a real login shell and a home directory. 

Each user on your system will have a user ID, otherwise called UID as well. If you don’t specify a UID when making a user using the useradd command, the OS will automatically assign one from the /etc/login.defs file. This assignment is based on your UID_MIN and UID_MAX values.

To find out what’s the UID range on your system, use the following command. 

grep -E '^UID_MIN|^UID_MAX' /etc/login.defs
How to check the list of users in linux? | candid. Technology

Now that we know our UID range, we can fetch the list of normal users on the system.

getent passwd {1000..60000}
How to check the list of users in linux? | candid. Technology

Once again, you can pipe the output of the above command through either awk or cut and fetch only the information you want. In this case, we’re fetching the username of the account. 

eval getent passwd {$(awk '/^UID_MIN/ {print $2}' /etc/login.defs)..$(awk '/^UID_MAX/ {print $2}' /etc/login.defs)} | cut -d: -f1
How to check the list of users in linux? | candid. Technology

Also read: How to rename a file in Linux?

Yadullah Abidi

Yadullah Abidi

Yadullah is a Computer Science graduate who writes/edits/shoots/codes all things cybersecurity, gaming, and tech hardware. When he's not, he streams himself racing virtual cars. He's been writing and reporting on tech and cybersecurity with websites like Candid.Technology and MakeUseOf since 2018. You can contact him here: yadullahabidi@pm.me.

>