list users and groups on Ubuntu

How to list users and groups on Ubuntu Linux?

Today we will learn How to list users and groups on Ubuntu Linux. Linux and its distributions are among the most efficient operating systems that provide a comfortable environment and useful features, which can be easily accessed by all local users and administrators. In this post, we will explain how to list users in Ubuntu Linux and the different ways to do it. Stay tuned!

Why is it so important to list users and groups in Ubuntu?

Linux and its distros, such as Ubuntu, are among the most popular operating systems often used by developers as a team. In this system, users are assigned different tasks, and each is added or removed from different groups depending on the need.

Therefore, the administrator or root user should be able to control the system for many security reasons and access a complete list of existing users and groups. This way, the root user will be sure that they have deleted or added the right users and given the right permissions or created the right restrictions.

A step-by-step guide to enrolling users in Ubuntu

In Ubuntu Linux, all user information is registered in a “passwd” file, located in “etc/passwd”. To list users on your Linux system, you need to access the contents of this file by following these steps:

  • Press Ctrl + Alt + T to open Terminal.
  • You can use the “cat” command to access the contents of etc/passwd/file. Alternatively, 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 a list of current users, whether they have recently connected to the system.

The output will be something like this:

list users and groups on Ubuntu

Here, each line corresponds to a specific user. So, the number of lines represents the number of users registered in the system.

What does a colon mean in “passwd file”?

In the etc/passwd/ file, each line contains a number of fields separated by a colon. Each field includes useful information about each user. As follows they are:

  • Root: Username
  • X: Encrypted password, which can be found on “etc/shadow/file”
  • 0: User identification number (UID)
  • 0: Group identification number (GID)
  • Root: Comment
  • /root: Home directory
  • /bin/bash: Shell used

List users using “cut”

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

This way, you will see the usernames defined by you through the desired pattern in the Passwd columns.

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

List Users Using “Let’s”

“awk” or “mawk” is similar to “cut” and allows you to pipe “cat” commands and separate users.

Remember, “awk” is a programming language designed to simplify the process of data extraction and data stream management, and it only displays usernames without any additional information.

You can use “awk” whenever you encounter complex text structures, which a single command can pretty much disassemble.

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

How to list users in Ubuntu using “gettent” command?

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

getent passwd

This way, you can retrieve all entries from databases like LDAP, DVS servers, files, etc.

$ getent passwd | cut -d: -f1

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

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

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

$ getent passwd | cut -d: -f1

You can also try using the “grep” command with “grep” as an existing user on your Ubuntu Linux system:

$ 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 the desired user without using the “grep” command:

$ getent passwd samreena

How to find the number of users in Ubuntu?

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

$ getent passwd | wc –l

How to list logged-in users in Ubuntu?

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

list users and groups on Ubuntu

What does the “w” command represent?

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

  • User: Username
  • TTY: Terminal Name
  • From: remote hostname
  • Login@: Login time
  • Idle: Idle time
  • JCPU: Amount of time used by processes connected to the TTY.
  • PCPU: The time used by the process is displayed in the key field.
  • What: User’s current process.

You can use the “K” command to check the list of logged-in users. However, the output will not be as detailed as before.

Conclusion

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

Scroll to Top