who am i and whoami

who am i and whoami

Post by Kurt Part » Sat, 07 Sep 1991 06:11:05

Under SunOS 4.0.3, I executed the following from the csh.
With kparten being me, and other being some other account I have.

% sh
$ login other
$ other commands
$ exit
% who am i
% whoami

Kurt Parten


who am i and whoami

Post by Guy Harr » Sun, 08 Sep 1991 03:43:04

Quote:>% sh
>$ login other
>$ other commands
>$ exit
>% who am i

"who am i" checks to see what tty it's being run from (by checking if
its standard input is a tty and, if so, what tty it is), and looks for
the entry in "/etc/utmp" for that tty; if it finds it, it prints the
login name it finds there.  If its standard input isn't a tty, or it
can't find the name of that tty, it gets the current real user ID and
prints the name that corresponds to that.  To quote SunOS 4.0.3's

     Used without arguments, who lists the login  name,  terminal
     name,  and  login time for each current user.  who gets this
     information from the /etc/utmp file.


     With two arguments, as in `who  am  i'  (and  also  `who  is
     who'), who tells who you are logged in as:  it displays your
     hostname, login name, terminal name, and login time.

Quote:>% whoami

"whoami" doesn't look at "/etc/utmp" at all, the fact that the SunOS
4.0.3 manual page for it lists "/etc/utmp" in its FILES section
nonwithstanding.  Instead, to quote the manual page:

     whoami displays the login name corresponding to the  current
     effective  uer  ID.  If  you  have used su(1) to temporarily
     adopt another user, whoami will report the login name  asso-
     ciated  with  that user ID. whoami gets its information from
     the geteuid() and getpwuid() library routines (see getuid(2)
     and getpwent(3), respectively).

So, in the case you gave, "who am i" reports "other", because when you
did "login other", it overwrote the "/etc/utmp" entry for your tty,
putting in "other" as the login name.  "whoami" reports "kparten",
because the shell it was run from was started *before* you did the
"login other", and therefore is running with an effective user ID of
"kparten"; thus, "whoami" is run with the same effective user ID, and
reports the name corresponding to it.