How to List Users in Linux, List all Users Command

Introduction

Linux OS is unique because of its multi-user characteristic allowing multiple users on one system, at the same time. However, tracking all users is essential.

In this article, earn multiple commands to list all Linux users along with their login information. These commands work on CentOS, Ubuntu, Arch Linux, and other Linux distributions as well.

Tutorial on how to list users in Linux.

Prerequisites

  • A Linux distribution installed and running
  • A user with sudo privileges
  • Access to a terminal/command line

List All Linux Users, 2 Options

List All Users in Linux with the /etc/passwd File

Details of local users can be found in the /etc/passwd file. Every line contained in the file contains the information of one user.

There are two options.

Open the etc/passwd file by typing the command:

cat etc/passwd

Alternatively, you can use the less command:

less etc/passwd

Note: To display a list of the logged-on users and the information such as boot time, processes, hostnames, and more, use the who command.

List All Linux Users with the getent Command

Database entries configured in the /etc/nsswitch.conf file include the passwd database with all the usernames and login information.

To extract this data, use the command:

getent passwd

Both Option 1 and Option 2 will display all the users and their login information.

a list of all Linux users output

Each line represents one user and has seven (7) fields.

The fields are separated by : (colons) and each line includes the following information:

1. Username
2. The encrypted password (represented by x, located in the /etc/shadow file)
3. User ID number (known as UID)
4. User group ID (known as GID)
5. User full name
6. User home directory
7. The login shell (by default set to bin/bash)

terminal line with user information

How to Only List Linux Usernames

In case you don’t need all the information related to each user, you can list only the usernames on the system. There are two ways to see just the first field (the username) of each user.

Option 1: Using the awk or cut command.

To list usernames only, you can use either of the following two (2) commands:

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

Option 2: Using the getent command with awk and cut.

To read and display the username without any additional information using the getent command, run the following command:

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

Alternatively, use the command:

getent passwd | –d: –f1

How to Search for Existing Linux Users

The getent command also allows you to check whether a user is present on the system.

Any of the following two commands will provide you with that information:

getent passwd | grep username
getent passwd username

If the user exists, it will display login information. On the other hand, if there is no such user, there will be no output.

For example, in the image below, the query displays whether a user named example exists. The output proves that such a user exists.

a query displaying whether a user named example exists

System User vs Normal User

A system user is the one that creates normal users. Therefore, in this instance, the system user is the root. This user is created when you first install the Linux operating system. Additionally, you can create system users for particular applications.

On the other hand, normal users are all users that the root (or a user with sudo privileges) creates. Each normal and system user has a real login shell, home directory, as well as a user ID (UID) number. The user ID number is given automatically in the range between the minimum and maximum values.

How to Check UID_MIN and UID_MAX

If you want to check what the UID range for normal users is, use grep with the following command:

grep –E”^UID_MIN|^UID_MAX” /etc/login.defs

The output shows that all normal users have a UID anywhere from 1000 (UID_MIN) to 6000 (UID_MAX).

checking the UID_MIN and UID_MAX

Note: Change the values in the command according to the minimum and maximum UID values for your system.

How to List Normal Users

With these numbers in mind, you ask for a list of all the users in that range with the command:

getent parrwd {1000..6000}

The query lists all the normal users, as seen in the image below. In this example, there are two normal users in the specified range.

listing normal users in linux example

Conclusion

After reading this guide, you should know how to list all Linux users, search for users, and find the number of Linux users in any Linux distribution ( Ubuntu, CentOS, RHEL, Debian, and Mint).

Next, you can learn about Linux file permissions, as well as how to list scheduled cron jobs for specific users.

原创文章,作者:kepupublish,如若转载,请注明出处:https://blog.ytso.com/223821.html

(0)
上一篇 2022年1月7日
下一篇 2022年1月7日

相关推荐

发表回复

登录后才能评论