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:
- Username of the currently logged-in user
- The terminal or console where the user initially logged in.
- Date and time of the user's login
- The computer's IP address or hostname from which the user logged in.
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:
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.
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:
- -m: Show the host name and user associated with standard input (stdin) such as the keyboard
- -q: Count the number of users who are logged in to the system
- -s: Display the name, line, and time fields only (default)
- -T: Display the status of the user's message status (as
- -u: Show user's idle time and process ID.
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:
- -b: Display the time when the machine last booted.
- -r: Display the current run-level
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:
- -d: Provide information about all dead processes
- -l: Display information about the system login procedure
- -p: See all current processes created by the INIT process
As we can see, there are no process related to any specific user. However, the output of these command might not always be empty.
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:
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.
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.
# This is a bash script that uses who in if else
if who | grep -q $USER; then
echo "$USER is logged in."
echo "$USER is not logged in."
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.