Jannah Theme License is not validated, Go to the theme options page to validate the license, You need a single license for each domain name.
DevOpsLinux

How to Check/List all Users on Linux?

The Linux operating system is different from other OSes due to its multi-user feature, allowing multiple users to work simultaneously. Maintaining a user record is important, however.

System administration begins with configuring users and managing groups. A crucial part of this process is monitoring the capability of all system entities to log on.

Ways to List Users on Linux

Ways to List Users on Linux

This article aims to discuss how to list the users of a system. This article concentrates on four terminal-based methods to accomplish this task, but both GUI- and CLI-based methods can be used. 

These methods apply to any Linux distribution, although Linux Mint 20 is used in the examples:-

Method #1 is to use the “cat” command

The “cat” command can be used to list all user accounts on a Linux system by following the steps below:

We will launch the terminal.

In the /etc/passwd file of the Linux system, you will find the user account information and passwords of all the users, and you can list them on the terminal with the “cat” command.

$ 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.

Method # 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. To list all the users on a Linux system, you need to take the following steps:

  • We will launch the terminal.
  • Execute the following command:
$ awk –F: ‘{ print $1}’ /etc/password

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

Method # 3: The “compgen” command

In the same way as with awk, all other details will be ignored with this command. To use “compgen” to find all users on a Linux system, the following steps must be performed:

  • Launching of the terminal will occur.
  • 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

You can list all users using the cat command in Linux:

“$ 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. A user’s full name (or the name of the program, if the account is associated with a program), a building number and room number, home and office telephone numbers and any other information the user deems relevant may be included.
  • /var/lib/vnstat – The user’s home directory.
  • /usr/sbin/nologin – Login shell for the user. The /etc/shells file contains the paths of valid login shells.

Linux List Users using getent command

Linux List Users using getent command

List all users by using the getent command

  • A list of all users can be queried by the getent command from the databases specified in the /etc/nsswitch.conf file, including the passwd database.
  • Enter the following command to see a list of all Linux users:
“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 the list of all Users in Linux?

How to Check the list of all 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 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 values 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:

How to Count the Linux list users?

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. All information except the user name can be removed by using the cut command:

We still need to filter out repeated names once we receive this list of usernames. Their command only provides 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 in 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 whom, the root is also listed here. While it 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 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.

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 shows 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.

Arpit Saini

He is the Director of Cloud Operations at Serverwala 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

Leave a Reply

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