How to List Users in Linux

Introduction

Welcome to this tutorial on how to list users in Linux. Linux is a powerful and versatile operating system that is used by many people around the world. It is important to know how to list users in Linux, as it can be used to manage user accounts and permissions. In this tutorial, we will discuss the different ways to list users in Linux, including using the command line, graphical user interface, and other tools. We will also discuss how to view user information and how to add and delete users. By the end of this tutorial, you should have a good understanding of how to list users in Linux.

How to List Users in Linux

1. Use the ‘cat’ command to view the contents of the ‘/etc/passwd’ file. This file contains a list of all users on the system.

2. Use the ‘getent’ command to view the contents of the ‘/etc/passwd’ file. This command is more efficient than ‘cat’ and can be used to query other system databases as well.

3. Use the ‘id’ command to view the user ID (UID) and group ID (GID) of a specific user.

4. Use the ‘groups’ command to view the groups a user belongs to.

5. Use the ‘finger’ command to view information about a specific user, such as their full name, home directory, shell, and more.

6. Use the ‘who’ command to view a list of users currently logged into the system.
[ad_1]

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

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.

[ad_2]

How to List Users in Linux

Linux is a powerful operating system that allows users to manage their system with ease. One of the most common tasks that users need to do is to list all the users on their system. This can be done in a few different ways, depending on the type of Linux system you are using.

Using the /etc/passwd File

The /etc/passwd file is a text file that contains information about each user on the system. This file can be used to list all the users on the system. To do this, open a terminal window and type the following command:

cat /etc/passwd

This will display a list of all the users on the system. Each line in the file contains information about a single user, such as their username, home directory, and shell.

Using the getent Command

The getent command is a utility that can be used to query the system’s user database. To list all the users on the system, type the following command in a terminal window:

getent passwd

This will display a list of all the users on the system. Each line in the output contains information about a single user, such as their username, home directory, and shell.

Using the id Command

The id command is a utility that can be used to display information about a specific user. To list all the users on the system, type the following command in a terminal window:

id -a

This will display a list of all the users on the system. Each line in the output contains information about a single user, such as their username, home directory, and shell.

Conclusion

Listing users in Linux is a simple task that can be done in a few different ways. The /etc/passwd file, the getent command, and the id command can all be used to list all the users on the system. Once you know how to list users in Linux, you can easily manage your system with ease.

Jaspreet Singh Ghuman

Jaspreet Singh Ghuman

Jassweb.com/

Passionate Professional Blogger, Freelancer, WordPress Enthusiast, Digital Marketer, Web Developer, Server Operator, Networking Expert. Empowering online presence with diverse skills.

jassweb logo

Jassweb always keeps its services up-to-date with the latest trends in the market, providing its customers all over the world with high-end and easily extensible internet, intranet, and extranet products.

GSTIN is 03EGRPS4248R1ZD.

Contact
Jassweb, Rai Chak, Punjab, India. 143518
Item added to cart.
0 items - 0.00