How to get list of users in Linux

Praneeth Bilakanti
2 min readMay 31, 2023

--

To get a list of users in Linux, you can use the cut, grep, and cat commands to extract the user information from the /etc/passwd file. Here's an example command

  1. Using cut command
cut -d: -f1 /etc/passwd

This command will display a list of usernames separated by new lines. Each line represents a user on the system.

If you want to exclude system accounts and only display regular user accounts, you can use the grep command to filter out system accounts. Here's an updated command:

cut -d: -f1 /etc/passwd | grep -vE '^(root|bin|daemon|sys|sync|games|man|lp|mail|news|uucp|proxy|www-data|backup|list|irc|gnats|nobody|systemd|_apt|messagebus)$'

This command uses grep -vE to exclude the system accounts specified within the regular expression. You can modify the regular expression as needed to exclude or include specific accounts.

2. Using cat command

You can achieve the same result using the cat command with the /etc/passwd file and some text processing commands like awk or cut. Here's an example

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

In this command, cat /etc/passwd outputs the contents of the /etc/passwd file, which contains user account information. Then, awk -F: '{print $1}' is used to extract the first field (username) from each line of the output. The -F: flag specifies the delimiter as a colon, which is used in the /etc/passwd file to separate fields.

If you want to exclude system accounts and display only regular user accounts, you can combine the cat and awk commands with grep, similar to the previous example:

cat /etc/passwd | awk -F: '{print $1}' | grep -vE '^(root|bin|daemon|sys|sync|games|man|lp|mail|news|uucp|proxy|www-data|backup|list|irc|gnats|nobody|systemd|_apt|messagebus)$'

This command will produce a list of usernames, excluding the specified system accounts, from the /etc/passwd file using the cat, awk, and grep commands. Again, ensure that you have the necessary permissions to access the /etc/passwd file or use sudo to execute the command.

3. Using the getent command:

The getent command retrieves entries from various databases, including the user database. You can use it to fetch the list of users by running the following command:

getent passwd | cut -d: -f1

This command uses getent passwd to retrieve user entries and cut to extract the usernames from the output.

4. Inspecting the /home directory:

On many Linux systems, user home directories are located under the /home directory. You can list the contents of the /home directory to see the user directories, each of which represents a user:

ls /home

Running this command will display the list of directories, with each directory representing a user’s home directory.

5. Checking the contents of the /etc/shadow file:

The /etc/shadow file stores password-related information for user accounts. Although it's not recommended to directly parse this file, you can inspect it to find user accounts. Use the following command to extract usernames from the /etc/shadow file:

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

This command uses awk to print the first field (username) from each line of the /etc/shadow file, which corresponds to the user accounts.

Keep in mind that to run these commands, you need administrative privileges or use sudo to execute them.

--

--

Praneeth Bilakanti
Praneeth Bilakanti

Written by Praneeth Bilakanti

DevOps , GCP Cloud Engineer & Java Developer with a strong foundation in system design. Passionate about leveraging cloud technologies for solutions.

No responses yet