Skip to Content

How to List All Users in Linux: A Comprehensive Guide

In Linux, users are crucial components of the system. They are assigned various permissions and privileges that determine what they can do on the system. Understanding how to list all users on a Linux system is a key administrative task, whether you’re managing a single machine or a large network of servers.

This article will explore the different methods you can use to list all users in Linux, explain the underlying files that store user information, and discuss how to retrieve this data using commands and tools that come with most Linux distributions.

Understanding Users in Linux

In Linux, every user is represented by a user account. These accounts can belong to regular users, system users, or administrative users (such as the root user). Each user has an associated user ID (UID) and a group ID (GID). The system keeps track of user details in a number of important configuration files.

The most common files that store user information in Linux are:

Get Your Free Linux training!

Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!
  • /etc/passwd: Contains basic information about all user accounts.
  • /etc/shadow: Stores encrypted user passwords.
  • /etc/group: Contains information about user groups.
  • /etc/gshadow: Stores group-related password information.

The /etc/passwd file is particularly important when listing users because it contains the basic details for each account, such as the username, UID, GID, home directory, and default shell.

Listing Users with Commands

There are several commands available in Linux to list users. Below are some of the most common and useful ones.

cat /etc/passwd

The simplest method for listing all users on a Linux system is to view the contents of the /etc/passwd file. This file stores information about each user account, with one line per user. You can use the following command to display it:

cat /etc/passwd

Each line in this file is structured as follows:

username:password:UID:GID:GECOS:home_directory:shell
  • username: The user’s login name.
  • password: A placeholder (usually an x or *), as actual passwords are stored in /etc/shadow.
  • UID: The unique user ID for the account.
  • GID: The primary group ID associated with the user.
  • GECOS: Full name or other information about the user.
  • home_directory: The path to the user’s home directory.
  • shell: The login shell used by the user (e.g., /bin/bash).

To extract just the usernames from the /etc/passwd file, you can use the following command:

cut -d: -f1 /etc/passwd

This command uses cut to split the file by the colon (:) delimiter and display only the first field (the username).

getent passwd

Another method to list users is using the getent command. getent fetches entries from system databases, and when used with the passwd argument, it lists users in a way that includes both local and networked accounts (e.g., users from an LDAP directory if configured).

getent passwd

This command behaves similarly to cat /etc/passwd, but it can provide a more comprehensive list of users from various sources.

Again, to list just the usernames, you can use the cut command:

getent passwd | cut -d: -f1

awk Command

For more advanced manipulation, the awk command is a powerful tool to extract and format information. You can use it to display the list of usernames as follows:

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

Here, -F: sets the field separator to a colon (:), and { print $1 } tells awk to print only the first field, which is the username.

cut -d: -f1 /etc/passwd | sort

If you want to list users in alphabetical order, you can combine the cut and sort commands as follows:

cut -d: -f1 /etc/passwd | sort

This command will list usernames alphabetically, making it easier to scan through the list.

Understanding System Users vs. Regular Users

In Linux, users are divided into two categories:

  • Regular users: These users are created by administrators for people who use the system, typically having UIDs starting from 1000 and above (this may vary depending on the distribution).
  • System users: These users are created by the system for running services and processes, usually having UIDs below 1000.

To list only regular users, you can filter the /etc/passwd file by excluding system accounts (those with UIDs below 1000):

awk -F: '$3 >= 1000 { print $1 }' /etc/passwd

This command uses awk to check the third field (UID) and only prints usernames with UIDs greater than or equal to 1000.

Listing System Users

To list only system users, you can use a similar approach by filtering out regular users:

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

This will display only system users with UIDs less than 1000.

Listing Users with id Command

The id command can be used to display the current user’s UID and GID, along with group memberships. However, it is not typically used for listing all users. Instead, it’s more useful for retrieving user details for a specific user.

To get the UID and GID of a particular user, run:

id username

For example:

id root

This will display the UID, GID, and group memberships of the root user.

Conclusion

Listing all users on a Linux system is an essential administrative task that can be accomplished using several different methods.

Whether you’re looking for regular users, system users, or just need a simple list of usernames, there are a variety of commands at your disposal, such as cat /etc/passwd, getent passwd.

Understanding the underlying files and commands is key for efficient system management, especially when you need to manage users, configure permissions, or monitor user activity. By using these methods, you can easily retrieve and filter user information, which is crucial for both security and system maintenance.