Signup/Sign In

Who Command In Linux With Examples

The “who” command is a Linux operating system command-line utility that is used to display a list of all logged-in users along with information about their login sessions.

It is very useful for system administrators who need to keep track of who is using the system, as well as for any individual users who wish to determine if anyone else is logged in.

Typically, the output of “who” command, if executed without any arguments, contains the following information:

  1. Username of the currently logged-in user
  2. The terminal or console where the user initially logged in.
  3. Date and time of the user's login
  4. The computer's IP address or hostname from which the user logged in.

Ubuntu terminal running who command

In addition to the basic “who” command, there are several options are available for alter command's output.

For instance, You can use the “-a” option to display information about all users, including those who have remotely logged in.

Print all information (-a)

You can use “-a” to display all information about the currently logged-in user as follows:

who -a

The output displays details about all logged-in users, such as the system boot time, login name, terminal name, and time.

Displays column headers (-H)

If you find who command output hard to read, the “-H” flag got you covered. It will show Column names/headers to help you easily read the output of the command.

Display column headers option in who command

We can use it with any other flag to show column header as shown above.

Let's take a look at useful options which are generally helpful to perform various tasks.

1. Display information about users with who command

In this section, we will explore options or flags which can be used to display information about users.

Here is the list of options and what they do:

  1. -m: Show the host name and user associated with standard input (stdin) such as the keyboard
  2. -q: Count the number of users who are logged in to the system
  3. -s: Display the name, line, and time fields only (default)
  4. -T: Display the status of the user's message status (as +, -, or ?)
  5. -u: Show user's idle time and process ID.

Print user information with who command using flags: -m, -q, -s, -T, -u

From the above screenshot, you can clearly sport the difference between each commands output.

2. Get System info with who command: -b, -r

Now, we will explore options or flags which can be used to display information related to system regardless of users.

We have two options related to system information:

  1. -b: Display the time when the machine last booted.
  2. -r: Display the current run-level

Display system information with who command

The above screenshot makes sense to add multiple options to be productive on command line.

3. Display information about processes: -d, -l, -p

Finally, we will explore options or flags which can be used to show info about processes running and user responsible for them.

Here are the options to shown process information about users:

  1. -d: Provide information about all dead processes
  2. -l: Display information about the system login procedure
  3. -p: See all current processes created by the INIT process

Get process information about a user with who command

As we can see, there are no process related to any specific user. However, the output of these command might not always be empty.

FAQs

Let's see some commonly asked questions related to who command.

Q. What is the difference between who, w and whoami command?

Nice question! These commands are used for similar purpose, but they limit amount of information provided as output.

Let's better understand the difference between these commands.

  • whoami: It prints the username of the user who ran the command and nothing else.
  • who: It shows who is currently logged in. It displays the username, terminal, login time, and remote host for each user.
  • w: It shows who is currently logged in and what they are doing. It displays the username, terminal, remote host, login time, idle time, CPU time, and current process for each user.

For example, if you run these commands on a Linux system, you may see output something like this:

Difference between whoami, who and w command shown in Ubuntu terminal

I hope this clarifies the difference between these commands.

Q. How to display hostname and user associated with stdin with who command?

You can display the hostname and the user associated with stdin, such as keyboard, when used with -m flag or literally any two arguments.

For example, who am i is a popular example, but any two words produce the same result.

Q. What is user message status?

User message status is simply a way to check permissions for writing messages to another user’s terminal.

Some possible message statuses are:

  • + : The user has permissions to write to the terminal
  • - : The user does not have permissions to write to the terminal
  • ? : Unable to find the terminal device

Q. Can we use who command in bash script or automation?

Yes, of course. For example, We can use who in bash if else statements to perform different actions based on the output of who command.

#!/bin/bash
# This is a bash script that uses who in if else

USER="curly"
if who | grep -q $USER; then
  echo "$USER is logged in."
else
  echo "$USER is not logged in."
fi

In the above script, we used “who” to check if a specific user is logged in or not.

Lastly, whether you are a system administrator or a regular user, knowing how to use the 'who' command can help you manage your system and troubleshoot issues more efficiently.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.