List Users in Linux 2023

linux

List Users in Linux

Linux information can be stored by the local users in the /etc/passwd file. Each line having a file contains content about a single user, including their username, user ID number (UID), home directory, and login shell.

Also, read Add and Remove Users in Linux

The following commands are used in the tutorial:

  • cat command
  • less command
  • cut command
  • awk command
  • getent command

cat Command

When using the “cat” command with the "/etc/passwd" file, you can view the user account information stored within. Each line in the file represents a user, and the first field on each line is the username. You can acquire a detailed list of users configured on your Linux system by showing the contents of this file.

cat /etc/passwd

less Command

When dealing with systems that have a large number of users, it can be beneficial to limit the output displayed from the "/etc/passwd" file at once. To accomplish this, use a terminal pager command such as “less” or “more” to go through the file’s content in a controlled manner, either line by line or page by page.

By utilizing these pager commands, you can conveniently navigate through the extensive user account information stored in the "/etc/passwd" file, enhancing readability and ease of use.

less /etc/passwd

Use more to achieve the same result. This is an older command with a more limited set of capabilities:

more /etc/passwd

cut Command

We can accomplish the same thing with the cut command. We must use the -d (delimiter) option and instruct it to choose only the first field using the -f (fields) option.

cut -d: -f1 /etc/passwd

Note: The "/etc/shadow" file contains encrypted passwords and requires administrative privileges to access. Hence, the use of “sudo” is required.

This command uses the “cut” command to extract the first field (username) from the "/etc/shadow" file, which contains password information.

sudo cut -d: -f1 /etc/shadow

awk Command

We may display only the username using the awk command. This is useful when building a script that wants to perform anything with a large number of user accounts. Listing the account user names and redirecting them to a text file can save you a lot of time. The rest of the command can then be copied and pasted onto each line.

We will instruct awk to use the colon “:” as a field separator and to print the first field. The -F (field separator) option will be used.

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

getent Command

The “getent” command is used to access and get information from system databases. To consider a list of files in the "/etc/passwd" file, we can use the “getent” command with “passwd” parameter. These instructions are the command to specifically retrieve and display the user account entries stored in the "/etc/passwd" file.

You can view the whole list of users configured on your Linux system by using this command.

getent passwd

compgen Command

To list the user accounts, use the compgen command with the -u (user) option. Instead of one long list with a single user name per line, we’ll pipe the output through the column command to list the user accounts in columns.

compgen -u | column

Conclusion

Listing users in Linux is an essential operation for both system administrators and users. This article has shown you five ways to complete this operation using commands like as “cut,” “cat,” “getent,” and “awk.” You may rapidly obtain a list of users on your Linux system using these ways, allowing you to manage and maintain user accounts effectively. Please feel free to share your thoughts and feedback in the comment section below.

AmritMatti

I’m the owner of “DevOpsTechy.online” and been in the industry for almost 5 years. What I’ve noticed particularly about the industry is that it reacts slowly to the rapidly changing world of technology. I’ve done my best to introduce new technology into the community with the hopes that more technology can be utilized to serve our customers. I’m going to educate and at times demonstrate that technology can help businesses innovate and thrive. Throwing in a little bit of fun and entertainment couldn’t hurt right?

AmritMatti

I’m the owner of “DevOpsTechy.online” and been in the industry for almost 5 years. What I’ve noticed particularly about the industry is that it reacts slowly to the rapidly changing world of technology. I’ve done my best to introduce new technology into the community with the hopes that more technology can be utilized to serve our customers. I’m going to educate and at times demonstrate that technology can help businesses innovate and thrive. Throwing in a little bit of fun and entertainment couldn’t hurt right?

View all posts by AmritMatti →

Leave a Reply

Your email address will not be published. Required fields are marked *