Linux - How to list all users and their UIDs
Categories:
Linux: How to List All Users and Their UIDs
Learn various command-line methods to retrieve a comprehensive list of users and their User IDs (UIDs) on a Linux system, essential for system administration and security auditing.
Understanding user accounts and their associated User IDs (UIDs) is fundamental for Linux system administration. UIDs are unique identifiers assigned to each user, crucial for file permissions, process ownership, and overall system security. This article explores several command-line utilities and techniques to effectively list all users and their UIDs, ranging from simple file inspection to advanced filtering with awk
and grep
.
Understanding /etc/passwd
The primary source of user account information on a Linux system is the /etc/passwd
file. This plain-text file contains one line per user account, with fields separated by colons. While it doesn't store passwords (those are in /etc/shadow
), it holds vital details including the username, UID, GID (Group ID), user's full name or comment, home directory, and default shell. Knowing its structure is key to extracting user data.
flowchart LR A[/etc/passwd File] --> B[Line per User] B --> C["Field 1: Username"] B --> D["Field 2: Password Placeholder (x)"] B --> E["Field 3: UID"] B --> F["Field 4: GID"] B --> G["Field 5: User Info (GECOS)"] B --> H["Field 6: Home Directory"] B --> I["Field 7: Default Shell"] E -- UID is here --> J[User ID]
Structure of the /etc/passwd file
Method 1: Using cat
and awk
The most straightforward way to view user information is by reading the /etc/passwd
file directly. We can then use awk
to parse the colon-separated fields and extract only the username and UID. The username is the first field, and the UID is the third field.
cat /etc/passwd | awk -F: '{print $1 ":" $3}'
List all users and their UIDs using cat
and awk
.
-F:
option in awk
specifies that the colon (:
) should be used as the field separator. $1
refers to the first field (username) and $3
to the third field (UID).Method 2: Using getent
The getent
command is a more robust and recommended way to query user databases. It retrieves entries from Name Service Switch (NSS) databases, which can include local files (/etc/passwd
), NIS, LDAP, and other sources. This ensures you get a complete picture, even in networked environments. We can pipe its output to awk
for formatting.
getent passwd | awk -F: '{print $1 ":" $3}'
List all users and their UIDs using getent
and awk
.
Method 3: Filtering for Human Users (UID >= 1000)
On most modern Linux distributions, system accounts typically have UIDs below 1000 (e.g., root
is 0, daemon
is 1, bin
is 2, etc.). Regular human users usually start with UID 1000 or higher. To filter out system accounts and only list interactive users, we can add a conditional check in awk
.
getent passwd | awk -F: '$3 >= 1000 {print $1 ":" $3}'
List human users (UID >= 1000) and their UIDs.
Method 4: Using grep
for Specific Users
If you need to find the UID of a specific user or a pattern of users, grep
can be combined with getent
or cat
to filter the output. This is useful for quickly locating information for a known user.
getent passwd | grep "^username:" | awk -F: '{print $1 ":" $3}'
# Replace 'username' with the actual username you are looking for
# Example for user 'john':
# getent passwd | grep "^john:" | awk -F: '{print $1 ":" $3}'
Find the UID of a specific user using grep
and awk
.
Method 5: Using id
command for current or specific user
The id
command provides detailed information about a user, including their UID, GID, and group memberships. While it doesn't list all users, it's excellent for quickly checking the current user or a specified user's details.
# To see your own UID:
id -u
# To see UID and other info for a specific user:
id username
# Example for user 'john':
id john
Using the id
command to get user information.
These methods provide a comprehensive toolkit for listing users and their UIDs on a Linux system. Whether you need a full list, filtered results, or details for a specific user, the combination of cat
, getent
, awk
, grep
, and id
covers most scenarios.