logname
Print the user's login name
TLDR
Display the currently logged in user's name
SYNOPSIS
logname [OPTION]...
PARAMETERS
--help
Display a help message and exit.
--version
Output version information and exit.
DESCRIPTION
The logname command is a standard Unix utility used to print the login name of the current user. It retrieves this information from the user's login record, typically from the utmp or wtmp files, which record logins and logouts. This name is established at the time of the initial login session.
Unlike whoami or id -un, which report the effective username (which can change if a user switches identity using commands like su or sudo), logname typically reports the name of the user who initially logged in to the current controlling terminal. This distinction is crucial in scenarios where a script or application needs to identify the original user, rather than the current effective user. The command outputs the login name to standard output, followed by a newline character. It exits with a status code of 0 upon successful execution and a non-zero status code if an error occurs. logname is part of the POSIX standard utilities.
CAVEATS
The logname command distinguishes itself from similar commands like whoami or id -un. While the latter two report the effective username (which can change after using su or sudo), logname aims to report the name of the user who initially logged in to the current controlling terminal. This relies on accurate login records (e.g., utmp file) and may not provide meaningful output in contexts without a controlling terminal, such as cron jobs or system services.
Furthermore, if the LOGNAME environment variable is manually altered, some implementations might prioritize it over the actual login record, leading to potentially misleading results, although the POSIX standard specifies it should derive from the login record.
<B>EXIT STATUS</B>
The logname command exits with status 0 upon success (login name found) and a non-zero status code if an error occurs or the login name cannot be determined.
<B>OUTPUT FORMAT</B>
The command prints the determined login name to standard output, followed by a single newline character.
HISTORY
The logname command is a fundamental utility that has been a part of Unix-like operating systems for a long time, dating back to early AT&T Unix versions. Its inclusion in the POSIX.1 standard (IEEE Std 1003.1) underscores its importance as a consistent and reliable way to retrieve the user's login identity. It was designed to provide the name of the user associated with the current login session, distinct from the effective user ID which might change due to su or sudo. GNU coreutils provides a common implementation of logname for Linux systems, aligning with the POSIX specification while adding common --help and --version options.