How to List Users in Linux? (Debian List Users)

Linux distributions have become popular among technology-based companies, researchers, and large application development communities. Managing user accounts within team-based projects is essential for efficient collaboration in such environments. In Linux systems, administrators can seamlessly interact with the system and oversee user management through various commands.

Among the several Linux distributions, Ubuntu stands out as one of the most widely used, offering an array of features tailored for system administrators. One crucial aspect of system administration is listing all users and effectively managing their tasks. This facilitates granting privileges, such as access to files or folders, and assigning permissions to ensure the security of the system's data.

In this article, we will demonstrate how to list users in Linux using the command-line interface. All commands discussed here are demonstrated on Ubuntu 22.04 LTS Jammy JellyFish distribution. Let's explore these commands to handle the user management processes on Ubuntu systems effectively.

How to List Users in Linux? (List Linux Users)

There are two primary methods available to see how to list users in Linux distributions.

  1. List users in Linux by displaying the /etc/passwd file content.
  2. List users in Linux using the getent Command.

List Users in Linux by Displaying the /etc/passwd File Content (Linux Command List Users)

The first method involves examining the contents of the /etc/passwd file, where details of all local users are stored. Users can access the contents of the /etc/passwd file using commands like cat or less:

$ cat /etc/passwd

how to list users in linux? (debian list users)

Or

$ less /etc/passwd

Each line in the /etc/passwd file represents information about one local user. These lines are divided into fields separated by colons. Important information displayed for each user includes the following:

  • The user's login name;
  • The encrypted password (typically represented by 'x,' as the actual password is stored in the /etc/shadow file);
  • User identification number (UID);
  • User's group identification number (GID);
  • User's full name (often referred to as GECOS);
  • User's main directory or home directory;
  • The default login shell is typically set to /bin/bash.

By observing the contents of the ‘/etc/passwd’ file, administrators can gain insights into the users registered on the system and their associated details. This method offers a straightforward way to list users in Linux.

List Linux Users Using awk and cut Commands (Linux Commands List Users)

When you need to display only the username from the /etc/passwd file in a Linux system like Ubuntu, you can utilize either the awk or cut commands. These are powerful text manipulation tools that allow you to extract specific fields from a file.

Using the awk command, you can easily retrieve the first field, which represents the username, from the /etc/passwd file. The syntax for this command is:

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

how to list users in linux? (debian list users)

Similarly, the cut command can also be employed for the same purpose. It allows you to specify the delimiter and the field number to extract. In this case, we use ':' as the delimiter to separate fields and '1' to indicate the first field (the username):

$ cut -d: -f1 /etc/passwd

Both of these commands will display only the usernames of all users registered on your Linux system, providing a concise way to extract this specific information from the /etc/passwd file.

List Users in Linux Using the getent Command 

The getent command in Linux serves to retrieve entries from the administrative database by using specified search keys. In Ubuntu, getent collects and displays entries from the database configured in the /etc/nsswitch.conf file. Users can use this command to query and list users by targeting the passwd database.

To print the list of Ubuntu users, simply execute the following command:

$ getent passwd

how to list users in linux? (debian list users)

For users who prefer to display only the usernames, they can incorporate the 'awk' or 'cut' parameters along with the getent command. Here's how to achieve that using awk:

$ getent passwd | awk -F: '{ print $1}'

Similarly, the same output can be obtained using the cut command:

$ getent passwd | cut -d: -f1

Both of these commands with getent allow to list users in Linux or retrieve specific information such as usernames.

Check Users in Linux Using the getent and grep Commands

You can utilize the getent command to ascertain whether a specific user exists on your Ubuntu Linux system. By combining getent with grep, you can quickly search for user existence. Here's the syntax:

$ getent passwd | grep user-name

For instance, suppose we want to determine if a user named 'samreena' exists on our system. We would execute the following:

$ getent passwd | grep username

how to list users in linux? (debian list users)

If the specified user 'samreena' exists on your Ubuntu system, the command will display the login information associated with that user. However, if there's no user with that name, the command will yield no output.

Alternatively, you can directly check for user existence without using the grep command by specifying the username after getent passwd:

$ getent passwd user_name

List All Connected Users in Linux

To list all currently connected users on a Linux host, the cat command on the passwd file won't suffice. Instead, you should execute either the "who" or "users" command.

$ who 

how to list users in linux? (debian list users)

$ users

The "who" command or "users" command, when executed, will display a list of currently logged-in users on the system. These commands provide real-time information regarding user connections to the Linux host.

List Users in Linux to Display All User Accounts

To determine the total number of user accounts on a Linux system, you can utilize the 'getent passwd' command followed by 'wc -l' to count the lines. 

$ getent passwd | wc -l

how to list users in linux? (debian list users)

This command retrieves information about user accounts from the system's password database and then counts the total number of lines returned, each representing a user account.

Above, we have seen how to list users in Linux using different Linux commands. These commands help you to manage different user accounts on your Linux system.

Conclusion

Linux provides a range of commands that are invaluable tools for system administrators in managing user accounts and ensuring system security. In this tutorial, we learned how to list users in Linux. 

By leveraging commands such as 'getent passwd,' administrators can easily retrieve comprehensive details about users on their systems, empowering them to monitor access, enforce security policies, and troubleshoot any issues that may arise. 

Understanding these commands and their various arguments is essential for administrators to perform user management tasks efficiently and maintain the integrity and security of their Linux systems or Linux VPS servers. With these tools at their disposal, administrators can confidently navigate user-related aspects of system administration, contributing to a robust and well-secured computing environment.


Blog