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?
- 3,988
- 7
- 38
- 62
- 661
- 2
- 8
- 16
4 Answers
You're likely running the wrong command.
whois 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)whoamiis 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 runningid -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.
-
3The two arguments `am` and `i` are not ignored, they are even specified by POSIX: "In the POSIX locale, limit the output to describing the invoking user, equivalent to the `-m` option." – xhienne Aug 20 '17 at 19:53
-
Accidentally downvoted, wouldn't let me change my vote. – PJ Brunet Nov 04 '21 at 05:47
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…
- 61
- 1
- 3
-
[Don't parse ls!](https://mywiki.wooledge.org/ParsingLs) Use `stat` instead: `stat -c '%U' "$(tty)"` You probably shouldn't parse `who am i` either, since usernames can contain spaces in some rare cases. – wjandrea May 09 '18 at 17:12
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/ .*//'
- 170
- 1
- 4
-
Note that this is Linux specific. This returns the original username in Unix / SunOS. – Underverse Nov 08 '15 at 23:16
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.
- 733
- 8
- 17