English
+370 5 205 5502 sales@monovm.com

How to list users in Linux systems?

For every Linux system administrator, it is essential to know its users and their access to the system. To get the user’s details, Linux offers various simple commands.

21 Feb, 22 by Susith Nonis 6 min Read

List of content you will read in this article:

Linux supports multiple users; thus, sometimes, it becomes necessary to keep track of all the users and groups available on the system. Therefore, the system administrator must ensure complete security so no user can violate the system’s security by running unauthorized commands or access. 

Not all the users on the system have the same rights. These users are specified within different groups based on their level of privileges. Some users are dedicated to running critical tasks, while some have only read access. All this is necessary to keep the system and data secure. Thus, the system administrator must know how to take out the list of all the system users.

In this article, we will explore various ways to look at the list of all the users by executing multiple commands. Linux offers simple commands that the user can run by having the proper access.

Linux contains some files that store information about all the users. The file is “/etc/passwd” that keeps the currently available users on the system. To look at the information of the file, you can execute the “cat”, “more”, or “less” command as per your requirement.

$ cat /etc/passwd

$ less /etc/passwd

$ more /etc/passwd

In the above output, you can see various columns containing different information about the user. Let’s take a look to understand what does this output means. 

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

We will specify the details in the given order.

As you can see, the password is specified as “x,” which means the password is stored in the encrypted form so that no one can misuse the user’s account. 

  • Using the “cut” command to list the Linux usernames.

If you only want to display the usernames from the ‘/etc/passwd” file, you can pipe the cat command’s output to the “cut” command, as shown below.

cat /etc/passwd | cut -d: -f1

The “cut” command used the “:” as the separator. You print all users’ lists, then pipe to the cut command as per the above command. Then we will only get the first column of the result, which is the usernames from the passwd file, as shown below.

  • Using the “awk” command for listing usernames.

Another way to display all the lists having only the usernames is to execute the cat command and pipe the output to the “awk” command that works almost similar to the “cut” command. The awk command is an interpreter for the AWK programming language that helps extract and manipulate the data streams. It makes it easier to execute even the complex commands that cannot be done by any other command.

In the below example, we will see how the “awk” command works and displays the output.

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

  • Using getent command for listing Linux users

One of the simple ways to list all the Linux users is to hit the “getent” command along with the “passwd” argument and an optional user you want to list on your system, as shown below.

getent passwd <optional_user>

The getent command takes all its entries from Name Service Switch databases such as LDAP, DNS server, etc., all the details regarding these data sources are present in the config file- nsswitch.conf, which is located under the /etc. Folder.

getent passwd

getent passwd | cut -d: -f1

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

  • Listing all the connected users on the Linux host

Using the “cat” command on the “passwd” file will not display all the connected users on the Linux host. For that, you need to execute the “who” or “users” command.

who

OR 

users 

The above two commands will list only the connected users on the system at the time of executing the command.

  • Using “/etc/group” for listing the groups on Linux

Other than listing users, Linux allows you to list all the groups available on the system. All the group details are stirred in the file- /etc/group. You can execute the “cat”, “less”, or” more” command on this file to display its content.

cat /etc/group

less /etc/group

more /etc/group

In the above output, you can see some details, and we will explain the meaning of different columns of the /etc/group file.

root:x:0:root

Where-

  • The root is the group name.
  • x is the password in the encrypted form
  • 0 is the GID (group ID)
  • The root is the user present in the group.
  • Using the “cat” command

As you can see in the above example, the details are too cumbersome. So to only get the details of the groups, you can use the cat command on the “/etc/group” file and pipe the output to the “cut” command as we did with the “passwd” file, as shown below.

cat /etc/group | cut -d: -f1

cat /etc/group | awk -F: ‘{print $1}’

  • Using getent command

You can use the following syntax of the “getent” command to display the group list.

$ getent <database> <key>

Here, the database is the group, and the key is optional. But if you do not provide any value for the parameter key, you will get the details of the complete group file.

getent group

To the details of one specific group, you can specify it as shown below.

getent group root

For every Linux system administrator, it is essential to know its users and their access to the system. For that knowing the users is necessary from the security point of view. To get the user’s details, Linux offers various simple commands. We have mentioned some of the commands in this article and how they work with different arguments.

Try these commands yourself ad understand the working.

People also read: 

Susith Nonis

I'm fascinated by the IT world and how the 1's and 0's work. While I veture into the world of Technology I try to share what I know in the simplest way possible. Not a fan of coffee, a sweet addict and a self accredited 'master chef'.