Which command show users that are logged in linux

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.

Which command show users that are logged 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 /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:

  • The cat command
  • The less command
  • The awk command
  • The getent command

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.

Which command show users that are logged in linux

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.

Which command show users that are logged in linux

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.

Which command show users that are logged in linux

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

more /etc/passwd

Which command show users that are logged in linux

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

Which command show users that are logged in linux

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.

Which command show users that are logged in linux

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.

Which command show users that are logged 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.

Which command show users that are logged in linux

Use getent to search the passwd database by UID:

getent passwd [UID]

The output shows the user entry related to the UID.

Which command show users that are logged in linux

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.

Which command show users that are logged 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.

How do you check users who logged in Linux?

To check information about users who are currently logged into the system, we use the who command in the Linux system. The who command is used to display the users logged into the system.

What is the command to view all currently logged in users?

The “whoami” command displays the user you are currently logged in and using in Windows. Hold down the Windows Key, and press “R” to bring up the Run window.