Difference between who and whoami commands

I am logging in as root in my shell and typing who and this is the output.

who
root     tty1         2014-08-25 14:01 (:0)
root     pts/0        2014-09-05 10:22 (:0.0)
root     pts/3        2014-09-19 10:08 (xxx.xxx.edu)

It effectively shows all the users that have established a connection.

ssh ramesh@hostname

Running who again will result in another entry for the user ramesh.

who
root     tty1         2014-08-25 14:01 (:0)
root     pts/0        2014-09-05 10:22 (:0.0)
root     pts/3        2014-09-19 10:08 (xxx.xxx.edu)
ramesh   pts/4        2014-09-19 12:11 (xxx.xxx.edu)

Inside the root shell, I just do su ramesh and then run whoami. It will give me the current user, ramesh, as the output.

Effectively, who gives the list of all users currently logged in on the machine and with whoami you can know the current user who is in the shell.


who: Print information about users who are currently logged in.

whoami: Print effective username of the user who ran whoami.

For example:

mohsen@debian:~$ who ## list logged in usernames
mohsen   :0           2014-09-19 16:31 (:0)
mohsen   pts/0        2014-09-19 16:32 (:0)
mohsen   pts/1        2014-09-19 19:42 (:0)

mohsen@debian:~$ whoami 
mohsen                        ##### print my username (mohsen)
mohsen@debian:~$ 

I also recommend a command better than who, the w command. Its output is:

mohsen@debian:~$ w
 21:45:45 up  5:16,  3 users,  load average: 0.68, 0.54, 0.46
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
mohsen   :0       :0               16:31   ?xdm?   1:40m  0.27s gdm-session-wor
mohsen   pts/0    :0               16:32    1.00s  0.15s  0.01s w
mohsen   pts/1    :0               19:42    2:03m  0.13s 14.06s /usr/bin/python

See Also :

last command, /var/log/btmp and /var/log/wtmp files.


Note on the difference between who and whoami.

The who command will always display the account that you used to login (the real user info).

The whoami command will show your effective user.

For example, if you login as "blammy", the who and whoami commands will display something like this:

> who
blammy  pts/0  2011-04-23 13:43 (123.23.123.123)

> whoami
blammy

This indicates that user "blammy" logged in on 23 Apr 2011 at 13:43 from ip "123.23.123.123".

If you then run su - kapow, you change your effective user to be "kapow".

Now, the who and whoami commands will display something like this:

> who
blammy  pts/0  2011-04-23 13:43 (123.23.123.123)

> whoami
kapow

Notice that the who info stays the same, but the whoami info changes based on the su.