`who am i` after `su` not showing new user id?

You're likely running the wrong command.

  • who is meant to show who is logged in, i.e. which user owns the terminal. It returns a line like this:

    ckhan    pts/1        2012-11-05 03:06 (c-21-13-25-10.ddw.ca.isp.net)
    
  • whoami is mean to show you what the effective user id is of the person running it. It returns just a single name, like this (and is equivalent to running id -un):

    ckhan
    

I think you may have literally typed who am i at the terminal, which ran who with two ignored arguments (am, i).

See man who and man whoami for more details.


Per my Ubuntu 12.04.2 man page for the 'who' command, 'who am i' (or who with any two arguments) is the same as 'who -m' and should give you the hostname and user associated with STDIN. However I get no output with 'who am i'. Either the man page is wrong or the command has a bug. Regardless, as previously answered by ckhan, the 'whoami' command will give you the username for your effective user ID. At least in Ubuntu 12.04.2, none of id, 'who am i', or 'whoami' will give you just the username of the person logged in on the current terminal. As a workaround you could get that with:

who | sed 's/ .*//'

Workaround:

ls -l `tty` | awk '{print $3}'

that works as replacement from who am i | awk '{print $1}' . Explanation below:

On many systems "who am i" is equivalent to "who -m". The problem here is that with some terminals, "who -m" returns nothing!

Example #1 run from a xfce4-terminal

Pegasus ~ # whoami
root
Pegasus ~ # who am i
thomas   pts/1        2017-08-19 11:15 (:0.0)
Pegasus ~ # who -m
thomas   pts/1        2017-08-19 11:15 (:0.0)
Pegasus ~ # who
thomas   tty8         2017-08-19 10:18 (:0)
thomas   pts/1        2017-08-19 11:15 (:0.0)
thomas   pts/5        2017-08-19 16:16 (:0.0)
Pegasus ~ # who am i | awk '{print $1}'
thomas
Pegasus ~ # 

but Example #2 from a gnome-terminal (same computer, same commands)

Pegasus ~ # whoami
root
Pegasus ~ # who am i
Pegasus ~ # who -m
Pegasus ~ # who
thomas   tty8         2017-08-19 10:18 (:0)
thomas   pts/1        2017-08-19 11:15 (:0.0)
thomas   pts/5        2017-08-19 16:16 (:0.0)
Pegasus ~ # 

This seems to be a consequence of gnome-terminal not adding utmp entries…

Tags:

Su

Whoami