- How to List Users in Linux
- The File Containing Users in Linux
- Listing All Users
- Using etc/passwd File
- Using getent passwd Command
- Listing only User Names
- Listing Currently Logged in Users
- Listing All Users in a Group
- Listing Sudo Users
- Listing Locked Users
- Conclusion
- Tags
- Recent Posts
- How to List Users in Linux
- Listing Users in Linux
- List Users with cat Command
- List Users with Terminal Pagers less and more
- List Users with awk Command
- List Users with getent Command
- Listing Normal and System users in Linux
- How to List Users in Linux (4 Methods)
- 1. List Users Using Cat Command in Linux
- 2. Use less and more to List Users in Linux
- 3. Use Awk Command to List Username in Linux
- 4. Use Getent Command to List Linux Users
How to List Users in Linux
Because of its multi-user features, Linux stands as the most liked operating system among people especially for those who love to run commands. Like its other exceptional characteristics, configuration and managing of users and groups is also a significant part of Linux system administration.
In Linux, there are several tools or program out there to list the user accounts on the system but instead of using any user listing tool we typically prefer user listing commands which fetch the details even from the depth of the user system accounts.
But before starting, there are some things you must have access to:
- A Linux distribution must have installed and should be running.
- A user with sudo privileges, if not, you can use sudo at the beginning of each command.
- Having access to the terminal.
So let’s dive into the details of how to list users in Linux. Although, the article was accomplished using Ubuntu machine, but it’s equally usable for other Linux distributions.
The File Containing Users in Linux
As a Linux user, you use different commands. For example, to create a user, delete a user, etc but have you ever thought how to count all the users in Linux or What file contains a list of the users on a Linux system?
You may already know that all the user information is stored in etc/passwd file and if we create a user then its information will also be automatically appended to this file.
Listing All Users
In this section, We will discuss here two methods that are considered to list users in Linux.
Using etc/passwd File
Open up your terminal and type:
Note: You will be supposed to start any command with the sudo keyword if you are not using the Linux system with sudo privileges.
This command will result in listing users, see the output below:
From the image above you can clearly see the line represents the single user dividing into seven fields and distinguish with Colon(:) containing the following information:
- User name.
- The “x” indicating the password is encrypted.
- The user ID number which is generally known as UID (User identifier).
- User group ID which is known as GID (Group Identifier).
- Full user name.
- The login shell, the default is set to /bin/bash .
Using getent passwd Command
Doesn’t stop here, besides etc/passwd file, there are also other ways to list users in Linux. We all know nothing is impossible in the Linux world so if you don’t accomplish anything by attempting a single command, there are several other commands which can perform the same and sometimes with more depth results.
The getent passwd command is also used to list users in Linux. It retrieves the users listing using the /etc/nsswitch.conf file in which all the database information is stored. Here is a demo of this command telling how it actually brings the same result as in etc/passwd file:
Here, you can see it brings the same output:
Listing only User Names
If you want to list users by only their names, then you can use either cut or awk commands. They both do the job and there is no need to be confused, pick one of the following commands according to your will:
The result will be something like this:
Listing Currently Logged in Users
It has been seen that people usually inquire about how to list users currently logged in the Linux server. If you are the one who is seeking how to list the usernames of currently logged users in Linux then this section of our post has your answer.
Below are the essential commands for that:
- The w command is the one which holds the complete information of users that are currently using the machine and what processes they’re performing.
- The who command shows the complete information of users that are currently logged in.
- users displays the names of users that are currently on the system.
Below is an attempt to execute them:
Listing All Users in a Group
The /etc/group file defines the groups on the Linux system. You can simply search this file to list and find all members of a group:
And in results, it’ll show all the user groups on the system:
You can also use getent group command to do the same, and you can pipe it to grep to search for a specific username:
$ getent group | grep username
Listing Sudo Users
To list sudo privileged users, you easily do that using the following command:
$ grep '^sudo:.*$' /etc/group | cut -d: -f4
Or using getent group command:
$ getent group sudo | cut -d: -f4
In my machine, I have only one sudo user, but this will list all sudo available users in the system.
Listing Locked Users
The following command is responsible for listing locked users:
$ passwd -S -a | grep LK | cut -d " " -f1
Just a side note, you can lock a user using usermod -L username command, so you can test it out.
Conclusion
After you’ve completed reading this tutorial, you’ll be able to list the users in any Linux system and you should have an idea about how multiple user listing commands work and how users are stored in a Linux machine.
Tags
Recent Posts
How to List Users in Linux
User management is a critical Linux system administration task. In large organizations, having insight into who has access to the system is crucial to correctly add users, remove users, and assign new user privileges.
This tutorial will show you how to list users on a Linux-based system. The guide provides four listing methods and explains essential concepts related to user administration.
Listing Users in Linux
Linux stores information about local users in the /etc/passwd file. Each line in the file contains information about a single user, including their username, user ID number (UID), home directory, and the login shell.
The following sections present multiple ways to access the data in /etc/passwd and list users on Linux distributions.
The commands used in the tutorial are:
Note: To display a list of the logged-on users and the information such as boot time, processes, hostnames, and more, use the who command.
List Users with cat Command
The cat command provides a straightforward way to list the contents of the /etc/passwd file.
The system outputs the entire file with all the users on the system.
To view the number of users only, pipe the output of the previous command to the wc command and make it count the number of lines:
The number of lines in /etc/passwd corresponds to the total number of users.
List Users with Terminal Pagers less and more
On systems with many users, it is useful to limit the /etc/passwd file output displayed at once. Use a terminal pager command, such as less or more , to browse through the file content line by line or page by page.
To open /etc/passwd using less , enter:
The first page of the file appears in the output. The list stops when it reaches the end of the terminal screen. Use the keyboard to navigate through the file.
Use more to get a similar result. This command is older and has a more limited set of functionalities:
List Users with awk Command
Use the awk command to list the usernames only, without additional information about each user. Since the data fields in /etc/passwd are separated by a colon symbol, the following syntax tells awk to output only the first field in each line:
Combine awk and less for a page-by-page view of the results.
List Users with getent Command
The getent command searches and displays system database entries. The searchable databases are listed in the /etc/nsswitch.conf file. By default, the file includes the passwd database.
List the entire contents of the passwd database by typing:
The output is the same as the output of the cat command.
However, you can use getent to look up specific users. To do so, use the following syntax:
If the user exists on the system, the command shows the related passwd entry line.
Listing Normal and System users in Linux
Linux-based systems have two types of users — system and normal users.
- System users are entities created by the system to run non-interactive processes, i.e., the processes that run in the background and do not require human interaction. The most important system user is root, which possesses administrative privileges.
- Normal users are human users created by root or another user with root privileges. Each normal user has a login shell and a home directory to store their files.
Both system and normal users in Linux have a unique user ID (UID) to identify them. System users have UIDs in the range from 0 (root user) to 999. Normal users typically receive UIDs from 1000 onwards, with each newly created user receiving the next smallest unused UID.
To check the UID range for normal users, use the grep command to search for the information stored in /etc/login.defs :
grep -E '^UID_MIN|^UID_MAX' /etc/login.defs
The output in this example shows that the smallest UID a normal user can receive is 1000, and the largest is 60000.
Use getent to search the passwd database by UID:
The output shows the user entry related to the UID.
Use UIDs in combination with getent to search for users in a range:
The command now lists all the users within the specified UID range.
This guide showed you how to list all Linux users, search for users, and find the number of Linux users in any Linux distribution.
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:
- 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.
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.
- -F: Lets awk know about which field separator to use, which, in this case, is “:”
- ‘’: 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:
To get the data of a specific user, you can use the following command.
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.
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.