Recent from talks
Nothing was collected or created yet.
Whoami
View on Wikipedia| whoami | |
|---|---|
coreutils whoami command | |
| Developers | Bill Joy, Richard Mlynarik, Intel, Microsoft, ReactOS Contributors, Novell |
| Operating system | Unix, Unix-like, iRMX 86, Windows, ReactOS, NetWare |
| Platform | Cross-platform |
| Type | Command |
| License | BSD: BSD License coreutils: GPLv3 iRMX 86, Windows, NetWare: Proprietary commercial software ReactOS: GPLv2 |
In computing, whoami is a command found on most Unix-like operating systems, Intel iRMX 86, every Microsoft Windows[1] operating system since Windows Server 2003, and on ReactOS. It is a concatenation of the words "Who am I?" and prints the effective username of the current user when invoked.
Overview
[edit]
whoami commandThe command has the same effect as the Unix command id -un. On Unix-like operating systems, the output of the command is slightly different from $USER because whoami outputs the username that the user is working under, whereas $USER outputs the username that was used to log in. For example, if the user logged in as John and su into root, whoami displays root and echo $USER displays John. This is because the su command does not invoke a login shell by default.
The earliest versions were created in 2.9 BSD as a convenience form for who am i, the Berkeley Unix who command's way of printing just the logged in user's identity. This version was developed by Bill Joy.[2]
The GNU version was written by Richard Mlynarik and is part of the GNU Core Utilities (coreutils).
The command is available as a separate package for Microsoft Windows as part of the GnuWin32 project[3] and the UnxUtils collection of native Win32 ports of common GNU Unix-like utilities.[4]
On Intel iRMX 86 this command lists the currents user's identification and access rights.[5]
The command is also available as part of the Windows 2000 Resource Kit[6] and Windows XP SP2 Support Tools.[7]
The ReactOS version was developed by Ismael Ferreras Morezuelas and is licensed under the GPLv2.[8]
This command was also available as a NetWare-Command residing in the public-directory of the fileserver. It also outputs the current connections to which server the workstation is attached with which username.
Example
[edit]Unix, Unix-like
[edit]# whoami
root
Intel iRMX 86
[edit]--WHOAMI
USER ID: 5
ACCESS ID'S: 5, WORLD
Windows, ReactOS
[edit]C:\Users\admin>whoami
workgroup\admin
See also
[edit]References
[edit]- ^ "Microsoft TechNet Whoami article". Archived from the original on 2008-05-04. Retrieved 2008-04-22.
- ^ 2.9.1BSD Manual Page
- ^ CoreUtils for Windows
- ^ Native Win32 ports of some GNU utilities
- ^ iRMX™86 INTRODUCTION AND OPERATOR'S REFERENCE MANUAL For Release 6
- ^ Windows 2000 Resource Kit Tool: Whoami.exe
- ^ Windows XP Service Pack 2 Support Tools
- ^ "Reactos/Reactos". GitHub. 3 January 2022.
Further reading
[edit]- Barrett, Daniel J. (2012). Macintosh Terminal Pocket Guide: Take Command of Your Mac. O'Reilly. ISBN 978-1449328986.
- Stanek, William R. (2008). Windows Command-Line Administrator's Pocket Consultant, 2nd Edition. Microsoft Press. ISBN 978-0735622623.
External links
[edit]- – 4.2BSD General Commands Manual
- – FreeBSD General Commands Manual
- whoami | Microsoft Docs
Whoami
View on Grokipediasu or sudo.[2] The command is particularly useful in shell scripts for conditional logic based on the current user or for debugging authentication issues.[3]
As part of the GNU Core Utilities (coreutils) package, whoami was written by Richard Mlynarik and is distributed under the GNU General Public License version 3 or later.[1] In the GNU implementation, its basic syntax is whoami [OPTION]..., supporting only two options: --help to display usage information and --version to output the version details of the utility.[1] Functionally equivalent to id -un, whoami provides a concise alternative for retrieving just the username without additional user or group details that id might output.[1] The command is available across major distributions like Linux and FreeBSD, though implementations vary—for example, FreeBSD's version lacks options and is considered obsoleted by id -un.[4]
Unlike the related who command, which lists all logged-in users and their terminal sessions, whoami focuses solely on the invoking user's identity.[5] The command who am i can also be used to display login session details for the current terminal, but whoami is the dedicated tool for printing just the effective username.[6] In modern usage, it remains a fundamental diagnostic command for verifying user context in secure environments and automated workflows.[7]
Fundamentals
Purpose and Functionality
Thewhoami command is a command-line utility in Unix-like operating systems that outputs the effective username associated with the current process or user session.[1] It serves as a straightforward tool for identifying the user context under which a shell or script is executing, particularly useful in multi-user environments where processes may run under different identities.[8]
Its primary function involves querying the operating system's user identification mechanism to retrieve and display the username without requiring any additional input or arguments in its basic form.[1] This process enables quick verification of the active user, equivalent to invoking the id command with the -un options to print only the effective username.[9] By leveraging system calls such as geteuid() to obtain the numerical effective user ID (EUID), whoami ensures that the displayed name reflects the privileges and identity currently in effect for the process, which may differ from the real user ID in scenarios involving privilege escalation like su or sudo.[9]
The effective user ID (EUID) represents the user identity under which the current process operates, determining file access permissions and other security attributes.[9] To convert this numerical EUID into a human-readable username, whoami consults the system's user database, such as /etc/[passwd](/page/Passwd) in traditional Unix systems, which maps user IDs to corresponding login names.[9] This resolution mechanism allows the command to provide accurate output even when the process's effective identity has been altered from the original login session.
Introduced in early Unix systems as a simple diagnostic tool, whoami facilitates confirmation of user context in both interactive terminal sessions and shell scripts, such as checking for root privileges before executing sensitive operations.[2] Its origins trace back to the need for basic user introspection in command-line environments, though detailed historical development is covered elsewhere.
Etymology and Naming
The name "whoami" is a direct concatenation of the English phrase "who am I?", reflecting its purpose of querying the current user's identity in a command-line environment. This linguistic derivation emphasizes brevity and intuitiveness, aligning with the design philosophy of Unix commands that prioritize mnemonic abbreviations over verbose descriptors. For instance, similar to "pwd" (print working directory), the name "whoami" serves as a quick, self-explanatory shorthand that users can easily recall and type during interactive sessions.[10] The command was introduced in the early development of Berkeley Software Distribution (BSD) Unix, specifically first appearing in 1BSD in early 1978, attributed to Bill Joy, a key contributor to BSD.[11] The naming convention embodies the casual, hacker-culture ethos of the era, where commands were crafted for efficiency and wit among developers working in resource-constrained environments. This approach avoided more formal alternatives, such as fully spelled-out identifiers seen in some contemporary systems, fostering a playful yet functional nomenclature suited to rapid terminal interactions.[11] The choice of "whoami" also draws from the needs of early interactive computing, where users frequently required instant checks of their session identity amid multi-user setups, making the command's informal phrasing a natural fit for prompting self-inquiry without additional syntax. Unlike the related "who am i" arguments to the who command, which originated in early BSD Unix (1BSD, 1978) for displaying the current user's login details, "whoami" streamlined the output to just the username.[11] This etymological design has endured, influencing user identification tools across subsequent Unix-like systems.History
Origins in Early Unix
Thewhoami command first appeared in 1979 as part of the 2BSD distribution, an early Berkeley Software Distribution built upon AT&T's Version 7 Unix, developed at the University of California, Berkeley by Bill Joy.[12] It was included among the standard user utilities in the 2BSD Programmer's Manual, listed under Section 1 commands, to provide a concise way to display the effective username associated with the current process.
This utility emerged during the rapid evolution of Unix in the late 1970s. Unix originated from the Multics project, where Bell Labs researchers Ken Thompson and Dennis Ritchie created a more accessible multi-user time-sharing system after Multics' complexity proved prohibitive. As Unix gained traction for shared computing environments, simple tools for user identity verification became essential to manage access and processes in time-sharing setups, filling a gap left by broader commands like who. Whoami thus addressed the practical demands of early Unix users needing quick, reliable self-identification without relying on session-specific data.
The command's initial implementation utilized the getuid() system call—introduced in Version 7 Unix—to fetch the effective user ID, then consulted the /etc/passwd database to resolve it to a username, ensuring accuracy even after privilege escalations via tools like su. This design contrasted with the who am i option in the who command, which drew from /etc/utmp and could falter in non-interactive contexts.[13]
Within early shell scripting environments, such as the Bourne shell debuted in Version 7 Unix, whoami facilitated conditional logic based on user identity, supporting rudimentary scripts for access control and automation before the advent of sophisticated authentication systems.
Evolution Across Operating Systems
Following its origins in early Unix, the whoami command spread to Unix-like systems in the late 1980s, becoming a core utility in BSD distributions for displaying the effective username and ensuring consistent behavior across variants like FreeBSD and NetBSD.[4] In the 1990s, whoami was ported beyond Unix to non-Unix platforms, including Microsoft's Windows NT via the Resource Kit starting with Windows NT 4.0 in 1996 as a separate tool; it was fully integrated as a native command in Windows 2000 and later versions.[14] Embedded and real-time systems also adopted it during this period, with variants appearing in Intel's iRMX 86 real-time OS, where the command lists the current user ID and access rights, as detailed in operating system command references.[15] ReactOS, an open-source Windows-compatible OS, emulates whoami to maintain binary compatibility with NT-era applications and drivers.[16] During the 2000s, the GNU Coreutils project refined the Unix-like implementation, incorporating improved error handling for scenarios where user entries are unavailable or invalid, such as when getpwuid fails to retrieve details for the effective UID. whoami was included in the GNU shellutils package in the early 1990s and later integrated into coreutils. This evolution marked a shift from a proprietary Unix tool to an open-standard utility, promoting its inclusion in resource-constrained environments like Linux initramfs, where BusyBox provides a minimal whoami symlink for boot-time diagnostics.Syntax and Usage
In Unix-like Systems
In Unix-like systems, thewhoami command displays the username associated with the current effective user ID (EUID) of the process. Its basic syntax is whoami [options], and when invoked without options, it outputs the effective username on a single line. For instance, executing whoami in a terminal might produce output such as alice, indicating the effective user is alice.[1][17]
The command's behavior depends on underlying system calls: it uses geteuid() to obtain the EUID and getpwuid() to retrieve the corresponding username from the system's password database, such as /etc/passwd or an equivalent service like LDAP. For the root user (EUID 0), it outputs root. If the lookup fails—due to an invalid EUID, a missing entry in the password database, or configuration issues—it reports an error, typically in the form whoami: cannot find name for user ID <uid>, and exits with a nonzero status.[18][17]
While not mandated by POSIX, which standardizes the id utility for user and group identification, whoami is widely available and equivalent to id -un. In GNU/Linux implementations from coreutils (introduced in shellutils in 1992), it supports options like --help to print usage information and --version to display build details, but lacks POSIX-standardized flags for output formatting. BSD variants, such as FreeBSD, provide a minimal version with no options, explicitly marking it as obsolete in favor of id -un and recommending id -p for interactive use.[19][17][20]
In Linux environments, the GNU coreutils version of whoami, available since the early 1990s, incorporates internationalization support via mechanisms like gettext for its messages. This distinguishes it from the more comprehensive id command, which outputs full user and group details including IDs, whereas whoami outputs only the effective username for simplicity in scripting and quick checks.[17][21]
In Windows and Compatible Systems
Thewhoami command in Windows NT-based operating systems, including Windows 2000 and later versions, displays information about the currently logged-on user, such as the domain and username in the default format domain\username.[22] It is executed via the Command Prompt or PowerShell and integrates with the Windows Security Identifier (SID) system to provide details on user identity and access tokens.[22]
The full syntax is whoami [/upn | /fqdn | /logonid], whoami {[/user] [/groups] [/claims] [/priv]} [/fo <Format>] [/nh], or whoami /all [/fo <Format>] [/nh], where optional parameters allow customization of output.[22] Key options include /user, which shows the domain, username, and SID; /groups, which lists the user's security group memberships with their SIDs and attributes; and /priv, which enumerates enabled, disabled, and available security privileges.[22] The /all option provides comprehensive details, combining user information, groups, privileges, and claims (in supported versions).[22] Additional flags like /fo <Format> specify output formats (table, list, or CSV), while /nh suppresses column headers.[22]
Introduced in 2000 as part of the Windows 2000 Resource Kit and available for download in support tools for Windows XP, the command became built-in starting with Windows Server 2003 and Windows Vista.[23] This evolution reflects its role in troubleshooting user contexts within Windows domains, where it handles formats like DOMAIN\user for networked environments.[22]
In compatible systems like ReactOS, an open-source Windows clone, whoami emulates the Windows implementation to support compatibility testing, displaying current local user details, groups, and privileges using Windows API calls such as GetTokenInformation and ConvertSidToStringSidW.[16]
Implementations
POSIX Compliance and Variations
Although thewhoami utility is not specified in the POSIX.1-2017 standard (IEEE Std 1003.1-2017, also known as Issue 7 of The Open Group Base Specifications), it is widely implemented in Unix-like systems as a non-standard extension equivalent to the POSIX-required id -un command, which prints the current effective user name.[24] This equivalence ensures similar functionality for retrieving the effective user ID via the geteuid() system call, which is POSIX-compliant.[25]
Implementations in major Unix-like systems provide core functionality consistent with this equivalence, though some include extensions. In Linux distributions, the whoami command from the GNU coreutils package prints the effective user name without options.[17] It supports GNU-specific options such as --help for usage information and --version for version details. These extensions can be suppressed by setting the POSIXLY_CORRECT environment variable.[17] Similarly, Oracle Solaris provides a whoami equivalent to id -un, incorporating the same --help and --version options.[26]
In BSD-derived systems like macOS and FreeBSD, whoami is implemented minimally, equivalent to id -un, and is considered obsolete in favor of the more versatile id utility.[27][28] These implementations lack non-standard options and match the expected output of a simple user name. On macOS, user name resolution integrates with the Directory Services framework, including local or networked directories like Open Directory, without altering the output format.
This widespread implementation supports portability in shell scripts across Unix-like environments; for example, whoami > /tmp/user will capture the effective user name reliably in these systems. However, variations in non-Unix-like environments may include additional details, potentially affecting script compatibility.
Proprietary and Legacy Systems
In proprietary and legacy operating systems, thewhoami command or its equivalents were adapted to fit unique architectures and security models, often diverging from Unix syntax to emphasize system-specific identifiers.
The Intel iRMX 86 real-time operating system, introduced in the 1980s for embedded applications, included a whoami command documented in its command reference manuals.[29] This implementation used Unix-like syntax but output the user ID in decimal format, such as "User id: # 5", potentially including access IDs like "WORLD", to support debugging in resource-constrained, real-time contexts.[29]
In the Digital Equipment Corporation (DEC) VMS operating system, developed from the 1970s through the 2000s, no native whoami command existed in the DCL environment; instead, the equivalent functionality was provided by the F$USERNAME lexical function, which returned the current process's username as a string for use in command procedures.[30] However, Unix compatibility layers such as GNV (GNU for VMS), introduced in the 1990s, emulated the standard whoami command to deliver the effective username in a Unix-like manner within the OpenVMS ecosystem.[31]
Legacy systems like OS/2, active from 1987 to 2001, incorporated whoami through its POSIX.1 subsystem and compatibility layers, which supported Unix-like utilities and output the user identity from the authentication context.[32] In mainframes such as IBM z/OS, whoami is available within the UNIX System Services (USS) layer but not integrated into core TSO/E or MVS environments, reflecting the platform's emphasis on job control languages over interactive shells.[33]
These adaptations in proprietary environments often prioritized system-specific IDs, such as decimal user numbers or domain-integrated usernames, over portable standard usernames, facilitating debugging and access control in isolated, vendor-controlled ecosystems.
Security and Practical Considerations
Common Use Cases and Risks
Thewhoami command finds frequent application in shell scripting for debugging and conditional logic, particularly to verify the effective user identity before proceeding with sensitive operations. For instance, administrators often embed checks such as if [ "$(whoami)" = "root" ]; then echo "Running as [root](/page/Root)"; else echo "Insufficient privileges"; exit 1; fi to ensure scripts execute only under appropriate privileges, preventing unintended actions or failures in automated workflows.[7] This usage is especially valuable in system maintenance scripts where user context directly impacts functionality, such as configuring services or modifying system files.[2]
Beyond debugging, whoami supports logging and auditing in automation pipelines by capturing the executing user for traceability. In continuous integration or deployment scripts, it logs actions like "User $(whoami) initiated backup at $(date)" to maintain accountability without relying on external tools.[10] Similarly, in interactive sessions following privilege escalation via sudo, users invoke whoami to confirm their elevated status, ensuring subsequent commands align with intended permissions.[3]
Despite these benefits, deploying whoami introduces notable security risks, especially in web-exposed contexts. Attackers exploiting command injection vulnerabilities in web shells or misconfigured CGI scripts frequently execute whoami as an initial reconnaissance step to identify the server's effective user, revealing potential privilege levels for further exploitation.[34] For example, payloads like ; whoami appended to vulnerable inputs can bypass filters and output sensitive user details directly to the attacker.
In penetration testing scenarios, whoami serves as a standard post-compromise command to assess user context, integrated into frameworks like Metasploit since its early 2000s development, where reverse shells prompt immediate execution to map the environment.[35] This reconnaissance can escalate if outputs are captured in insecure logs, potentially enabling social engineering by exposing internal usernames or hierarchies to external parties.
To address these vulnerabilities, best practices recommend avoiding whoami in public-facing or user-input-handling scripts, where injection risks amplify information disclosure. In private automation without privilege escalation, favor environment variables like $USER for routine checks, as it avoids spawning a subshell process for improved performance and reduced overhead in high-frequency executions.[36] However, for verifying effective user privileges, whoami remains preferable due to its direct system call. Mitigation further involves deploying restricted shell environments, such as rbash configured with a limited PATH, which can prevent access to reconnaissance tools like whoami to contain breaches.[37][38]
Alternatives and Related Tools
In Unix-like systems, theid command serves as a common alternative to whoami, providing more comprehensive details about the effective user ID (UID), group ID (GID), and group memberships, whereas whoami outputs only the effective username.[2][5] For instance, invoking id -un yields the same username as whoami, but the full id output is more verbose and versatile for scenarios requiring group information, such as access control checks in scripts.[5] Another option is logname, which displays the original login name rather than the effective user, making it useful for tracking the initiating user in elevated sessions like those under sudo, unlike whoami which reflects the current effective identity.[2]
Environment variable queries like echo $USER or echo $LOGNAME offer a faster, non-command alternative for retrieving the username, as they access shell variables set during login without invoking external processes.[2] However, these methods are less reliable than whoami or id -un because environment variables can be modified or unset in certain contexts, such as scripted sessions or after privilege escalations, potentially leading to inconsistent results across user sessions.[39] In practice, whoami is often preferred for its atomicity as a dedicated command that directly queries the effective UID via system calls like geteuid(), ensuring consistency without reliance on modifiable shell state, while id is frequently used alongside it in configuration files for fuller identity verification.[5] However, environment variables can be unreliable if modified, underscoring whoami's reliability via direct system queries.
On Windows systems, the native whoami command provides user identification similar to its Unix counterpart, outputting the current domain and username (e.g., DOMAIN\username), with options like /all for extended details on groups and privileges.[22] A simple batch-script alternative is echo %USERNAME%, which pulls the username from the USERNAME environment variable, offering quick access but sharing the same reliability limitations as Unix environment queries since the variable can be altered.[40] In PowerShell, equivalents include $env:USERNAME for the username alone or [System.Security.Principal.WindowsIdentity]::GetCurrent().Name for a fuller domain-qualified identity, which is particularly useful in scripting for its integration with .NET objects and avoidance of external executables.[41]
For cross-platform applications, programming languages provide programmatic alternatives that abstract user identification across operating systems. In Python, os.getlogin() retrieves the name of the user logged into the controlling terminal, similar to logname rather than whoami, for interactive sessions, while combining os.getuid() with pwd.getpwuid(os.getuid()).pw_name offers a more robust UID-to-name mapping that works reliably even in non-terminal environments. These methods prioritize system calls over environment variables for consistency, though os.getlogin() may fail in detached processes. In Java, System.getProperty("user.name") returns the username under which the JVM is running, functioning as a direct equivalent to whoami by accessing the underlying OS user context without additional dependencies.[42] Such tools are favored in software development for their portability and integration into larger applications, contrasting whoami's shell-specific simplicity.