Signup/Sign In

How To List Users In Linux

Posted in Programming   JANUARY 7, 2022

    How To List Users In Linux

    Introduction

    Linux OS is unique because of its multi-user attribute enabling several users on one machine simultaneously. However, monitoring all users is crucial.

    In this post, acquire numerous scripts to list all Linux users and their login credentials. These instructions work on CentOS, Ubuntu, Arch Linux, and other Linux distributions.

    List All Linux Users

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

    Details about local users may be obtained in the /etc/passwd file. Every line included in the file comprises the details of one user.

    There are two alternatives.


    Open the etc/passwd file by entering the command:

    cat etc/passwd


    Alternatively, you may use the less command:

    less etc/passwd

    Note: To show 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 specified in the /etc/nsswitch.conf file contains the password database with all the usernames and login information.
    To extract this data, run the command:

    getent passwd


    Both Option 1 and Option 2 will reveal all the users and their login credentials.

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


    1. Username

    2. The encrypted password (represented by x, found in the /etc/shadow file)

    3. User ID number (known as UID) (known as UID)

    4. User group ID (known as GID) (known as GID)

    5. User complete name

    6. User home directory

    7. The login shell (by default set to bin/bash)

    How to Only List Linux Usernames

    If you don’t need all the information associated with each user, you may simply display the usernames on the system. There are two methods to view only the first field (the username) of each user.

    Option 1: Using the awk or cut command.

    To list usernames exclusively, you may use any 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 show the username without any further information using the getent command, perform the following command:

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

    Alternatively, use the command:

    getent passwd | –d: –f1

    How to List Normal Users

    Using 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 usual users, as shown in the picture be\slow. In this example, there are two typical users in the defined range.

    Conclusion

    After reading this article, you should know how to list all Linux users in any Linux distribution ( Ubuntu CentOS, RHEL, Debian, and Mint).

    About the author:
    Adarsh Kumar Singh is a technology writer with a passion for coding and programming. With years of experience in the technical field, he has established a reputation as a knowledgeable and insightful writer on a range of technical topics.
    Tags:ubuntuhowtolinux
    IF YOU LIKE IT, THEN SHARE IT
     

    RELATED POSTS