An essential Linux system administration task is user management. Understanding How to List Users in Linux? who has access to the system is essential in large organizations in order to properly add users, remove users, and assign new user privileges.
You will learn how to list users in Linux-based systems through this article. The manual outlines key user administration ideas and offers four listing ways.
- A system running Linux.
- Access to the terminal/command line.
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 login shell.
The following sections present multiple ways to access the data in /etc/passwd and list users on Linux distributions.
How to List Users in Linux? The commands used in the tutorial are:
The cat command
The less command
The awk command
The getent command
List Users with cat Command
The cat command provides a straightforward way to list the contents of the /etc/passwd file.
To view the file, type:
cat /etc/passwd
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:
cat /etc/passwd | wc -l
The number of lines in /etc/passwd corresponds to the total number of users.
List Users with Terminal Pagers less and more
How to List Users in Linux? 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:
less /etc/passwd
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:
more /etc/passwd
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:
awk -F':' '{ print $1}' /etc/passwd
Combine awk and less for a page-by-page view of the results.
awk -F':' '{ print $1}' /etc/passwd | less
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.
How to List Users in Linux? List the entire contents of the passwd database by typing:
getent passwd
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:
getent passwd [username]
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 formed by the system to carry out non-interactive procedures, i.e., background operations that don’t involve interacting with users. The most significant system user, root, has administrative rights.
- Normal users are created by root or another user with root access. They are actual people. A login shell and a home directory for storing files are provided for every typical user.
Linux assigns each user a unique user ID (UID) that serves as their identification. UIDs for system users vary from 0 (the root user) to 999. UIDs for regular users normally start at 1000, and the smallest unused UID is assigned to each newly created user.
Use the grep command to look through the data kept in /etc/login and check the UID range for common users.def:
grep -E '^UID_MIN|^UID_MAX' /etc/login.defs
How to List Users in Linux? 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:
getent passwd [UID]
The output shows the user entry related to the UID.
Use UIDs in combination with getent to search for users in a range:
getent passwd {[first-UID] ...[last-UID]}
The command now lists all the users within the specified UID range.
Conclusion
This post demonstrated how to find the total number of Linux users in any Linux distribution, and how to List Users in Linux. and do a user search.
Then, discover how to list scheduled cron jobs for specified users and Linux file permissions.