I recently found out that sudo whoami
returns the root user whereas sudo who am i
return the currently logged-in user.
What could be the reason for this?
Asked
Active
Viewed 1,318 times
0

Macdonald
- 880
- 7
- 21
-
Because that's the way the commands were designed. Sometimes you want to know whose privileges you're executing under, and sometimes you want to know who the actual shell session user is. – Noah Mar 19 '21 at 14:56
-
More info: [Identify user in a Bash script called by sudo](https://stackoverflow.com/questions/3522341/identify-user-in-a-bash-script-called-by-sudo) – Noah Mar 19 '21 at 14:56
-
1`sudo who mom likes` is special too :) – Niloct Mar 19 '21 at 14:58
-
Any usual bash commands should have a man page. `man whoami` and `man who` (even `man who am i`) would print the man pages – yunzen Mar 19 '21 at 15:00
-
@Niloct I never knew it was special. `sudo who mom likes` shows the current user too – Macdonald Mar 19 '21 at 15:08
-
@MacDonald I meant special in sense of funny. Both commands returns the current logged user associated with the standard input because of the assumed `-m`. Check `who --help`. – Niloct Mar 19 '21 at 15:13
1 Answers
1
The command who lists the currently logged in user.
The command whoami show the current user.
Since you are the root user via sudo, sudo whoami
will list root.
sudo who
will list the currently logged in users.
sudo who am i
will implicate option -m
which is
-m only hostname and user associated with stdin
So this will only filter the one user who are you from all the other users currently logged in.

yunzen
- 32,854
- 11
- 73
- 106