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
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:
whoamishows the effective user. If a binary has the SUID bit set,whoamireturns the owner of the file, not the user running it.