How to List Users in Linux?

FREE Online Courses: Elevate Skills, Zero Cost. Enroll Now!

In this article, you will learn how to list the users in Linux-based operating systems. We will learn more about users, what happens when we create them and then will we see how to list the users using various commands like awk, cut, cat, and getent.

We will also look at some bonus tips like displaying the number of users, UID range, printing only the usernames, searching for a particular user name, and more. Apart from all of this, we will also understand the format of the output that is printed on the screen. So buckle up and read till the end!

What is a user in Linux?

We all know what a user is, but let us take a look at what a user is in the intimidating words of computer science language. A user is an entity in an operating system that can manipulate files and perform several other applications like running, writing, and reading programs.

We know by now that Linux is a multiuser platform. To keep the system productive, organized, and functional we must manage these users properly. The system administrator must know all the users to manage users and their permissions.

Tracking all the users is a very crucial job. We can make use of multiple tools to do so, which we will look at in this article. We also know that everything in Linux is a file, all the information regarding users is stored in the “/etc/passwd” file, and the hash passwords are stored in “/etc/shadow” file.

Each user is identified by a unique UID, which is a “User Identification number”. When the operating system is installed, the UID 0 is given to the root user and the UID 1 to 199 are reserved for the system users. Meaning that as and when a new user is created they will be assigned a sequential number from 1 to 199. The UIDs for the local users begin from 1000.

Creating a user in Linux

Before we even learn how to manage the user and get information regarding them, let us first look at what the system does when creating a new user.

When we are creating a user In Linux, the system does the following:

1. It assigns a UID to the new user.
2. It creates another new /home directory for the new user.
3. Sets the default shell as /bin/sh for the new user
4. Creates a private user group named after the user name you entered.
5. Contents of the directory “/etc/skel” are copied to the home directory of the new user.
6. It copies files containing environmental variables for the user’s session like .bashrc, .bash_profile, and .bash_logout to the new user’s home directory.

Listing all the users in the /etc/passwd file in Linux

There are many ways to list the users in the /etc/passwd file, let us look at some of them.

1. The first method is to list all of the data in the file, to do so, use the following command:

less /etc/passwd

listing all the users in the passwd file by using the less command

Before we look into other methods, let us first understand the output of the above screenshot. The data is printed in the format shown below:

<username>:<password>:<UID>:<GID>:<GECOS>:<home_dir>:<shell_path>

Let us take a closer look at what each field is for.

a. <username>

This field specifies the name of the use you want to create.

b. <password>

This field specifies the value of an encrypted password. An “x” in this field denotes that the encrypted password is stored in the /etc/shadow file.

c. <UID>

This field specifies the value of the user identity number.

d. <GID>

This field takes in the value of the

e. <GECOS>

This field specifies the additional information like the full name of the user or comment (GECOS)

f. <home_dir>

This field specifies the absolute path of the user’s home directory

g. <shell_path>

This field specifies the login shell of the user

2. If you want to print out only the usernames and not the remaining data, we can either use the awk command or the cut command, let us look at both:

a. You can use the awk command as follows to print only the usernames:

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

listing all the users in the passwd file by using the awk command

b. You can use the cut command as follows to print only the usernames:

cut -d: -f1 /etc/passwd

listing all the users in the passwd file by using the cut command

c. the third method to display all of the information regarding the users is using the “getent” command. Use the getent command as shown below to print all the information regarding the users in the system:

getent passwd

listing all the users in the passwd file by using the getent command

Checking if a user exists or not

Hopefully, by now we all know that if we want to search for something we make use of the “grep” command. Similarly to check if a user exists or not, we pipe the grep command to the getent command as shown below:

checking if a user exists or not by piping the grep command with the getent command

However, we can also check if a user exists or not without the grep command also, we can simply specify the string we are searching for after the getent command as shown:

getent passwd <username>

checking if a user exists or not by using the getent command

Counting the number of users

We also know by now that if we want to count something, we need to use the “wc” command. Therefore if we want to count the number of users in the passwd file, we simply pipe the getent command with the wc command along with the option “-l” to print out the number of lines.

counting the number of users

Printing the contents of the /etc/shadow file

We saw earlier that the hash passwords are stored in the file “/etc/shadow”. We can print out its contents by using the following command:

less /etc/shadow

printing the contents of the shadow file

Before we head further, let us understand the output shown in the above screenshots. The output is in the following format:

[username]:[enc_pwd]:[last_pwd_change]:[pwd_validity]:[warn_date]:[acc_validity]:[acc_disablity]

Let us take a closer look at what each field is for.

1. [username]

This field specifies the name of the user

2. [enc_pwd]

This field specifies the encrypted password. A black entry {::} denotes that a password is not required to login into that user’s account and an asterisk, {:*:}, denotes the account has been disabled.

3. [last_pwd_change]

This field gives information about when the password was last changed

4. [pwd_validity]

This file specifies the number of days after which the password will expire

5. [warn_date]

The warning period specifies the number of days before the password expiry date, from which the user will start receiving a warning notification for password change.

6. [acc_validity]

The account validity field specifies the number of days after which the account will be disabled, once the password is expired

7. [acc_disablity]

The account disability field specifies the number of days since which the account had been disabled.

Types of users in Linux

Now that we know how to list all the users in the system, let us discuss the types of users in an operating system. Generally speaking, there are 2 types of users in Linux:

1. System user

The system user is the one that creates normal users. In other words, the system user is the root. The system user is automatically created when you first install the operating system. Moreover, you can also create system users for particular applications and programs.

2. Normal user

The normal users are the ones that are created by the system or root user, actually, we even users with sudo privileges can create normal users

Checking UID_MINand UID_MAX

We saw earlier how the user identification number is allotted to each user, If you want to check what the UID range is for the normal users, use the following command:

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

checking uid min and uid max

Listing normal users only

Now that we have the UID range of the normal user, we can use this to print all the normal users in the system by using the following command:

getent passwd {1000..6000}

listing normal users only

Using the cat command to list users

Just before we finish, here is a bonus method for printing the user information – using the cat command. Just like the gentent command, we use the following command:

cat /etc/passwd

listing all the users in the passwd file by using the cat command

Summary

As you have seen, listing users can be done by many commands like cut, cat, awk, getent, and many more. You have now learned what users are, what happens when we create them, and different ways to list the users using various commands like awk, cut, cat and getent.

You have also learned some bonus commands like displaying the number of users, UID range, printing only the usernames, searching for a particular user name, printing only normal users, and more. Apart from all of this, you also understood the format of the output that is printed on the screen.

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

follow dataflair on YouTube

Leave a Reply

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