logname Command Cheat Sheet
The logname command prints the name of the user who originally logged in to the current session (from /var/run/utmp).
Synopsis
logname
Usage
Print Login Name
logname
# Output: faruk
logname vs whoami vs id
This is the critical distinction.
- Login as
faruk. - Switch to
rootviasudo su.
whoami
# Output: root (Who am I effectively right now?)
logname
# Output: faruk (Who owns the login session?)
id -un
# Output: root
Use logname in scripts when you need to know the "real" human behind the sudo command, even after they switched users.
Exit Status
| Code | Meaning |
|---|---|
| 0 | Success |
| 1 | Information not available (e.g., cron job or non-interactive shell) |
Notes
lognamereads from systemsutmpfile. If no entry exists (e.g., Inside a Docker container with no login), it printslogname: no login name.