If I ssh to a linux server, and use
whoami, it will print effective userid:
$ whoami bo
And if I su to root, then do the
whoami again, it will print
$ whoami root
who will show who is logged on.
$ who bo pts/0 2020-01-09 11:33
Even if I already su to root:
$ su $ whoami root $ who bo pts/0 2020-01-09 11:33
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
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 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
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
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.
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
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
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