Overview
The `logname` command displays the name used by a user to start a login session on the system to standard output. This has a subtle but important difference from `whoami`. While `whoami` outputs the 'effective user' name currently executing the command, `logname` outputs the 'real login user' name of the session. Therefore, even after obtaining another user's privileges with `sudo` or `su` commands, `logname` will still show the name of the originally logged-in user.
Key Features
The main features of the `logname` command are as follows:
- Prints the user name that initiated the login session.
- Operates based on the value of the `$LOGNAME` environment variable.
- Used to verify the actual logged-in user.
- A simple command with very few options.
Difference between logname and whoami
Both commands output user names, but show different results in privilege change scenarios.
- logname: Outputs the real logged-in user name. It does not change after using `su` or `sudo`.
- whoami: Outputs the effective user name currently executing the command. After using `su` or `sudo`, it outputs the changed user name, such as `root`.
Main Options
The `logname` command is very simple, primarily using basic help options like `--help` or `--version`.
1) Help
Generated command:
Try combining the commands.
Description:
`logname` Executes the command.
Combine the above options to virtually execute commands with AI.
Usage Examples
Explore various usage examples of the `logname` command to understand its functionality.
Check Current User Name
logname
Prints the name of the currently logged-in user.
Compare User Names After su Command
logname
su
whoami
logname
After switching to the `root` user with the `su` command, compare the output of `whoami` and `logname`. `whoami` outputs `root`, but `logname` outputs the original logged-in user name.
Installation
`logname` is part of the `coreutils` package and is included by default in most Linux distributions. No separate installation is required.
Tips & Considerations
Here are some points to consider when using the `logname` command.
Tips
- In scripts, it is recommended to use `whoami` to check the privileges of the user currently executing the command, and `logname` to record the actual user who logged into the system.
- `logname` can be used to track the real logged-in user, similar to the output of the `w` command.