Home > Environment & Utility > whoami

whoami: Display the Current Logged-in Username

The `whoami` command prints the effective username of the current logged-in user. It is useful for quickly verifying which user is executing commands.

Overview

whoami is an abbreviation for 'Who am I?', a command that prints the account name of the user currently executing commands in the shell. It helps to clearly identify which user has privileges when using the `sudo` command or switching to another user (`su`). This command prints the username based on the value of the `$EUID` environment variable and returns the same result as `id -un`.

Key Features

The main features of the `whoami` command are as follows:

  • Prints the name of the currently logged-in user.
  • Prints the effective user ID, which may differ from the actual logged-in user (`logname`).
  • Often used in scripts to check the current executing user.
  • A simple command with very few options.

Differences Between whoami and who

Both `whoami` and `who` display user information, but they differ in the scope of information provided.

  • whoami: Only prints the name of the user currently executing the command.
  • who: Prints the names, login times, terminal information, and more for all users currently logged into the system.

Main Options

The `whoami` command is very simple, primarily using basic help options like `-h` or `-v`.

1) Help

Generated command:

Try combining the commands.

Description:

`whoami` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Learn the functions of the `whoami` command through various usage examples.

Check Current Username

whoami

Prints the name of the currently logged-in user.

Check After Switching to Root User

whoami
sudo whoami

After gaining root privileges with the `sudo` command, running `whoami` will output `root` as the effective username currently executing the command.

Check After Switching to Another User

whoami
su guest
whoami

After switching to another user, `guest`, with the `su` command, running `whoami` will output `guest`.

Installation

whoami is part of the `coreutils` package and is included by default in most Linux distributions. No separate installation is required.

Tips & Cautions

Here are some points to note when using the `whoami` command.

Tips

  • In scripts, you can use `if [ "$(whoami)" == "root" ]; then ... fi` to check if the current user is `root`.
  • `whoami` outputs the same information as `id -un`. The `id` command provides more detailed user information, such as UID and GID.

Related commands

These are commands that are functionally similar or are commonly used together.


Same category commands