Skip to main content

Module 6: User & Permission Management

Lesson 59: whoami Command

In this lesson, you'll learn how to use the whoami command to display the current logged-in username in Linux, and more importantly, how to use it inside scripts to control execution based on who is running them.

The whoami command prints the effective username of the currently logged-in user.

It is one of the smallest but most useful identity commands in Linux, especially when working with sudo, shell scripts, and automated jobs where the running user determines what the script is allowed to do.

Syntax

whoami [OPTIONS]

Options

Option Description
--help Display help information and exit
--version Output version information and exit

1. Display the Current Logged-In User

The most basic use run whoami without any arguments to print the username of the current effective user.

whoami
ravi

This tells you exactly which user account the current shell session belongs to.

Useful when you are switching between multiple accounts and lose track of which terminal belongs to which user.

2. Check if You Are Running as Root

When you prefix a command with sudo, Linux temporarily elevates your privileges to root.

Running whoami under sudo confirms that elevation is active.

sudo whoami
root

Without sudo, the same command returns your regular username. This distinction matters in scripts where certain operations must be run as root.

Note: whoami reports the effective user, not the login user. If you run sudo whoami, it returns root even though you logged in as a regular user.

To see the original login user regardless of sudo, use logname instead.

Pro TecMint Β· Root Plan
This Article is for Root Members
Join Root to read the full article and unlock everything

Full Access to Every Article, Course & Certification Track

Join thousands of Linux professionals who use Pro TecMint to advance their careers.

Ad-free access to all premium articles
All courses: Learn Linux, Bash, Golang, Ubuntu and more
RHCSA, RHCE, LFCS & LFCA certification prep
New courses added every month
Private Telegram community & priority support
Root Plan
$8/mo
or $59/year billed annually
Save $37 with annual plan
Start Reading This Article in the Next 60 Seconds
Join Root Plan β†’