Skip to content
Tutorials Center
My learning

Topics

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.

Terminal
whoami

Try it

Bash Who am I, in three ways

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.

Bash Using whoami safely in a script

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

Terminal
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. whoami exists in GNU coreutils, BSD and busybox; id -un is the POSIX way and works everywhere.
  • Not the same as logname. logname prints the login name recorded for the terminal session. After su or sudo, whoami reports the target user and logname the 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 -u and whoami read the kernel's state.
  • who am i (two words) is a different command: it shows the login session line from who, with terminal and login time, and prints nothing outside a login session.
  • Containers. A container started with --user 1234 often has no /etc/passwd entry for that number, so whoami fails; id -u still works.
  • Files you create belong to the effective user, which is why sudo touch f makes a root-owned file.
  • In scripts, compare numbers ([ "$(id -u)" -eq 0 ]) rather than the string root, since a different account can hold UID 0.
  • Set-user-ID programs run with a different effective ID; whoami called from inside one shows the program owner.