In terms of Linux system administration activities, user management is considered a crucial aspect. Moreover, knowing the methods to list all users in a Linux system is a highly desired and valuable skill.
Today, we’ll show you various methods to list users on your Linux system, ensuring you know how to manage user accounts effectively.
Table of Contents
How to List All Users in Linux?
Listing users in a Linux system is a task that can be accomplished with various commands.
To list all users and their details, follow these methods:
Method 1: Using cat Command
The most straightforward method to list all users is by displaying the contents of the /etc/passwd file. This file comprises one line for each user account which display essential details like:
- Username
- User ID (UID),
- Group ID (GID),
- Home directory
- Login shell, etc.
To view the entire list of users, use the cat command by mentioning the /etc/passwd file:
cat /etc/passwd
Note: For a more manageable view, especially on systems with numerous users, use the less or more commands.
Method 2: Using awk Command
If you want to list only the usernames without additional details, using the awk command is the easiest way. By specifying the field delimiter and the desired field, you can extract just the usernames:
awk -F':' '{ print $1 }' /etc/passwd
Method 3: Using getent Command
The getent command is another powerful tool that queries system databases, including the user accounts database.
To list all users with getent, simply type:
getent passwd
Note: This command is particularly useful for systems that use a centralized user management system. It lists all users, not just those in the /etc/passwd file.
Method 4: Using compgen Command
For a quick list of all user names without additional details, you can use the compgen command:
compgen -u
Method 5: Using who Command
The who command provides a list of all users currently logged into the system. Additionally, it shows users’s login time, terminal name, and other details as well:
who
Conclusion
To list all users in a Linux system, use the cat, awk, getent, and compgen command. The cat /etc/passwd command displays the entire file, which includes information for all users. For a more concise list, the awk -F’:’ ‘{ print $1}’ /etc/passwd command can be used to extract just the usernames.
I am a passionate Linux expert with over 3+ years of experience in a wide range of distributions, including Ubuntu, CentOS, Arch Linux, Linux Mint, Debian, Fedora, and more. I love helping others learn Linux by writing clear guides that solve problems and teach new skills.