How to List Users in Linux: A Complete Guide
(How to List Users in Linux) Both personal users and enterprises extensively utilize Linux because it represents an open-source operating system. System administrators need to handle user account management as one of their essential administrative obligations. System resource access management combined with problem troubleshooting and policy compliance checks depends on the ability to list users in Linux.
This guide comprises detailed instructions about listing Linux users at different levels. The system offers three methods to list user accounts through commands and file inspection and criteria filters.
Understanding Users in Linux
Before diving into the methods of listing users in Linux, it’s essential to understand how user accounts are structured. A Linux system has three main types of user accounts
- Root user: The system administrator, also called the “superuser.” The root user has unrestricted access to the system, allowing the creation, deletion, and modification of other user accounts and system setting
- System users: These are special accounts used by system processes or services. These users typically have limited access and are not intended to log in directly.
- Regular users: These accounts are typically created for human users who interact with the system. Regular users have more restricted access compared to the root user.
Users are stored in the /etc/passwd
file, where each line represents a different user account, and the file contains important information about each account.
Method 1: Listing Users Using the cat
Command
One of the simplest ways to list users in Linux is by reading the /etc/passwd
file. This file contains details about all user accounts on the system.
To list all users in Linux, run the following command in the terminal:
This will display a list of all users, including the system users. Each line in the /etc/passwd
file corresponds to a user account and is formatted as follows
- username: The name of the user.
- password: The user’s password (often represented as
x
if password is stored elsewhere). - UID: The unique user ID.
- GID: The primary group ID.
- GECOS: A comment field that may contain the user’s full name or description.
- home_directory: The path to the user’s home directory.
- shell: The user’s default shell (e.g.,
/bin/bash
).
Method 2: Using the awk
Command to Filter Usernames
If you only want to display the usernames, you can use the awk
command to extract just the first field (the username) from the /etc/passwd
file.
This command uses awk
with the -F:
option to specify the delimiter (colon), then prints the first field, which is the username.
Method 3: Listing Users with getent
The getent
command is a useful tool for querying the system databases, including user information. It can be used to list all users by querying the passwd database.
This command will return a list similar to the output of cat /etc/passwd
, but it can also include users from external sources like LDAP or NIS (Network Information Service), if configured. This makes getent
a more reliable choice for systems with centralized authentication.
To list just the usernames using getent
, you can use awk
in combination with the command:
Method 4: Using the cut
Command
The cut
command is another method for extracting specific fields from a file. You can use it to extract usernames from the /etc/passwd
file.
This command specifies the delimiter as :
(colon) and extracts the first field (the username) from each line in the /etc/passwd
file.
Method 5: Listing Only Human Users (Excluding System Users)
System users often have user IDs (UIDs) less than 1000, while human users typically have UIDs starting from 1000 or higher. You can filter out the system users and display only human users by using the awk
command to check for UIDs greater than or equal to 1000.
This command will display only the usernames of regular users, excluding system accounts.
Method 6: Listing Users with Specific Groups
If you want to list users belonging to a specific group, you can use the getent
command combined with grep
. For example, to list all users in the “staff” group, use the following command:
This command filters the users by the “staff” group and then extracts only the usernames.
Method 7: Using id
Command to View User Information
If you need more detailed information about a specific user, you can use the id
command. This command provides the UID, GID, and group memberships of a given user.
For example, to see the details of a user named “john,” run:
The output will look like this:
Here, uid
is the user ID, gid
is the group ID, and groups
lists the groups that the user is a part of.
Method 8: Listing Logged-in Users with w
or who
If you want to know which users are currently logged into the system, you can use the w
or who
command. These commands provide information about the currently logged-in users, their active sessions, and the terminal they are using.
To list all logged-in users, use:
Or:
Both commands show similar information, including the username, terminal, login time, and the IP address (for remote users).
Method 9: Listing Users with last
Command
The last
command is used to show the most recent login history of users. It pulls data from the /var/log/wtmp
file, which tracks logins and logouts.
To view the list of recent user logins:
This command will display the login history, including the username, the terminal used, the login time, and the duration of the session.
Method 10: Using finger
Command
The finger
command provides information about users on the system, including their login name, home directory, login time, and more. If the finger
utility is installed on your system, you can use it to check details about a user:
This will show information about all users who have the finger
service enabled.
Conclusion
System management requires administrators to list users in Linux because it enables verification of system access permissions and user account administration as well as login activity monitoring. Linux offers a set of commands together with different methods which enable administrators to list users according to specific purposes. Linux administrators have multiple command options available so they can choose an appropriate tool that fits their requirements ranging from basic cat commands and awk to advanced getent and last functionalities.
System management and user account performance can be ensured through regular user listings which also allow for security policy enforcement. System administrators working in any Linux environment need to understand different commands because these tools represent essential elements for system administration.