Linux whoami, who, w commands
In this article, I will show you how to see information about the users that are logged in on the machine. For this we are going to look at the whoami, who and w commands.
Each of the commands has it’s own purpose so let’s see how and when to use them.
whoami
This command is a concatenation of the words who, am, i and it prints the current logged in user.
The syntax for this command is:
whoami [options]
and the basic usage is:

The whoami command has only two options, –help and –version that will print the help and version messages and exit.


Keep in mind that using sudo whoami will give you a different result

In the above example the output is root. This means that the user has sudo access. In the case that the user doesn’t have sudo access, an error message will be displayed.
Whoami comes in handy when you are ssh-ing in different systems or when switching users in order to perform different tasks and you need to confirm if you are running commands under the correct user.
who
While the whoami command returns the current logged in user, the who command displays the list of the logged users, the time of the last system boot, current level of the system.
The who command has the following syntax
who [OPTION][FILE NAME]
If we run the command without options, it will display a list of logged users

Not let’s have a look at some options that we can use along with the who command (you can check the manual page here).
who -q
The -q option will display the name of the logged in users and their count.

who -u
Using the -u option will print the regular user information and the idle time for each listed user

who -b
-b (–boot) option will display the time of the last system boot

who -r
The -r will show the current runlevel of the system

who -T
-T (-w or -mesg can you used in order to achieve the same result) will check the permission for writing messages to your terminal.

Based on the permission set for the user, you can have the following statuses
+ -> user has permission to write to terminal
- -> user does not have permission
? -> unable to find the terminal device
who -a
The -a (–all) option is useful when you don’t want to use separate options in order to see different data. This option prints the info provided by -u, -T, -t, -r, -p, -l, -d, -b.

w
The w command is a built in utility that prints out information about the users that are currently logged in on our machine and what each user is doing.
The syntax for the w command is
w [OPTIONS] [USERNAME]
If we run the command without any options we will have the following output

Let’s take a closer look at the output shown in the image above and see what each line and column means.
On the first line we have the following information
- the current system time
- for how long the system has been up
- number of logged in users
- the system load average (a measurement of the number of jobs that are currently running or waiting. It shows how busy the system is for the given interval - 1,5,15 minutes
On the second line we have
USER - name of the logged in user
TTY - name of the terminal used by the user
FROM - ip address from where the user is logged in
LOGIN@ - time of login
IDLE - time from the last interaction of the user with the terminal
JCPU - time used by all the processes attached to the tty
PCPU - time used by the user's current process
WHAT - the current process and options
From the third line onward the output show information about the currently logged in users.
If we run the w command and pass a user name as argument, the output will show information about only that user

The information displayed by the w command is pulled from the /var/run/utmp file
Now let’s see some options that can be used along with the w command (for all the options that can be used with the w command see the man page here).
w -s
The -s(–short) option will print only the USER, TTY, FROM, IDLE and WHAT fields.

w -h
-h(–no-header) option will print the information about the currently logged in users and will ignore the header

w -i
If we use the -i(–ip-addr) option, the command will show the IP address in the form filed instead of the host name

w -o
-o(–old-style) will print blank space when times for IDLE, JCPU and PCPU are less than one minute

This is it for this post. Thank you for reading.
Remember that you can always check the manual to see how to use a command and don’t be afraid to google it.
