It's login.
The Linux login(1) man page says:
The value for $HOME, $USER, $SHELL, $PATH, $LOGNAME, and
$MAIL are set according to the appropriate fields in the password entry.
The FreeBSD login(1) man page says:
The login utility enters information into the environment (see
environ(7)) specifying the user's home directory (HOME), command
interpreter (SHELL), search path (PATH), terminal type (TERM) and user name
(both LOGNAME and USER).
The NetBSD, OpenBSD and OS X man pages say the same thing.
Here's the source code from the util-linux login:
setenv("HOME", pwd->pw_dir, 0); /* legal to override */
setenv("USER", pwd->pw_name, 1);
setenv("SHELL", pwd->pw_shell, 1);
/* ... */
setenv("LOGNAME", pwd->pw_name, 1);
Here's the source code from the FreeBSD login:
(void)setenv("LOGNAME", username, 1);
(void)setenv("USER", username, 1);
(void)setenv("PATH", rootlogin ? _PATH_STDPATH : _PATH_DEFPATH, 0);
manpage,whoamireports the name associated with your effective user ID. Which means it will return something different if you're usingsudoor running a setuid executable. If you havesudoset up, trysudo whoamifor example. – Joseph R. May 19 '13 at 11:44USERandUSERNAMEare ordinary environment variables, which means that, if you want, you can set them to arbitrary values. Just typeUSER=xyz. In other words, even if those variables exist, there is no guarantee that their values match the currently logged-in username. – Uwe May 19 '13 at 12:03guarantee, I meant by default (i.e. assuming user did not change them). – tshepang May 19 '13 at 12:22sudo whoamiandsudo echo $USER– Joseph R. May 19 '13 at 16:58sudo echo $USER, the shell expands$USER, then callssudo. So of course it doesn't produce the same output aswhoami. Likesudo whoami,sudo sh -c 'echo $USER'does (typically) outputroot. Regarding your comment aboutwhoamiusing the EUID, note thatsudo whoamiwould outputrooteven ifwhoamiused the UID.sudosets both EUID and UID for the command it runs (except in the very unusual situation that you explicitly configure it to behave otherwise). Comparesudo id -utosudo id -ru. – Eliah Kagan Oct 07 '14 at 16:55