Site icon Hostbillo Blog – Web Hosting, Devops, Tech, and more

How to List Users and Groups in Linux? {4 Easy Ways}

list users in linux

Introduction

Being a system admin on Linux often involves having an in-depth listing of all users and groups on the server. Having the right number of users and making sure you didn’t neglect to delete is extremely important for security reasons.

Linux has a variety of methods for listing users and groups. To gather meaningful information, you need to first access the passwd and the group files on your system using the cut command. For setting up custom databases on your host, a more efficient method is to utilize the getent command, which depends on the Name Service Switch, another Unix-based tool.

One of the most important Linux system administration tasks is user management. The ability to accurately add, eliminate, and allocate new user privileges depends on knowing the individuals with access to the system in large organizations.

On a Linux-based framework, this guide will demonstrate how to list users in Linux. In addition to outlining key ideas on user administration, this article offers four listing ways.

System Requirements

The principal system prerequisites are as follows:

List Users in Linux

Your Linux system stores details about local users inside the /etc/passwd file.

A user’s username, user identification number (UID), home directory, as well as login shell are all listed on each line of the file, along with other details about that specific user.

The sections that follow identify the list of users in Linux, and provide a variety of methods for accessing data in/etc/passwd on Linux systems.

This lesson uses the following commands:

Execute the who command to see an overview of the users who have currently logged on as well as details on boot time, procedures, hostnames, and other things.

Also Read: A Complete Guide to useradd Command in Linux with Examples

List Users on Linux using the Terminal Pager Command

Limiting the number of files displayed in the /etc/passwd file output simultaneously on systems with multiple users is beneficial. To read over the contents of the file word by word or page by page, utilize a terminal pager command like or in addition to.

Type the following to open a /etc/passwd file with less: 

less /etc/passwd

With the output, however, only the file’s first page is visible. Once it reaches the terminal screen’s edge, the list comes to an end. To move around the file, utilize the keyboard.

For a similar outcome, use more. This command has become dated and offers fewer functionalities:

more /etc/passwd

List Users using the /etc/passwd File

The /etc/passwd file contains details regarding the local user. Within this file, each line corresponds to a user’s login details. You can employ the following cat or less to access the file:

less /etc/passwd

This file contains seven fields, separated by colons, each of which contains the data listed below:

The proposed awk or cut commands can be used to output only the first field that contains the username when you are just interested in it:

Outcome

List User Names only in Linux using awk Command

List user names in Linux using the awk command, leaving out any extra details about the individual users. Given that the data columns in the/etc/passwd file get distinguished by colons, the next syntax instructs awk to produce only the primary field on every line:

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

Getting a page-by-page examination of the output, integrate less and awk.

awk -F':' '{ print $1}' /etc/passwd | less

Also Read: 50+ Linux Commands with Screenshots (Download PDF)

List Users in Linux using the cat Command 

A simple method for listing every element of a /etc/passwd file is to use the list users in Linux to the cat command.

Enter the following to view the file:

cat /etc/passwd

The system prints the full file, including a list of every single user.

Employ the wc command to determine the total amount of lines while piping the outcome of the command before it to display only the users: 

cat /etc/passwd | wc -l

The overall amount of lines in the /etc/passwd file equals the number of users.

Also Read: How to Zip a File in Linux?

Differentiate Between Normal and System Users

Both systems as well as non-system users can log into Linux-based computers.

Linux assigns each user a distinct user ID (UID) that serves as their identification. UIDs for system users vary from 0 (the root user) to 999. Every newly formed user gets the following-smallest unused UID, with normal users commonly obtaining UIDs starting at 1000.

Employ the grep command to look through the data that has got saved in /etc/login.defs to verify the UID range for common users.

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

The result from this example demonstrates that the smallest and biggest UIDs that a typical user can obtain are 1000 and 60000, respectively.

To query the passwd database using UID, execute getent:

getent passwd [UID]

The user entry associated with the UID gets displayed in the output of the program.

Find users inside a range by using UIDs along with getent:

getent passwd {[first-UID]..[last-UID]}

At this point, the command displays a list of all users inside the provided UID range.

Find Out if a User account already exists in Linux

when utilizing the grep command, you can utilize the previous commands to check whether the following user is present on the Linux machine:

compgen -u | grep sai
getent passwd | grep -q sai && echo "User sai found" || echo "User sai not found"
compgen -u | grep -q ram && echo "User ram found" || echo "User ram not found"

Another easier command would be the following:

Count the Number of User Accounts in the Linux

To find out how many users your system has, take a look at the following wc command:

compgen -u | wc -l
getent passwd | wc -l

Summing Up

You learned how to determine the total amount of Linux users across every Linux distribution, list all those who use Linux, and do a user scan in this article. Regardless of the Linux distribution—Ubuntu, CentOS, RHEL, Debian, or Linux Mint—, the same commands must get used.

Exit mobile version