Linux whoami Command
whoami is a very simple Linux command and can be used to find out the name of user who is currently logged in the terminal i.e. if you are logged in to the system with username ‘john’ then the output of the whoami will just display back ‘john’ on the screen. whoami is different from who as who displays all users who are logged in the system.
# whoami john
# who john pts/0 2020-05-03 06:18 (192.168.2.21) john pts/1 2020-05-04 03:22 (192.168.2.21) ...
whoami command really comes in handy when you are working on a system that does not display the name of the current user in the shell prompt. Surprisingly, the “whoami” command does not carry any complex switches, the only two options available with it are:
# whoami --help # whoami --version
# whoami --help Usage: whoami [OPTION]... Print the user name associated with the current effective user ID. Same as id -un. --help display this help and exit --version output version information and exit GNU coreutils online help: [http://www.gnu.org/software/coreutils/] For complete documentation, run: info coreutils 'whoami invocation'
# whoami --version whoami (GNU coreutils) 8.22 Copyright (C) 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http: gnu.org="" licenses="" gpl.html="">. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Richard Mlynarik.</http:>
whoami V/s logname
The whoami command prints the name of the current, effective user. This may differ from your login name (the output of logname) if you’ve used the sudo command. The following example distinguishes whoami from logname. In normal situations, they both print your username:
# logname john
# whoami smith
When you become the root user via sudo, then effectively you are the root user, and whoami indicates this:
# sudo logname smith
# sudo whoami root