A tiny command that tells which user started the login session. 04.04.2026 | reading time: 2 min Ever needed the original login name of a session? Use `logname` to print the user who started the login session; it is small, specific, and handy in scripts. Quick demo Case example: an admin connected via SSH as user devops — running `logname` returns the session owner, for instance: `logname` -> `devops`. Pitfalls and behavior Under the hood `logname` calls the system getlogin() which consults the utmp login records, so it can fail in non-login contexts like cron jobs, containers, or shells started with `su` without a dash and then exits nonzero; `whoami` on the other hand reports the effective user from the UID and is more robust. Practical uses In scripts capture the login name with `who_logged=$(logname)` to build filenames or audit messages, but provide a fallback such as `who_logged=$(logname) || who_logged=$(whoami)` to avoid surprises when `logname` cannot determine a name. Complementary tools Combine `whoami` to see the effective identity, `id` to inspect UID and groups, and `who` or `w` to view active login sessions when diagnosing mismatches between login name and effective user. Wrap-up Small commands solve specific problems; `logname` gives the original login name when available, but always handle its failure in scripts — keep exploring Linux and consider certifications like CompTIA Linux+ or LPIC-1 with intensive exam preparation at bitsandbytes.academy. Join Bits & Bytes Academy First class LINUX exam preparation. utilities processes scripting