Linux whoami Command – Check the Current User
The whoami command in Linux is a simple yet useful utility that displays the currently logged-in user. It is often used in scripts and troubleshooting to verify user privileges.
Syntax
This command returns the username of the currently active user.
Example Usage
1. Check the Current User
Run:
Output:
This indicates that the logged-in user is john.
2. Verify User Identity After Switching Users
If you use the su command to switch to another user:
Output:
This confirms that the session is now running under anotheruser.
3. Use whoami in a Shell Script
You can integrate whoami into a script for verification:
4. Check the User Running a Command
To confirm which user is executing a command, use:
Output:
This indicates that the command was executed with root privileges.
Difference Between whoami, who, and id
| Command | Description |
|---|---|
whoami | Displays the currently logged-in user. |
who | Lists all logged-in users on the system. |
id | Shows user ID (UID), group ID (GID), and group memberships. |
Conclusion
The whoami command is a quick and easy way to check the current user session in Linux. It is especially useful when working with multiple user accounts or running scripts that require user verification.
Would you like additional details or modifications for SEO optimization? š
