Whoami command in Linux with Examples

The Linux operating system, regardless of which distro you’re using, has tons of diverse commands baked into it. Some of these can get incredibly complex, and some are… extremely simple. The whoami command definitely qualifies as the latter.

Despite being a very simple command, whoami can come in handy all the time. If you can’t already tell by its name, the whoami command just outputs the username of whichever account you’re logged into. That is, the effective user. So, running sudo whoami is going to give you a different result than just running the command as a normal user.

Follow along with the screenshots and examples below to quickly master the whoami command and understand exactly how it works.

In this tutorial you will learn:

  • How to use the whoami command on Linux
How to use whoami Linux command
How to use whoami Linux command
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software whoami
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Intro to whoami Linux command

The whoami command prints the effective user. We’ll explain how to use it in the examples below.

Running the whoami command with three different users
Running the whoami command with three different users
  1. whoami doesn’t accept any extra options. You just execute the command by itself.
    $ whoami
    linuxconfig
    

    As you can see from the output, we have executed the command as user linuxconfig.

  2. Running the command as a different user will obviously yield a different result. The easiest way to see this for yourself is by executing the command with sudo or while logged into the root account.
    $ sudo whoami
    root
    

    Since whoami shows our effective user id, it says that we are root, even though we only used sudo to execute the command and never actually logged into the account.

  3. You may be interested to know that the id -un command does the exact same thing. But, you’ll probably find whoami a lot easier to remember.
    $ id -un
    linuxconfig
    
  4. You can also use the --help or --version options with the command. I’m not really sure why anyone would, or if there’s anyone that can claim they actually have. But in the interest of completeness, we’ve included them in this guide.
    $ whoami --help
    AND
    $ whoami --version
    

Closing Thoughts

Although simple, whoami is very useful when you’re sitting at a blank prompt and you aren’t sure which user you are logged into. It can also be useful in Bash scripting if you need the script to run a test and see which user is running the script. For example, the command can test to see if root is executing the script, and if so, give an error that it should only be run as a normal user.



Comments and Discussions
Linux Forum