Linux Command Reference ยท Permissions and Users
Linux whoami command
Reference for the Linux whoami command: print the effective user name, how it differs from id, logname and $USER, and how it behaves under sudo and in containers.
whoami prints the user name that matches the effective user ID of the process. It is the shortest way to see which account a command is really running as, and it is equivalent to id -un.
The word "effective" matters. Under sudo whoami prints root, even though you logged in as someone else, because sudo runs the command with root's effective ID. To see the user who originally logged in, use logname or the SUDO_USER variable.
whoami
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
whoami [OPTION]
| Name | Description |
|---|---|
--help |
Show usage. |
--version |
Show the version. |
There are no other options. whoami takes no arguments.
Returns: 0 and prints the name; 1 with whoami: cannot find name for user ID N when the effective ID has no entry in the user database.
Usage notes
- Same as
id -un.whoamiexists in GNU coreutils, BSD and busybox;id -unis the POSIX way and works everywhere. - Not the same as
logname.lognameprints the login name recorded for the terminal session. Aftersuorsudo,whoamireports the target user andlognamethe original one. - Not the same as
$USER. The variable is set by the login program and can be changed or missing (cron and containers often do not set it).$EUID,id -uandwhoamiread the kernel's state. who am i(two words) is a different command: it shows the login session line fromwho, with terminal and login time, and prints nothing outside a login session.- Containers. A container started with
--user 1234often has no/etc/passwdentry for that number, sowhoamifails;id -ustill works. - Files you create belong to the effective user, which is why
sudo touch fmakes a root-owned file. - In scripts, compare numbers (
[ "$(id -u)" -eq 0 ]) rather than the stringroot, since a different account can hold UID 0. - Set-user-ID programs run with a different effective ID;
whoamicalled from inside one shows the program owner.
Related
id,groups,whoandw,chown- Lessons: Users, Groups and sudo, Environment variables