In Linux, the “whoami” command is utilized to display the username which is currently logged into the system. This command is the combination of “who”, “am”, and “i” strings that are concatenated with each other. In certain situations, users require a current username to access root privileges or authentication.
By considering its needs, this guide is to demonstrate the “whoami” command with practical implementation in the Linux system.
The content that supports this guideline is as follows:
- How Does the “whoami” Command Work in Linux?
- Check out the Current Username
- Check an Account for sudo Permissions
- Switch to a New User
- Check the Login Date and Time
How Does the “whoami” Command Work in Linux?
In Linux, the administrators require the name of logged-in users to authorize the privileges. To know the information of the current user, the “whoami” command facilitates by the following syntax:
General Syntax
whoami [option]
In the above syntax, the “whoami” command tells us the username who is currently logged into the system. Different options of the “whoami” command are enlisted below:
- version: It represents the currently installed version of “whoami” in the operating system.
- help: This option assists various usage of the “whoami” command.
Let’s practice the “whoami” command.
Check the whoami Version
To check the current version of the “whoami” command, the “–version” utility is executed as below:
$ whoami --version
The output shows that “whoami (GNU Coreutils) 8.32” is installed in the current operating system.
Example 1: How do You Check out the Current Username?
To display the currently logged-in username, execute the “whoami” command as shown below:
$ whoami
The output shows the existing username “itslinuxfoss” that is currently logged in the operating system.
Example 2: How to Check an Account for Sudo Permissions?
To check the administrator privileges of the currently logged-in user, the “whoami” is used with the “sudo” utility. The below script will require the authentication to display the sudo permission:
$ sudo whoami
The above output confirms that the logged-in user has all the root privileges.
Example 3: How do You Switch to a New User and Display the Username?
An interesting example is creating and switching to the newly created user and then displaying the username using the “whoami” command.
To create and switch to a new user:
$ sudo su user_2
It requires a new password for using the administrator privileges.
To display the newly switched username, execute the “whoami” command:
$ whoami
Bonus Tip
Users can utilize the “w” and “who” commands separately, having their own functionalities as described below:
W command
It specifies the details of the logged-in user and current operations.
Example: How to Check the Information of Current User?
The “w” command provides detailed information about the logged-in user with login time:
$ w
The output shows that the current user was logged in “30” seconds ago.
Who command
It represents the user’s name that currently has permission to access the terminal.
Example: How to Check the Login Date and Time?
To find out the login time of the current user the “who” command is used. It displays the username with login time and date as below:
$ who
The output shows that the current user “itslinuxfoss” was logged in to the system on the date “2022-12-08” and time “23:50”.
Conclusion
In Linux, the “whoami” command displays the username currently logged into the operating system. This command is significant for installing packages requiring a username or accessing the root privilege. Additionally, you can check out the date and time when the user was logged in to the system using the “who” command. This guide has explained the “whoami” command with possible examples in Linux.