DEV Community

BC
BC

Posted on

Differences between `whoami`, `who` and `w` - Linux Tips

whoami

If I ssh to a linux server, and use whoami, it will print effective userid:

$ whoami
bo
Enter fullscreen mode Exit fullscreen mode

And if I su to root, then do the whoami again, it will print root:

$ whoami
root
Enter fullscreen mode Exit fullscreen mode

who

who will show who is logged on.

$ who
bo   pts/0        2020-01-09 11:33
Enter fullscreen mode Exit fullscreen mode

Even if I already su to root:

$ su
$ whoami
root
$ who
bo   pts/0        2020-01-09 11:33
Enter fullscreen mode Exit fullscreen mode

who am i

Huh? I heard who, but I don't know you can run who am i.

$ who am i
bo   pts/0        2020-01-09 11:33
Enter fullscreen mode Exit fullscreen mode

And what's the difference with who? The answer is, who will show all users who has a login-tty, and who am i will only show the current login-tty user. Say if I ssh'ed to server in another terminal window, then run who and who am i:

$ who
bo   pts/0        2020-01-09 11:33
bo   pts/1        2020-01-09 11:45
$ who am i
bo   pts/0        2020-01-09 11:33
Enter fullscreen mode Exit fullscreen mode

Now you can see the difference. Ok, if this weird who am i works, how about who are you? Let's try:

$ who are you
bo   pts/0        2020-01-09 11:33
$ who is this
bo   pts/0        2020-01-09 11:33
$ who logged on
bo   pts/0        2020-01-09 11:33
Enter fullscreen mode Exit fullscreen mode

They all print the same thing as who am i! Actually as long as there are 2 parameters followed by who, it will print the current login-tty user, no matter what the parameters are.

But you can't run it with 3 parameters, it will print errors:

$ who is this tty
who: extra operand ‘tty’
Try 'who --help' for more information.
Enter fullscreen mode Exit fullscreen mode

w

w will Show who is logged on and what they are doing, so it is like who, plus the "what they are doing" info:

$ w
12:12:01 up 78 days, 15:52,  2 users,  load average: 0.25, 0.34, 0.29
USER TTY      FROM          LOGIN@  IDLE   JCPU   PCPU  WHAT
bo   pts/0    67.xx.xx.xx   11:33   0.00s  0.13s  0.00s sshd: bo[priv]
bo   pts/1    67.xx.xx.xx   11:45   26:49  0.04s  0.04s -bash
Enter fullscreen mode Exit fullscreen mode

Then can we use w am i? let's try it:

$ w am i
12:15:25 up 78 days, 15:55,  2 users,  load average: 0.08, 0.22, 0.25
USER TTY      FROM          LOGIN@  IDLE   JCPU   PCPU  WHAT
Enter fullscreen mode Exit fullscreen mode

You can see it only prints the header info, but no users in the USER section. This is because with extra parameter, w will only take the first parameter as a user id and print user with that id. So for w am i, it will try to find a user with user id am, if this user exists and has a login-tty open, print it out, otherwise show nothing.

$ w bo
12:17:01 up 78 days, 15:57,  2 users,  load average: 0.25, 0.34, 0.29
USER TTY      FROM          LOGIN@  IDLE   JCPU   PCPU  WHAT
bo   pts/0    67.xx.xx.xx   11:33   0.00s  0.13s  0.00s sshd: bo[priv]
bo   pts/1    67.xx.xx.xx   11:45   26:49  0.04s  0.04s -bash
Enter fullscreen mode Exit fullscreen mode

Top comments (12)

Collapse
 
ghost profile image
Ghost

The cli is a more modern version of Alexa, you don't even have to talk to it, you just type it. And some of us are more comfortable writing than talking :)

Collapse
 
bitecode profile image
BC

haha, among the 3 big philosophy questions: "Who am I? Why am I here? Where do I find meaning?" Apparently cli is modern and smart enough to answer one of them - who am i -, it won't take long to answer the rest two. (I hope the answer is not 42) 😊

Collapse
 
ghost profile image
Ghost

It's kinda interesting tho, I'm not sure Adams was aware of the phenomena whein made the 42; with AI we put the question and get the answer, no clue about the process or rationale behind it; we'll know the answers not knowing why, historically the usual process was scientists discovered something and engineers behind looked for uses; that will probably invert in the future, engineers with AI will get answers and scientists behind will try to understand why did it work, like archeology. God indeed will work in misterious ways. And could it be a de-facto god if we made it? what would make us?, and to be this kind of all (or almost) know all "god" the AI needs to have what we see as concience? have to even pass the Turing test? could it be a dumb god? that knows everything but itself?...

... ok, that was way to much coffee for me, I'm gonna take a cold shower now.

Thread Thread
 
bitecode profile image
BC

that will probably invert in the future, engineers with AI will get answers and scientists behind will try to understand why did it work ...

I think it is already happening, one of my professor who has CS and Math background is working on explaining the deep learning network with Optimal Mass Transportation Theory, Convex Geometry and Monge-Ampere Equation.

Besides that, interesting thoughts 🤔

Collapse
 
tkdmzq profile image
TKDMzq

I never was so offended by something that I agree with. Some of us just can't talk to human beeings.

Collapse
 
ghost profile image
Ghost

hahaha, I fear the day AI gets "human" enough to make those interactions akward too :D

Collapse
 
moopet profile image
Ben Sinclair

Gonna throw finger into the mix?

Collapse
 
bitecode profile image
BC

oh, I never used finger before, thank you Ben for sharing this.

Seems it doesn't come with the default shell commands set, need to do apt install finger, and the output seems similar to w? Can you share more of the benefits using it compare to w if you don't mind?

Collapse
 
moopet profile image
Ben Sinclair

It used to be used all the time, but it's fallen out of favour. It's insecure, it was a plain-text client-server system so you could say finger ben@scramble.moopet.net or even just finger @scramble and it would interrogate the finger daemon on that host. If you set a file in your home directory called .plan it would display as part of the finger output:

$ finger teasmade@scramble.
Login: teasmade                 Name: Teasmade
Directory: /home/teasmade               Shell: /bin/bash
Last login Mon Feb 15 10:53 2016 (EST) on pts/0 from x.x.x.x
No mail.
Plan:
Make tea

There's a good overview of it here: computerhope.com/unix/ufinger.htm

Benefits include being able to query remote hosts, and being able to control what people can see about you by editing files in your home.

There's also the last command, which will give a listing of who was logged in:

$ last

moopet   pts/0        192.168.0.16     Sun Aug  4 19:20 - 19:21  (00:01)
moopet   pts/0        192.168.0.16     Sun Aug  4 19:19 - 19:20  (00:00)
reboot   system boot  5.2.5-arch1-1-AR Sun Aug  4 18:49 - 13:32 (10+18:43)
moopet   pts/0        192.168.0.16     Sun Aug  4 17:40 - 17:40  (00:00)
moopet   pts/0        192.168.0.16     Sun Aug  4 17:39 - 17:39  (00:00)
root     tty1                          Sun Aug  4 17:32 - 17:42  (00:09)
reboot   system boot  5.2.5-arch1-1-AR Sun Aug  4 17:32 - 17:42  (00:09)
Thread Thread
 
bitecode profile image
BC

I see, "being able to query remote hosts" this would be useful in some cases, gonna read more on it, thank you Ben :D

Collapse
 
codemouse92 profile image
Jason C. McDonald

This is really cool!

Collapse
 
bitecode profile image
BC

thank you Jason :D