First, thank you for making this suggestion. However I do not believe this to be a deficiency of the whoami command. This is expected behavior in the GNU and Unix systems.
flaviano petrocchi wrote: > I use an alternate root user with login capabilities, created with > "useradd -u 0 -o -g 0 -G 0 -m -s /bin/bash superuser". Note that you are simply creating a new way to log into the account. You are not creating a new account. You are simply enabling a new way to log into the existing root superuser account. I think that is a key point. It isn't a separate account. This isn't even a documented behavior as far as I know. It is simply the way the implementation has always worked. > If I login with superuser, whoami returns root or superuser > depending on which user comes before in the /etc/passwd file. Yes. That is because the password file defines the mapping from uid to name. The first one in the file defines the name. This is the way it has always worked. For decades. > echo $USER instead always returns the correct user name. Caution here. The USER variable is sometimes overridden by the operating system. In particular Red Hat overrides USER and LOGNAME from their original settings in /etc/profile. Traditionally LOGNAME referred to the name the user used to log into the system. (This was also available with 'who am i' too.) RH breaks this by setting both LOGNAME and USER to the output of 'id -un'. Other systems that I am familiar with (Debian, Ubuntu, SuSE, HP-UX, Solaris) do not override the value. > I think whoami should always report the correct name maybe The disagreement here is on the "correct" name. The correct name is the named defined for the current effective user id from the password database (not necessarily the /etc/passwd file for example if NIS/YP or LDAP is in use). The correct name is not the $USER nor $LOGNAME since those might be changed and invalid in the environment. > retrieving the USER environment variable instead of parsing the > passwd file. Those who use whoami to check if a user is root should > instead use id -u and check if the uid is 0. The 'whoami' command must map the current user id to the associated name. Otherwise too much system breakage would occur. For example contemplate the following: $ USER=foo whoami foo That wouldn't be good. If a script desires to use $USER then the script should simply use $USER instead of calling 'whoami'. Also although I admit that while I usually use 'whoami' in my scripts the standard POSIX standard method would be to use 'id -un'. Note that 'whoami' is a BSD derived utility and isn't standardized by POSIX. BSD therefore is the reference for the behavior of this command. This has decades of legacy behavior and therefore it really isn't practical to try to change the behavior now. Instead new behavior should be part of a new command. But in this case what you wish is easily available simply by using the $USER value directly so there really isn't a need to put this in a command at all. See also the 'logname' and 'who am i' commands. Bob _______________________________________________ Bug-coreutils mailing list [email protected] http://lists.gnu.org/mailman/listinfo/bug-coreutils
