Linux Commands

How to Print the Usernames of Currently Logged-In Users in Linux

A Linux system can have many users logged in at a go. The Linux administrator checks the logged-in users and can see what each user is doing on their terminal. Checking who is currently logged in is one way of enhancing the security of your system.

This guide discusses how you can print the usernames of the currently logged-in users in Linux.

Different Ways of Printing the Usernames of Logged-In Users in Linux

Linux can be accessed remotely, and many users utilize a protocol like SSH to access their Linux systems remotely. Keeping a tab on who is logged into the system is part of the administrator’s job. Let’s see the various ways to achieve that.

1. Using the “W” Command

A command like “w” retrieves the details of the currently logged users from the /var/run/utmp file when executed. It then displays the login details in a table-like manner for analysis.

Here’s how to run it:

$ w

You will get an output like the one in the previous image. Let’s understand what details it gives:

  • USER – They represent the usernames of the logged-in users.
  • TTY – It represents the terminal in which the user is logged in with.
  • FROM – This is the hostname or the IP address from where a user is logged into the system.
  • IDLE – It represents the idle time of a given user.
  • LOGIN@ – It is when a given user logged into the system.
  • WHAT – It outlines the details of the current process that the user is doing.
  • JCPU – It represents the total time consumed by all processes in the TTY.
  • PCPU – It represents the time used by the active process represented under the WHAT of each user.

That’s how to interpret the details about the usernames that are currently logged in Linux.

The “w” command lets you narrow down the login details of a particular user. For that, begin by listing the usernames of logged-in users as we did earlier. Then, specify one username to get more details.

$ w [username]

When using “w” to print the usernames of logged-in users, you can reduce the information displayed by adding the -s flag as shown in the following:

$ w -s

2. Using Who

When you want to print the username of logged-in users with less details, the who command is your best choice. The command outputs the username, the terminal device, the login date and time, and the hostname or the logged-in user’s IP address.

$ who

Adding the -a option gives the details of the last system boot and other information while still printing the usernames of logged users.

3. Using Whoami

Unlike who, the whoami command only prints the username of the logged user of the active shell. Therefore, you can’t use it to monitor others.

The command mainly applies when you want to get your username and confirm who is using the active shell.

$ whoami

Its output only gives the username of the active shell.

4. Using ID

When combined with the –un flag, the id command works the same as the whoami. It displays the username of the active shell as follows:

$ id -un

You can’t use this method to get the usernames of other logged-in users.

5. Using Users

Linux offers the users command which prints the usernames of all currently logged-in users without adding any details. You will only see their username as displayed in the following:

$ users

6. Using Finger

Finger is a Linux package that acts as a lookup utility. When executed, it shows the user details such as their terminal name, login name, idle time, and login time.

To use the lookup utility, install it first using the following command for Ubuntu:

$ sudo apt install finger

Once the package installs, run it as follows to look up the user details:

$ finger

7. Using the Last Command

In Linux, last displays the log details of all the login sessions for a given time range. It shows how and when the different users logged into the system and who is currently logged in.

$ last

The previous output is extensive. You can narrow it down. For instance, you can retrieve the last five login details by adding 5 to the command.

$ last -5

That way, you narrow the output for easy system monitoring. The logged-in users have the “logged in” message on the right.

Conclusion

When different users are logged into the system, it’s possible to monitor them and print their usernames using various options including the “who”, “w”, “last”, “finger”, “users”, and whoami” commands. We’ve seen the examples of using each of them to retrieve the usernames. Try them and find which works best for your case.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.