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

Posted on

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

Linux distributions are widely used in tech companies, research, and application development. Effective user account management is crucial for team collaboration. Linux administrators can easily manage users through various commands.

Ubuntu is a popular Linux distribution with features for system administrators. Listing users and managing their tasks is a key aspect of system administration, allowing for privilege and permission management to ensure data security.

In this article, we will demonstrate
how to list users in Linux
using the command line. The commands will be demonstrated on Ubuntu 22.04 LTS Jammy JellyFish. Let’s explore these commands!

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

Two primary methods exist for listing users in Linux distributions.

  1. List users by displaying the contents of the /etc/passwd file.
  2. List users using the getent command.

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

The first method is to examine the
/etc/passwd
file, which stores details of all local users. Use commands like cat or less to access the content:

$ 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. The lines are divided into colon-separated fields. Important information includes:

  • 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 is a straightforward way to list users.

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

To display only usernames from the /etc/passwd file, use either
awk or cut
commands. These are text manipulation tools for extracting specific fields.

The
awk
command simplifies retrieving the first field (username) from /etc/passwd. Here’s the syntax:

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





The cut command can achieve the same result by specifying the delimiter and field number. Using ‘:’ as the delimiter and ‘1’ for the first field (username):

$ cut -d: -f1 /etc/passwd

Both these commands will display all usernames, providing a concise way to extract that information from /etc/passwd.

List Users in Linux Using the getent Command 

The
getent
command retrieves entries from administrative databases based on specified keys. In Ubuntu, it collects and displays entries from the database configured in
/etc/nsswitch.conf
. You can query and list users by targeting the ‘passwd’ database.

To get a list of Ubuntu users, execute:

$ getent passwd





To display only the usernames, use the
‘awk’ or ‘cut’
parameters with getent. Here’s how to use awk:

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

Similarly, use the cut command:

$ getent passwd | cut -d: -f1

Both commands with getent allow you to list users or retrieve specific information like usernames.

Check Users in Linux Using the getent and grep Commands

Use getent to check if a user exists. Combining it with grep lets you quickly search. Syntax:

$ getent passwd | grep user-name

Share this: