The Ultimate Guide to Linux List Users: Essential Commands and Techniques

The Ultimate Guide to Linux List Users: Essential Commands and Techniques

A typical Linux server could have anywhere between one to thousands of users. Server administrators need to know how to find out the users on their systems because managing users is a crucial task in Linux server administration. It involves adding, removing, and assigning new user privileges. This article introduces four ways you can use to list users on your system.

Let’s start with an overview of the need for listing users on a Linux system.

When it comes to user management, Linux is very flexible, and administrators can list and collect them in various permission groups.

For instance, Linux emphasizes multiple security measures to prevent users' personal data breaches. For instance, the path “/etc/passwd” contains information about the local users. Each row represents a single user's data, including the user's name & id, home directory, and login information.

Because of the importance of listing users on a Linux system, Linux provides several methods that get the job done.

We’ll explore these methods in detail in this tutorial. But before that, let’s first see why listing users is essential and what are the possible use cases.

Advantages of Linux list users in Linux System

Here’re the benefits of listing users in a Linux system;

  1. System administration: You can list all users in the system as system administrators to check their account status, verify their permissions, and perform maintenance tasks such as password resets and account deletion.
  2. Troubleshooting: You can use the list of users to identify any conflicts or issues that may be causing problems on the system. For example, if multiple users are logged in simultaneously and experiencing performance issues, you can use the list to identify which users are running resource-intensive processes.
  3. Security: Experts suggest system administrators review system's user list regularly. This ensures no unauthorized users can access the system and that all the user accounts are secured adequately with complex passwords
  4. Collaboration: Listing all users in a Linux system can also assist in collaboration between users. You can use the list to find other users with whom you need to collaborate on specific projects or to see if particular users have system accounts.
  5. ?Learning: Finally, listing all users in a Linux system can assist you in learning more about the architecture and organization of the system. Reviewing the list of users, you can better understand how users are organized into groups and how permissions are assigned and managed in the system.

Types of Users in Linux

Linux-based systems have two distinct types of users - system and normal users.

System Users

System users are created automatically when new software or an operating system is installed. The system uses these users to run specific services and daemons.

These users usually have limited privileges and are not meant to be used interactively. In general, besides the sysadmins, other users don’t interact with these users.

Root, bin, daemon, sys, and nobody are examples of system users.

Normal Users

On the other hand, normal users are created by administrators or users and are intended to be interactive. They can log in to the system and run applications.

These users are usually assigned to one or more groups that control their access to system resources. Normal users can also create and modify files and directories in their home directories and other directories to which they have access.

In Linux, system and normal users are assigned a unique user ID (UID) for identification purposes. System users are given UIDs ranging from 0 (root user) to 999, while normal users receive UIDs starting from 1000. As new users are created, they are assigned the next available UID in descending order.

You should know that system users typically have limited privileges and no home directories, whereas normal users have more privileges and a home directory to store their files and settings.

Understanding the /etc/passwd File

A typical Linux environment has multiple users with different privileges. A critical security challenge is keeping each user’s data isolated and protected from unauthorized access.

A Linux system generally maintains the information about local users at the path "/etc/passwd". Each row in this file contains information (user name, ID, home directory, and login information) about a user.

The following image shows the information in /etc/passwd file about a user named Nick. As you can see, this contains critical information such as username, encrypted password, and password expiry dates.

How to list users in Linux

How to List Users in Linux

Now that you have a clear idea of how users are classified, let’s move on to the different methods you can use to list users in Linux.

Prerequisites

Like most things in Linux, listing users in Linux has the following prerequisites:

  • A machine running any Linux distribution
  • A user with administrator privileges

List Users by Using the Cat Command

The cat command is a shortened form for concatenation. It reads the data from a file without opening it. It can also generate new files and add (append) information to them.

Here’s the syntax for the cat command. Remember to replace file_name with the name of the file.

When listing users, we’ll use the cat command to list all users on Linux. Below is the syntax for implementing the "cat" command, where "file_name" is replaced with the actual name of the file to be read.

$ cat file_name        

Now, as mentioned earlier, Linux stores information about local users in the /etc/passwd file. We can use the cat command to list the users in the terminal:

$ cat /etc/passwd        

Upon executing this command, you’ll get similar output, which includes a list of the users. Each row in the table displays details for a user, including their username, UID, home directory, and login shell.

If we want to see the number of users allowed to use the system, we can use the following command:

$ cat /etc/passwd | wc –l        

In the above command, the "wc" command counts a file's lines, words, or bytes. In this instance, we'll count the rows of user data. Next, we used "-l" to indicate that the lines are counted.

After running the command above, we obtained the number of users whose data is stored in the file (which is 35, in our case).

If you would like to get the details of any particular user, you can grep the username with the cat command as shown below.

cat /etc/passwd |grep user_name        

List Users by Using “less” or “more”

