Kiến Thức Linux

How to List Users in Linux

1489939951Linux.jpg

Introduction

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.

How to list users in Linux.

Prerequisites

  • A system running Linux.
  • Access to the terminal/command line.

Listing Users in Linux

Linux stores information about local users in the <a href="https://phoenixnap.com/kb/etc-passwd" target="_blank" rel="noreferrer noopener">/etc/passwd</a> 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.

To view the file, type:cat /etc/passwd

The system outputs the entire file with all the users on the system.

The output of the cat command used on passwd file.

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.

Counting the number of lines in the passwd file with the cat and wc commands.

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:  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.

Using the less command to control the amount of output on the screen.

Use more to get a similar result. This command is older and has a more limited set of functionalities:more /etc/passwd

Using the more command to control the amount of output on the screen.


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

Displaying only the usernames using the awk command.

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.

List the entire contents of the passwd database by typing:getent passwd

The output is the same as the output of the cat command.

The ouput of the getent passwd 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.

Searching the passwd base by username using the getent command to list users in Linux.

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.

Using the grep command to check the smallest and largest UID a normal user can receive.

Use getent to search the passwd database by UID:getent passwd [UID]

The output shows the user entry related to the UID.

Searching the user according to their UID using getent.

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.

Searching the passwd database by providing a UID range, using getent to list users in Linux.

Conclusion

This guide showed you how to list all Linux users, search for users, and find the number of Linux users in any Linux distribution.

Next, learn about Linux file permissions and how to list scheduled cron jobs for specific users.

Đăng ký liền tay Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !

Add Comment

Click here to post a comment

Đăng ký liền tay
Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !