How to List Linux Users on Ubuntu

How to List Linux Users on Ubuntu

In this article, we will show you how to list users on Ubuntu VPS. By the end of this tutorial, you’ll be able to perform some basic commands to view all registered users and groups to manage your system administration. So, without further ado, let’s get started.

Reasons to List System Users in Ubuntu

Linux is popular among tech research groups and development communities because it is suitable for team-based projects. This means that oftentimes, a Linux-based system is used by multiple users at the same time.

For that reason, managing these users and groups is a day-to-day task. System administrators can take advantage of Ubuntu’s complete features to control how users and groups interact with the system.

For instance, you can grant certain users and groups to access specific files and folders. What’s more, you can adjust configuration and administrative rights to modify your system and secure your data by preventing any harmful anonymous process.

However, before configuring those settings, it is better to learn how to list users on Ubuntu VPS or server. The reason is simple: you will need to see their permissions and activities to verify every operation on your machine.

Do note that since we will list users on Ubuntu VPS, we have to use the command line to access it. You can work with the terminal shell (Linux and macOS) or PuTTY (Windows). If you’re a Hostinger user, the login credentials are located in the hPanel’s Servers tab.

How to List Users on Ubuntu

We are going to divide this tutorial into three parts — listing all users, listing groups, and listing logged in users.

Listing All Users on Linux

Linux stores all the information about user registrations in a file called passwd, which is located at /etc/passwd.

  1. To access the content of the file, open your terminal and type the following command:
    less /etc/passwd
  2. The script will return a list that looks like this:
    root:x:0:0:root:/root:/bin/bash
    daemon:x:1:1:daemon:/usr/sbin:/bin/sh
    bin:x:2:2:bin:/bin:/bin/sh
    sys:x:3:3:sys:/dev:/bin/sh
    …

In the output above, each line corresponds to a user. The number of lines will depend on the number of registered users.

Each line contains seven fields that are separated by colons (:). These fields hold specific information about a particular user.

Let’s take a look at the first line:

root:x:0:0:root:/root:/bin/bash

Here are the meanings of each field, from left to right:

  • root — the login name of the user.
  • bplaceholder for the password (the actual password is stored in a separate file).
  • 0User ID, which is unique for each registered user. By default, root has the User ID of 0.
  • 0 — Group ID. Like User ID, it is unique for every user.
  • rootComment field. Contains a short description of the user, usually filled out with users’ full name.
  • /rootHome directory. This is the main directory of the users. For users other than root, the folder will be /home/username.
  • /bin/bashUser shell. Refers to the shell that users use to sign in to the system.

If you only want to view the name of the users, you can run this special command:

cut -d : -f 1 /etc/passwd

Listing User Groups on Linux

In Linux, you also have the option to create groups. You basically gather several users and grant them joint privileges or access. It is particularly useful if multiple teams are using the same system.

Unlike users, the group information is stored in a file named group that can be found at /etc/group.

  1. To list the contents of this file, simply open up the terminal and type the following command:
    less /etc/group
  2. The line above should give this output:
    Root:x:0: Daemon:x:1: Bin:x:2: Sys:x:3: .  . .

Notice that the group names are exactly the same as the name of our users. This is the result of user private groups (UPG), a scheme employed by Linux where each user will have their own private group.

Just like the previous one, it is possible to view the group names only:

cut -d : -f 1 /etc/group

Listing Logged In Users

To list logged in users, simply type the following letter in your terminal:

w

Besides showing the active users, the command also provides some additional information like login time, remote host, and idle time. The full results will look like this:

The results of the w command

Let’s breakdown the results of the w command:

  • 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 ⁠— the user’s current process.

Alternatively, there is another command that performs a similar function to w:

who

However, the results won’t be as detailed as the w command:

root     pts/0     2019-10-21 10:02

Conclusion

Linux is a perfect OS for team-based projects because many people can log in to the same system. Due to this nature, it is important to learn how to manage users for better system administration.

The first lesson you need to know is to list the registered users, which will be useful to verify all operations and protect your data.

In this guide, we have shown you how to list users on Ubuntu VPS using simple commands. In addition, you’ve also learned how to list all user groups and monitor every logged-in user.

Good luck and be sure to check other VPS tutorials!

Author
The author

Domantas G.

Domantas leads the content and SEO teams forward with fresh ideas and out of the box approaches. Armed with extensive SEO and marketing knowledge, he aims to spread the word of Hostinger to every corner of the world. During his free time, Domantas likes to hone his web development skills and travel to exotic places.