Are you looking for a way to list users in Linux? This can be especially useful when you want to manage system permissions, monitor user activities, or troubleshoot errors as a Linux system administrator. Linux offers countless commands that aid you in listing users, including cat, awk, and getent commands.
In this article, we’ll talk about how to list users in Linux using various methods. Before that, we’ll briefly look at the types of users in Linux.
Types of Users in Linux
In Linux, there are generally three types of users, each with different levels of access and control over the system:
- Superuser or Root user
- System Users
- Regular Users
1. Superuser Or Root User
The root user or superuser is the most powerful user account on a Linux system. Moreover, it has high privileges to access everything, including creating, reading, modifying, and writing files, as well as executing any command.
Additionally, the root user can install or remove any software and even create or delete user accounts. Therefore, it’s important to use the root user account carefully to prevent any unexpected damage to the system. Lastly, the user ID of the root account is 0.
2. System Users
The system users are created automatically when you install an operating system or certain software. Generally, these users have fewer privileges than root users and don’t have home directories or the ability to log in directly.
Moreover, the system user accounts aren’t utilized by actual people to log in. Instead, they’re utilized by background services or applications, such as web servers and databases. Their main purpose is to keep the services isolated and running securely.
Lastly, the system user ID ranges from 0 to 999.
3. Regular Users
This type of user is created by the administrators. Regular users have their accounts and can access the system with the credentials given by the administrator. In short, regular users have limited access to the system.
For instance, they can create, modify, and run files, but only within their home directories. Each regular user is also allocated a unique ID for identification, starting from 1000.
Generally, regular users have the fewest privileges. They can run applications but can’t make system-wide changes, like installing software or managing other users, without permission from the root user.
Familiarity With /etc/passwd File
The /etc/passwd file stores local user account information in the Linux system. Moreover, each line in the file describes login details for a user and contains 7 entries, including username, password, userID, groupID, user description, home directory, and shell directory, with all entries separated by a colon (:).
For instance, let’s view and understand a line of code from /etc/passwd file:
linux:x:1001:1001:Linux,,,:/home/linux:/bin/bash
The breakdown of the above code of line is:
- linux: This field is for the user name, in our case “linux” is the username.
- x: It indicates the encrypted password stored in the /etc/shadow file.
- 1001: It indicates the user ID number. The user ID is special for each user. However, 0 is dedicated to the root user.
- 1001: This is the group ID number kept in the /etc/group file.
- Linux: This field consists of all the extra user information such as the real name of a user, contact number, and more.
- /home/linux: This is the user’s home directory path.
- /bin/bash: It’s the path of the user’s login shell. In addition, by default, users who can log into the system are usually set up with the bash or sh shell (/bin/sh). However, there are other options, like csh, zsh, and more.
How to List Users in Linux
In Linux, listing users is crucial for managing accounts. There are numerous ways available to solve this hassle, including cat, cut, awk, and grep commands. You can utilize any of these to achieve the desired output.
1. Using cat Command
The cat command displays all information about the users stored in the etc/passwd file.
Now, execute the cat command to list users in the Linux:
$ cat /etc/passwd
As a result, every line of the output displays the details for a user, such as their username, home directory, UID, and login shell.
2. Using cut Command
The cut command extracts the specific section from each row. Additionally, it can also sort and list the users in alphabetical order in combination with other commands.
If you want to obtain the first section (username) from the list of users, execute the cut command with the -f1 flag:
$ cut -d: -f1 /etc/passwd
Let’s break down the syntax:
- -d: specifies the delimiter used to separate the data on every row
- -f1: selects the first field
- /etc/passwd: This file contains user information.
Furthermore, to extract the second field, run the cut command with the -f2 option:
$ cut -d: -f2 /etc/passwd
Moreover, you can also cut with the sort command to sort the list of users (usernames) in alphabetical order:
$ cut -d: -f1 /etc/passwd | sort
3. Using awk Command
The awk command allows you to search, extract, manipulate, and format data from the /etc/passwd file. Moreover, it also breaks the input into fields using spaces, tabs, or other delimiters, and lets you perform different actions on those fields.
Now, execute the awk command to display the output:
$ awk -F":" '{ print $1}' /etc/passwd
In the above syntax:
- -F”:” It is a field separator
- ‘{ print $1}’ displays the first field of each row. You can change it with any field number.
However, to retrieve the third field (user ID), execute the below command:
$ awk -F":" '{ print $3}' /etc/passwd
4. Using grep Command
The grep ( Global Regular Expression Print) command searches for the specific strings or patterns within a file.
Let’s execute the grep command to find the precise strings or username:
$ grep -i linux /etc/passwd
Now, analyze the syntax:
- -i: make the search case-insensitive.
- linux: searches for the specific string or username.
Furthermore, you can get the line number of a specific username by using the grep command with the -n flag:
5. Using getent Command
The getent command reads the user’s information from local (/etc/passwd) and remote databases. However, by default, it references the /etc/nsswitch.conf file to determine where to fetch the data from.
Now, execute the getent command to display the list of users:
$ getent passwd
Additionally, to view specific user details, use the getent command followed by the exact username:
Wrapping Up
As a Linux system administrator, it’s important to know the users and their access to the system. Therefore, Linux provides several commands to help you gather user information, such as cat, cut, awk, and more.
Firstly, we used the cat command to display all the user details. Secondly, utilized the cut or awk commands to show specific fields. Then, executed the grep command to filter the details based on a pattern or string. Lastly, we used getent command.
I’m a technical writer with a Bachelor’s in Computer Science. Through my research and writing, I aim to provide readers with comprehensive, informative articles that can assist them make informed decisions about their technological needs.