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

Why is it that when I switch user su <username> and then execute who am i is outputs the previous user which I was logged as and not the one I’ve switched to?

Asked By: user1091856

||

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.

Answered By: ckhan

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/ .*//'
Answered By: Marnix A. van Ammers

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…

Answered By: Thomas Baeckeroot

In my case, the user shell was /bin/false in /etc/passwd which is why su didn’t return an error. In other words, su somebody failed with no error and I was still root, not somebody.

The solution was su somebody -s /bin/bash or whatever shell you want. And then obviously whoami works as expected.

Answered By: PJ Brunet
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.