Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have sudo rights on a redhat box; once I've sudo su - to become root in a shell, are there any commands I can run to see what username I su'd FROM?

share|improve this question
up vote 8 down vote accepted

The shell's parent process is su, so you need to find out the user running su's parent process:

ps -o user= $(ps -o ppid= $PPID)

But you shouldn't be doing sudo su - if your version of sudo is not too old to have sudo -i. Sudo sets the environment variable SUDO_USER to the name of the user who ran sudo. You won't see it with sudo su - because su - scrubs the environment.

$ sudo -i
# echo $SUDO_USER
share|improve this answer
And sudo -si gives you a shell with SUDO_USER set. – bahamat Jan 27 '11 at 23:11
@bahamat: Just sudo -i gives you a shell. In fact: sudo: you may not specify both the `-i' and `-s' options – Gilles Jan 27 '11 at 23:13
Yes, you're right. That wasn't in the man page, but when executed that's the message that comes out. – bahamat Jan 29 '11 at 17:03

Run command whoami it will return you something like that:

gladimdim tty2        2011-01-27 23:54 (:0)

In bold "gladimdim" is the user which was initially logged to system.

share|improve this answer
who am i is not a command on my system, and whoami returns "root" – phunehehe Jan 27 '11 at 23:20
On my system it works: whoami returns 'root', who am i returns my username – rubik Jan 28 '11 at 16:57
@rubik That's just because it's the who command. – Chris Down Nov 7 '11 at 14:00
I tried to edit this question to say whoami instead of who am i. who am i is not a reliable solution because it's not installed on many systems by default, so shell scripts may fail. I don't mind if you don't approve the edit though. It's a matter of 'personal preference'. – William D. Edwards Sep 20 '14 at 6:25

I think he meant:

$ who -am i

which could (depending) be parsed the same as:

$ who -am

The "-a" lists all users currently logged in and "-m" filters that down to only those users (should really be one) who are associated with who's STDIN, which is to say it'll tell you who is logged in at your terminal. That's what you're looking for.

$ who -am
share|improve this answer

Your Answer


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.