Skip to content

whoami Command Cheat Sheet

whoami prints the effective username of the current user when invoked.


Synopsis

whoami [OPTION]...

Basic Usage

whoami
# Output: faruk

If running with sudo:

sudo whoami
# Output: root


Why Use It?

Verifying Permissions in Scripts

It is often used in shell scripts to check if the script is running as root.

if [ "$(whoami)" != "root" ]; then
    echo "Script must be run as root."
    exit 1
fi
Note: A more portable way is checking id -u (returns 0 for root).


Comparison

Command Output Description
whoami root Effective User ID (EUID)
logname faruk Initial Login Name (even if sudo was used)
id -un root Same as whoami

Notes

  • EUID: whoami shows the effective user. If a binary has the SUID bit set, whoami returns the owner of the file, not the user running it.