Linux Server

How to Check/List all Users on Linux – Serverwala

The Linux operating system is different from other OSes due to its multi-user feature, allowing multiple users to work simultaneously. However, it is vital to keep track of all users.

The configuration and management of users and groups are fundamental aspects of system administration. A key part of this process is monitoring the capability of all system entities to log on.

Ways to List Users on Linux

Throughout this article, we will discuss the methods for listing the users of a Linux system. This article concentrates on four terminal-based methods to accomplish this task, but both GUI- and CLI-based methods can be used. 

Although the following methods are discussed on a Linux Mint 20 system, you can use any Linux distribution:-

The “cat” command is method #1

The following steps should be taken in order to use the “cat” command to list all user accounts in a Linux system:

The terminal will be launched.

Use the “cat” command to list all the users on the terminal to display all the user account details and passwords stored in the /etc/passwd file of the Linux system.

$ cat /etc/passwd

The command below displays the usernames and some additional information along with the usernames. View all the users of the Linux system by scrolling through this list.

# 2: Using the “awk” command

You can use the “awk” command if you only want to display usernames, so you don’t see all the technical information that “cat” returns. The following steps should be performed in order to use this command to list all users on a Linux system:

  • The terminal will be launched.
  • Execute the following command:

$ awk –F: ‘{ print $1}’ /etc/passwd

This command will return only the usernames when you run it in your terminal. It contains a list of every Linux user on the system.

Method # 3: The “compgen” command

With this command, all other details are ignored, just like with the “awk” command. The following steps should be performed in order to use the “compgen” command to list all users on a Linux system:

  • The terminal will be launched.
  • Execute the following command:

“$ compgen –u”

Your Linux usernames will be returned by this command.

Linux List Users using the /etc/passwd file

Linux list all users account using the /etc/passwd file

For Linux, the cat command can be used to list all users:

“$ cat /etc/passwd”

  • My observations are as follows:

root:x:0:0:root:/root:/bin/bash

daemon:x:1:1:daemon:/usr/sbin:/bin/sh

bin:x:2:2:bin:/bin:/bin/sh

sys:x:3:3:sys:/dev:/bin/sh

sync:x:4:65534:sync:/bin:/bin/sync

games:x:5:60:games:/usr/games:/bin/sh

man:x:6:12:man:/var/cache/man:/bin/sh

vnstat:x:131:137:vnstat daemon,,,:/var/lib/vnstat:/usr/sbin/nologin

There are seven fields in each line of the file. Consider the following example:

“vnstat:x:131:137:vnstat daemon,,,:/var/lib/vnstat:/usr/sbin/nologin”

Where,

  • vnstat – The name of the user.
  • x – Encrypted password is stored in the /etc/shadow file.
  • 131 – User identification number (UID)
  • 137 – Primary GID (group identification number)
  • vnstat daemon – GECOS. It may include the user’s full name (or the name of the program if the account is for a program), building and room numbers, office phone number and home phone number, and any other contact information.
  • /var/lib/vnstat – The user’s home directory.
  • /usr/sbin/nologin – Login shell for the user. Pathnames of valid login shells come from the /etc/shells file.

Linux List Users using getent command

Use the getent command to get a list of all users

  • The getent command displays entries from databases configured in /etc/nsswitch.conf file, including the passwd database, which can be used to query a list of all users.
  • To get a list of all Linux user, enter the following command:

“getent passwd”

  • As you can see, the output is the same as when displaying the content of the /etc/passwd file. If you are using LDAP for user authentication, the getent will display all Linux users from both /etc/passwd file and LDAP database.
  • If you only want to print the first field containing the username, you can use awk or cut:

getent passwd | awk -F: ‘{ print $1}’

getent passwd | cut -d: -f1

How to Check Users in Linux?

Having learned how to list all users on a Linux box, we can simply pipe the list to grep to linux show users if a user exists.

As an example, the following command can be used to determine whether a user with the name jack exists in our Linux system:

“getent passwd | grep jack”

A command like the one above prints the login information of a user if they exist. If no output is displayed, the user does not exist.

In addition to checking whether a user exists with the grep command, the following can also be used:

“getent passwd jack”

The login information for the user will also be displayed if the user already exists.

Get the number of user accounts on your system by running the wc command with the getent passwd output:

“getent passwd | wc -l”

My Linux system has 33 user accounts, as you can see from the output above.

