Looking at the man page, whoami reports the name associated with your effective user ID. Which means it will return something different if you're using sudo or running a setuid executable. If you have sudo set up, try sudo whoami for example.
– Joseph R.May 19 '13 at 11:44

4

USER and USERNAME are ordinary environment variables, which means that, if you want, you can set them to arbitrary values. Just type USER=xyz. In other words, even if those variables exist, there is no guarantee that their values match the currently logged-in username.
– UweMay 19 '13 at 12:03

@Uwe By guarantee, I meant by default (i.e. assuming user did not change them).
– tshepangMay 19 '13 at 12:22

2

@Tshepang As a follow up to my first comment: compare the results of sudo whoami and sudo echo $USER
– Joseph R.May 19 '13 at 16:58

2

@JosephR. For sudo echo $USER, the shell expands $USER, then calls sudo. So of course it doesn't produce the same output as whoami. Like sudo whoami, sudo sh -c 'echo $USER' does (typically) output root. Regarding your comment about whoami using the EUID, note that sudo whoami would output root even if whoami used the UID. sudo sets both EUID and UID for the command it runs (except in the very unusual situation that you explicitly configure it to behave otherwise). Compare sudo id -u to sudo id -ru.
– Eliah KaganOct 7 '14 at 16:55

On my Fedora 16 box, I have both USER and USERNAME set and your command only returns LOGNAME.
– Joseph R.May 19 '13 at 11:50

1

@JosephR., unfortunately I don't have Fedora on hands but I've looked into FreeBSD's sources as well, see UPD..
– poigeMay 19 '13 at 12:43

But this is obviously not the case on Fedora. All I'm saying is, login doesn't seem to be the only thing setting these variables.
– Joseph R.May 19 '13 at 14:10

1

Note that Linux is just a kernel, it doesn't have a login command. OSes that use Linux as their kernel are free to use any implementation they like. For instance, Debian based systems tend to use the one from shadow-utils, not util-linux.
– Stéphane ChazelasJan 7 '17 at 9:34

1

Note that login is often not invoked when logging in over ssh or by most graphical login managers.
– Stéphane ChazelasJan 7 '17 at 9:35

There's no rule. Some shells like tcsh or zsh set $LOGNAME. zsh sets $USER.

It may be set by some things that log you in like login (as invoked by getty when login on a terminal and sometimes by other things like in.rlogind), cron, su, sudo, sshd, rshd, graphical login managers or may not.

If there's been a login though, in my experience, $USER is generally set (but it may not be updated after a change of user id (via setuid commands) within that login session. POSIX requires that $LOGNAME be set upon login (and cron).

To get the login name portably, best is to use the logname command (if there's not been any login, it may return nothing). To get the user id, use id -u. To get one username corresponding to the current effective user id: id -un. To get all of them (most of the time, there's only one user name per user id, but that's not guaranteed):

perl -le 'while ($n = getpwent()) {print $n if getpwnam($n) == $>}'

Though that may not work on systems where the user database cannot be enumerated (as happens sometimes with networked user databases for instance).

You probably want to rely on the POSIX standard here, since at some point in time you will probably care about not just user login (managed by the login program) but also cron jobs and the like.

Therefore, you should know that POSIX requires $LOGNAME but not $USER. E.g. $USER may not be set by cron, as pointed out in an answer by Keith Thompson, which also references some history on how this relates to the history of System-V vs BSD:

... at least on my system (Ubuntu 14.04) the $USER environment variable is not set for cron jobs. Instead, you can use $LOGNAME, which is part of the environment for cron jobs.

According to the environ(7) man page (type man environ to read it), $USER is used by BSD-derived programs and $LOGNAME is used by System-V-derived programs.