"less" and "more" are two other techniques for listing user information stored in any file.

These two terminal pager commands allow us to read files line by line or page by page.

The syntax for using the "less" command to list users from any file is shown below.

$ less /etc/passwd        

In the snippet, we successfully executed the list of users stored in the /etc/passwd file. A user list may be read using the less command as well. As it reaches the end of the terminal, it will display the users till we scroll the terminal down using the down button to display the remaining data from the file.

The syntax for the "more" command to list the users is shown below.

$ more /etc/passwd        

This command has limited capabilities. For instance, it displays only a percentage of the file’s content (as shown in the screenshot below).

Pressing the "enter" key displays more of the file's contents.

List Users by Using the awk Command

When you cat or less the /etc/passwd file, you get all the information stored in the file. As such, you need to sort through a lot of data to get the desired information.

Luckily, there are different hacks that you can use to list only a specific field.

The "awk" command offers an alternative method of listing system users. The command comes in handy when you simply need to display the user's name and ignore all other data that may be included in the file.

Here’s the command's syntax for just listing the user's name:

$ awk -F: '{print $1}' /etc/passwd        

The colon ":" is used in the syntax to separate the input from the awk.

It displays the first value of each row where -F is a file, and the argument is responsible for opening the file, reading the contents, and printing the result.

Here’s how the output of the command looks like in our test terminal:

If the file is particularly large with multiple pages, you can pipe the output of the awk command into the less command to display the contents screen by screen.

$ awk –F: ‘{print $1}’ /etc/passwd | less        

List Users by Using the getent Command

The "getent" command is similar to the "cat" command in that it displays all the information. The "getent" also displays all the users' information.

The getent command is used to search and display entries in the system databases, which are listed in the /etc/nsswitch.conf file. Note that the passwd database and other system databases are included by default.

Here’s the syntax of the getent command.

$ getent passwd        

You can see that we provided passwd, the database of the users. However, you can supply any other file to the command (useful if you have user information in other files).

The entire user data is displayed as the output of the command, as shown in the screenshot below.

However, you can use getent command to look up specific users. To do so, use the following syntax:

$ getent passwd [username].        

You can also search based on the UID.

$ getent passwd [UID]        

Or, if you want to list a range of users with UID, please check the command below.

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

Conclusion

In this article, we introduced a list of concurrent users utilizing the same system. Managing multiple users at the same time is an administrative task. We also investigated some of the user recruitment strategies in Ubuntu 20.04. As we all know, managing users is necessary, but first, it is critical to understand who is using the system to make it secure for others and prevent data breaches between all users.

RedSwitches offer the best dedicated server pricing and deliver instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.

FAQ-Linux list users

Q: How can I list all users in Linux using the command line?

A: You can list all users in Linux by using the "getent" or "cut" command with the "/etc/passwd" file or by using the "awk" command with the "/etc/passwd" file

Q: Can I list only the usernames without additional information?

A: Yes, you can use the "cut" command with the "-d" and "-f" options to list only the usernames without additional information.

Q: How can I list only the currently logged-in users?

A: You can use the "who" command to list all currently logged-in users or the "w" command to get detailed information about currently logged-in users.

Q: How can I list all users, including system users?

A: You can use the "getent" command with the "/etc/passwd" file to list all users, including system users.

Q: Can I sort the list of users in alphabetical order?

A: Yes, you can use the "sort" command with the "-k" option to sort the list of users alphabetically based on the username.

Q: What is the difference between a Root User and a Normal User?

A: The root user is essentially the same as the administrator account in Windows; it has full access to the system and can access all files and capabilities. On Linux, regular users ( Normal Users) have fewer privileges. For instance, they cannot install applications or modify system folders.

Q: Where are user details stored in a Linux Operating System?

A: Linux creates a user for every person and service on the system. Information about these users is stored in a file called /etc/passwd.

Q: How can I list users in Linux using the 'getent' command?

A: The command to list users in Linux is: getent passwd

Q: What is a system user?

A: A system user is a user account used for system administration. They typically have a UID (User Identifier) lower than 1000.

Q: How can I use Linux's 'cat' function to list every user?

A: Use cat /etc/passwd to print the passwd configuration file.

Q: How can I determine if a user is present on the Linux system?

A: Use the 'cut' and 'grep' commands to determine whether a user is present on a Linux system. A typical usage would be: grep "^: " /etc/passwd | cut -d': ' -f1

Q: What distinguishes a system user from a typical user?

A: A system user is a user account for system administration purposes, while a normal user is a regular account created for individuals to perform their tasks on the system.

Q: How can I find users in Linux using the 'awk' command?

A: You can use the following command to get a list of all users using the 'awk' command in Linux: awk -F': ' '{ printf "%s\\n", $1 }' /etc/passwd

要查看或添加评论,请登录

社区洞察

其他会员也浏览了