How to list users and groups on Ubuntu Linux?

List of content you will read in this article:

    Linux and its distributions are among the most efficient operating systems that provide a comfortable environment and useful features, which all local users and administrators can easily interact with. In this article, we will talk about how to list users in Ubuntu Linux and explain the different ways to do this. Keep along with us!

    Why is listing users and groups so important on Ubuntu?

    Linux and its distros, such as Ubuntu, are among the most popular operating systems developers often use as a team. 

    In this system, different tasks are assigned to users, and each is added or removed from different groups depending on the necessity. 

    Therefore, the administrator or root user should be able to control the system and access a full list of the existing users and groups for many security reasons. 

    This way, the root user will be ensured that they have been deleted or added correct users and given the right permissions or make the proper restrictions.

    A step-by-step guide to listing users on Ubuntu

    On Ubuntu Linux, all of the users’ information is registered in a “passwd” file, which is located at “etc/passwd”. 

    To list the users on your Linux system, you should access the content of this file by following these steps:

    • Press Ctrl + Alt + T to open the terminal.

    • To access the contents of the etc/passwd/file you can use the “cat” command. Otherwise, you can navigate the list using the “more” or “less” commands.  

    $ cat /etc/passwd 

    $ less /etc/passwd 

    $ more /etc/passwd

    Executing these commands is one of the best ways to display the list of current users, whether they have recently been connected to the system.

    The output would be something like this:

    Here, each line is related to a specific user. Therefore, the number of lines represents the number of registered users in the system. 

    What do the colons mean in the “passwd file”?

    In the etc/passwd/file, each line contains different fields separated by a colon. Every field includes useful information about each user. They are as follows:

    • Root: Username

    • X: Encrypted password, which can be found in “etc/shadow/file”

    • 0: User identification number (UID)

    • 0: Group identification number (GID)

    • Root: Comment

    • /root: Home directory

    • /bin/bash: Shell used

    1. List users using “cut”

    As you can see above, to list usernames on Ubuntu, you can use “cat” command. In case you want to separate usernames, which are available in the first column, you can pipe the “cat” command using “cut” and “d” options. 

    This way, you can see the usernames defined by you through the desired pattern on the passwd columns.

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

    1. list users using “awk”

    The “awk” or “mawk” is the same as “cut” and help you to pipe the “cat” command and separate users. 

    Remember, “awk” is a programming language designed to ease the process of data extraction and managing data streams, and it only displays the user name without any additional information. 

    You can use “awk” whenever you encounter complicated text structures, which a single command can roughly isolate.

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

    How to list users on Ubuntu using “getent” command?

    The “getent” command helps you easily collect administrative database entries, which are set in the “etc/nsswitch.conf f/” file, and list users and groups if required.

     getent passwd <optional_user>

    This way, you can retrieve all the entries from databases such as LDAP, DVS server, files, etc. 

    $ getent passwd | cut -d: -f1

    You can also use the “awk” parameter along with the “getent” to display the first field as follows:

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

    Alternatively, you can use the “cut” command to display the same output:

    $ getent passwd | cut -d: -f1

    You can also check out the existing user on your Ubuntu Linux system using the “getent” command along with the “grep” as follows:

    $ getent passwd | grep user-name

    For example, suppose you want to search for the username “samreena” and see if such a user exists in the system.

    $ getent passwd | grep samreena

    You can find out the desired user without using the “grep” command:

    $ getent passwd samreena

    How to find the number of users on Ubuntu?

    If you want to know the number of users in your Ubuntu system, you can use the following command:

    $ getent passwd | wc –l

    How to list logged-in users on Ubuntu?

    To list active or logged-in users, you can type the “w” letter in your terminal. The output would be something like this:

     

    What does the “w” command represent?

    The “w” command not only displays the list of active users but also represents additional information such as login time, remote host, and idle time. Each part of the above result means:

    • User: username

    • TTY: terminal name

    • From: the name of the remote host

    • Login@: login time

    • Idle: idle time

    • JCPU: the amount of time used by processes attached to the TTY.

    • PCPU: the time used by the process displayed in the WHAT field.

    • WHAT: current process of the user.

    You can also use the “who” command to check the logged-in users’ list. However, the output would not be as detailed as the previous one. 

    The Bottom Line

    Here, you learned about different commands that help you to list users and groups on your Ubuntu system. This is one of the best ways to monitor every logged-in user and run the team-based projects as functionally as possible. Let us know if you have any questions or suggestions in the comment section! Good luck!

    People also read: 

    Alternate Text Gọi ngay