Chat Zalo Chat Messenger Phone Number Đăng nhập
How to List Users in Linux {4 Methods Explained} - phoenixNAP

How to List Users in Linux {4 Methods Explained} – phoenixNAP

Introduction

User administration is a critical Linux system administration task. In large organizations, having information about who has access to the system is crucial to correctly adding users, removing users, and assigning 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 management.

Prerequisites

  • A system running
  • Linux.

  • Access to the terminal/command line

.

The list

of users in Linux

Linux stores information about local users in the /etc/passwd file. Each line of the file contains information about a single user, including their user name, user ID number (UID), home directory, and logon shell.

The following sections introduce several ways to access data in /etc/passwd and list users in Linux distributions.

The

commands used in the tutorial are

: The cat command The minus command The

  • awk
  • command The

  • getent command List

of users with cat command The cat

command

provides an easy way to enumerate the contents of the /etc/passwd file. To view the file, type : cat /etc/passwd The

system generates the

complete file with all

users of the system. To see only the number of

users, pipe the output of the previous command to the wc command and have 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 of users with

terminal pagers more and more

On systems with many users, it is useful to limit the output of the /

etc/passwd

file

displayed at one time. Use a terminal locator command, such as minus or more, to browse the contents of the file line by line or page by page.

To open /etc/passwd

using less, type: 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 functionality: plus

/etc/passwd

List of users

with awk Command

Use the awk command to list only user names, with no additional information about each user. Because the data fields in /etc/passwd are separated by a colon, the following syntax instructs awk to generate only the first field on each line: awk -F’:’ ‘{ print $1}’ /etc/passwd Combine awk and less to get a page-by-page view of the results. awk -F’:’ ‘{ print $1}’ /etc/passwd

|

less

List of users with the

getent command

The getent command finds 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

.

However, you can use getent to search for specific users. To do this, use the following syntax

: getent passwd [user name] If the user exists on the system, the

command displays the related passwd input line

. List of normal and system users in Linux

Linux-based systems have two types of users:

system users and normal.

  • System users are entities created by the system to run non-interactive processes, that is, processes that run in the background and do not require human interaction. The most important user of the system is root, which has administrative privileges.
  • Normal users are human users created by root or another user with root privileges. Every normal user has a login shell and home directory to store their files.

Both system users and regular Linux users have a unique user ID (UID) to identify them. System users have UIDs in the range of 0 (root user) to 999. Normal users typically receive UIDs starting at 1000, with each newly created user receiving the next smallest unused UID.

To check the UID range for normal users, use the grep command to find the information stored in /etc/login.defs: grep -E ‘^UID_MIN|^UID_MAX’ /

etc/login.defs

The output of this example shows that the smallest UID

that 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 displays user input related to the

UID. Use UID in

combination with getent to search for users in a range: getent

passwd {[first-UID].. [last-UID]}

The command now lists all users within the specified UID range.

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.

Contact US