How to List Users in Linux (4 Methods)

As a Linux administrator, it is an important job to ensure that access to directories and files is granted to select, trustworthy users. This prevents unintended users from accessing the entirety of the Linux file system, thus, safeguarding the integrity of the system. Therefore, it becomes essential to learn about the commands that help you list all users in your Linux system.

There are multiple ways to list users in Linux. Some of them are using commands such as cat, more, less, which are used to read and alter files in Linux. The other method is by using pattern scanning methods with tools such as Awk and Getent. In this guide, let’s look at how to list all users in Linux.

1. List Users Using Cat Command in Linux

The cat command in Linux stands for “concatenate” and is used to print, merge, or create files in Linux. The file containing all users’ info on Linux systems is stored in the /etc directory, inside the root folder in a file named “passwd.” Launch the terminal and start typing the following command:

cat /etc/passwd

For example, let’s take the user account — “beebom : x : 1001 : 1001 : Beebom,,,,: /home/beebom : /bin/bash.” Here, each field is separated by a colon and denotes the following:

  • beebom: Name of the account.
  • x: Password, which is encrypted and can be found in the /etc/shadow file
  • 1001: Unique user ID. You can see from the above screenshot that the user ID for the user “beebom” precedes the ID 1000, which is taken by the user Abubakar here.
  • 1001: Group ID
  • Beebom: Full name of the user. The commas that precede the full name are to store info such as phone number and other fields if your Linux distro asks so.
  • /home/beebom: The home directory of the user.
  • /bin/bash: The default shell, i.e. Bash in this case.

2. Use less and more to List Users in Linux

Similarly, you can use less and more to read the contents of /passwd and display it on the terminal. More and Less are used to view files in the command line. More can be used to view the contents of multiple files and the same is separated by lines, whereas less is used to switch between files.

less /etc/passwd
more /etc/passwd
More Linux users

3. Use Awk Command to List Username in Linux

Awk is a scripting language that’s used for pattern scanning and indexing. One of the primary uses for Awk is to allow users to search, list, and display files that meet a certain criterion set by the user using the command line. This command makes it simple to search for specific files with specific terms on the operating system. Here’s how to list all the users in Linux using the awk command.

awk -F: '{print $1}' /etc/passwd
  • -F: Lets awk know about which field separator to use, which, in this case, is “:”
  • ‘{Print $1}’: Tells awk to print the contents of the first field. Fields are separated by ‘:’ in this case.
  • /etc/password: Tells awk the file to print from.

4. Use Getent Command to List Linux Users

Getent (short for Get entry) can be used to get the user data from the /passwd file. Getent is exclusively used to get the data (files) from the database. Consider it as “ls” command but for listing file contents instead of directories. The syntax of the same is:

getent passwd

To get the data of a specific user, you can use the following command.

getent passwd abubakar

where “abubakar” is the user name. Here’s what you will see as the output.

Getent can also help you count the number of users in your Linux system with this command.

getent passwd | wc -l

The aforementioned methods were some of the most common and easier ones to list users in Linux. Some of the other ways to list all the users are by using cut and compgen commands. Although, the above methods should suffice. If you want to check all the available users in Linux, you could always do so by going to Settings -> Users in any distro.

#Tags
Comments 0
Leave a Reply

Loading comments...