As a Linux system administrator, you might need to retrieve a list of all users on your system. Linux stores user information in the /etc/passwd file, which contains details such as usernames, user IDs (UID), group IDs (GID), home directories, and login shells.

This guide will cover multiple methods to list users in Linux, applicable to Ubuntu, Debian, RHEL, CentOS, AlmaLinux, RockyLinux, Fedora, Arch, and other Linux distributions.


1. List All Users Using /etc/passwd File

The /etc/passwd file contains all user accounts on the system. To display its contents, use the following command:

cat /etc/passwd

Example Output

root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
mysql:x:5:60:games:/usr/mysql:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh

Each line represents a user, with the following fields separated by colons (:):

  1. Username
  2. Encrypted password (stored in /etc/shadow)
  3. User ID (UID)
  4. Primary Group ID (GID)
  5. User Information (GECOS field)
  6. Home Directory
  7. Login Shell

To display the first or last 5 users:

head -5 /etc/passwd
tail -5 /etc/passwd

2. List Only Usernames

If you want to display only the usernames, use the following commands:

Using awk

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

Using cut

cut -d: -f1 /etc/passwd

Output Example

root
daemon
bin
sys
sync
games
mysql
redis
man

3. List Users Using getent Command

The getent command retrieves user information from system databases, including /etc/passwd and other sources like LDAP.

getent passwd

To list only usernames:

getent passwd | cut -d: -f1

To check if a specific user exists:

getent passwd username

Example:

getent passwd carrero

4. Count the Number of Users

To count the total number of user accounts, use:

getent passwd | wc -l

Or:

cat /etc/passwd | wc -l

5. Find If a User Exists

To check whether a user exists, use:

getent passwd username

Example:

getent passwd carrero && echo "User exists" || echo "User does not exist"

Another method using grep:

cat /etc/passwd | grep '^carrero:'

Using compgen:

compgen -u | grep carrero

6. Find System and Regular Users

System users usually have a UID below 1000, while regular users have UIDs starting from 1000.

To check the UID range:

grep "^UID_MIN" /etc/login.defs
grep "^UID_MAX" /etc/login.defs

Example output:

UID_MIN    1000
UID_MAX    60000

List Only Regular Users

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

List System Users

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

7. Using compgen to List Users

compgen -u

This will print only usernames.


8. List Currently Logged-In Users

To see the users currently logged into the system, use:

who

or:

w

Example output:

carrero    pts/1    18t.2xx.2x.1z   11:37    0.00s  0.01s  0.00s gcc

To check login details of a specific user:

w carrero

To view system uptime and logged-in users:

uptime

Example output:

17:25:56 up 51 days, 1 user, load average: 0.00, 0.00, 0.00

9. Using a Script to List Normal Users

The following script lists all regular users who can log in:

#!/bin/bash
# List all normal user accounts (UID >= 1000)

UID_MIN=$(grep "^UID_MIN" /etc/login.defs | awk '{print $2}')
UID_MAX=$(grep "^UID_MAX" /etc/login.defs | awk '{print $2}')

awk -F':' -v min=$UID_MIN -v max=$UID_MAX '{ if ($3 >= min && $3 <= max && $7 != "/sbin/nologin") print $1 }' /etc/passwd

10. List Both System and Normal Users

#!/bin/bash
# List all normal users and system users

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

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

Sample Output:

---- Normal Users ----
smith
carrero
colorvivo

---- System Users ----
root
bin
daemon
adm
lp
sync

11. Find Users with Login Disabled

Some accounts are disabled for security reasons, such as nobody, daemon, or apache. To find such users:

awk -F':' '{ if ($7 == "/sbin/nologin") print $1 }' /etc/passwd

Example Output

nobody
sshd
apache
mysql

Conclusion

You now know multiple ways to list users in Linux, including:

  • Reading from /etc/passwd
  • Using getent
  • Filtering by UID for system vs. regular users
  • Counting users
  • Checking if a user exists
  • Listing currently logged-in users

For more details, refer to the manual pages:

man passwd
man getent
man awk
man cut
help compgen

These methods provide a comprehensive approach for managing and listing users in Linux. Try them out and optimize your system administration workflow! 🚀

Scroll to Top