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
whoamicommand on Linux
|Category||Requirements, Conventions or Software Version Used|
|System||Any Linux distro|
|Other||Privileged access to your Linux system as root or via the
|Conventions||# – requires given linux commands to be executed with root privileges either directly as a root user or by use of
$ – 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.
- 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
- 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
sudoor while logged into the root account.
$ sudo whoami root
whoamishows our effective user id, it says that we are root, even though we only used
sudoto execute the command and never actually logged into the account.
- You may be interested to know that the
id -uncommand does the exact same thing. But, you’ll probably find whoami a lot easier to remember.
$ id -un linuxconfig
- You can also use the
--versionoptions 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
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.