Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

up vote 7 down vote accepted

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.

share|improve this answer
    
+1 for Nice Answer. –  Md. Mahbubur R. Aaman Nov 16 '12 at 10:23
add comment

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/ .*//'
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.