How To List All The Users In A Linux Or Unix System?


Linux is a multi-user operating system. It means more than one user can use system resources simultaneously. To use the system resources a user must have a user account on the system. So before we start discussing today’s topic you should understand the type of accounts that a Linux/Unix system can have.

Types Of User Account

Basically there are three types of the user account in Linux or Unix –

Root account –

A root account is the most privileged account in Linux/Unix. You can compare this account with the administrator account on Windows. It can perform tasks such as installing/removing a package, alter the configurations, managing other users of the system, etc. Root always has a User id (UID) of 0. A root account is created automatically at the time of operating system installation.

Normal user account –

A normal user account is created by a user while installing OS or after the installation of OS by providing the required information. Usually, these accounts have limited access to system resources. To create this type of user account you can read how to add/remove users in Linux.

Service or system account –

A service account is used for running applications and processes. And these are also created to own data and configuration files. A service or system account is not intended to use by people except for performing administrative tasks. The UID in the range of 0 to 1000 is for service accounts and is configurable in /etc/login.defs. You can create a service or system account using -r flag with useraddcommand. Look at the command below –

useradd -r system_account_name

The /etc/passwd file

This file contains the information of all the user accounts in a system. Each line contains a single user with some other information. You can easily display this file using cat or less command in the terminal. Use one of the following commands-

cat /etc/passwd

Or

less /etc/passwd

Or

more /etc/passwd

Now you will see that each line has seven fields. And each of them is separated by a colon. The first field in each line is the user names of either root, normal user, or service/system account. Each field of a line contains the following information.

  • Username
  • Encrypted password(x means the password is stored in /etc/shadow file )
  • User ID (UID)
  • User’s group ID
  • Full name of the user
  • User’s home directory
  • Default login shell

How to display the list of user names only

To display the list of user accounts use the following command in your terminal. This list will include all types of account i.e. root, normal user, and system/service accounts. Use the following command –

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

And after the execution of this command, you will get the output something like this –

Using getent command to display the list of users

getent is a Unix command that helps a user to get entries from a number of important text files known as databases. We can use this command to display the content of /etc/passwd file.

getent passwd

Or use the following command if you want to display the field containing username only –

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

Or

getent passwd | cut -d: -f1

How to search for a specific user

If you want to search for a specific user, you can do this by using grep with getent command. Use the following command in your terminal –

getent passwd | grep lalit

To count the total number of users, use the following command –

getent passwd  |  wc -l

Now I hope this article is useful to you. If you want to say something on this topic write us in the comments below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.