Linux whoami command

Linux whoami command

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

whoami

This command returns the username of the currently active user.

Example Usage

1. Check the Current User

Run:

whoami

Output:

john

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:

su - anotheruser whoami

Output:

anotheruser

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:

#!/bin/bash USER=$(whoami) echo "You are logged in as $USER"

4. Check the User Running a Command

To confirm which user is executing a command, use:

sudo whoami

Output:

root

This indicates that the command was executed with root privileges.

Difference Between whoami, who, and id

CommandDescription
whoamiDisplays the currently logged-in user.
whoLists all logged-in users on the system.
idShows 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? 🚀

Souy Soeng

Souy Soeng

Our website teaches and reads PHP, Framework Laravel, and how to download Admin template sample source code free. Thank you for being so supportive!

Github

Post a Comment

CAN FEEDBACK
close