Skip to content

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

logname
# Output: faruk

logname vs whoami vs id

This is the critical distinction.

  1. Login as faruk.
  2. Switch to root via sudo 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

  • logname reads from systems utmp file. If no entry exists (e.g., Inside a Docker container with no login), it prints logname: no login name.