System and Normal Users

Regular (normal) users do not really have any technical differences from the system. The system is typically created when installing new packages and the OS. A system user can be created in some cases and be used by certain applications.

Users with normal privileges are those that have been created by the root or another person with sudo access. Normal users have their own login shell and home directory.

UID is a numeric identifier for each user. If not specified when creating a new user with the useradd command, the UID will be automatically selected from the /etc/login.defs file depending on the UID_MIN and UID_MIN values.

  • To check the UID_MIN and UID_MIN values on your system, you can use the following command:

“grep -E ‘^UID_MIN|^UID_MAX’ /etc/login.defs”

UID_MIN          1000

UID_MAX         60000

From the output above, we can see that all normal users should have a UID between 1000 and 60000. Knowing the minimal and maximal value allows us to query a list of all normal users in our system.

The command below will list all normal users in our Linux system:

getent passwd {1000..60000}

vagrant:x:1000:1000:vagrant,,,:/home/vagrant:/bin/bash

jack:x:1001:1001:,,,:/home/jack:/bin/bash

anne:x:1002:1002:Anne Stone,,,:/home/anne:/bin/bash

patrick:x:1003:1003:Patrick Star,,,:/home/patrick:/usr/sbin/nologin

Your system UID_MIN and UID_MIN values may be different so the more generic version of the command above would be:

Read More: How to Update Git Version on Linux, Windows, Mac?

How to Count the User Accounts in Linux?

You can see all the open log-in sessions on a machine by using the who command:

If you want a more detailed view of what each login session is doing, you can use the we command:

If you use the -h option with who or w, you can count the line count in the output. By omitting the header lines, we do not want to count them. Pipe the output with a vertical bar (“”) to create a command pipeline. In the pipeline, output from one program is passed onto the next. Our function counts the lines of both who and w -h by pipelining them to wc -l, resulting in an active session count.

The number of unique users

According to the above method, login sessions are counted once, but if a user has more than one open login session, they are counted twice. Counting unique users requires more creativity. By using the cut command, we can remove all information except for the user name:

This command gives us only a list of usernames, but we still need to filter out repeated names. It says, “take the output of who, and show only the first field of information, separated by a space.”

We can accomplish this by adding the sort -u command. By sorting alphabetically and filtering out duplicate names, the list is sorted as follows:

Finally, we add wc -l to our command pipeline to count these unique users:

Counting processes run by any user using ps

Using the ps command, you can create a list of every process that is currently running on the system. You can do this by using the options -e, -a, -h, and -o user in ps. These options can be combined the following way:

The command tells the shell to print the name of every user and every process owned by that user, without showing the headers.

As well as the users previously listed by who, root is also listed here. While who displays only logged-in users, ps displays any user who owns a running process, even if the user does not have a terminal open. Root is included in ps, as are other system-specific users.

How to Find System Users and Normal Users?

Regular (normal) users do not really have any technical differences from the system. Users are typically created during OS and package installation. A system user can be created in some cases and used by some applications.

Users that have been created by root or another user with sudo privileges are considered normal users. Most normal users have a login shell and home directory.

User IDs are numeric IDs assigned to each user. 

Check your system’s UID_MIN and UID_MIN values by running the following linux user commands:

grep -E ‘^UID_MIN|^UID_MAX’ /etc/login.defs

UID_MIN          1000

UID_MAX         60000

From the output above, it can be seen that the UID for most users should be between 1000 and 60000. We can query a list of all normal users in our system using the minimal and maximal values and find users in linux.

Read More: Learn to use SCP Command in Linux (with Examples)

Conclusion

You should now be able to find out what Linux distribution has  the most users ( Ubuntu, CentOS, RHEL, Debian, and Mint).

Furthermore, you learned linux show all users,linux check users,linux view users and filter users in your Linux system and how to distinguish system users from normal Linux users.

Linux distributions such as Ubuntu list users, CentOS, RHEL, Debian, and Linux Mint can be used with the same commands.

Get in Touch

Arpit Saini

He is the Director of Cloud Operations at Serverwala Cloud Data Centers Pvt Ltd and also follows a passion to break complex tech topics into practical and easy-to-understand articles. He loves to write about Web Hosting, Software, Virtualization, Cloud Computing, and much more.

Related Articles

Adblock Detected

Please consider supporting us by disabling your ad blocker