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.

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

2 Answers 2

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
gilles
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 "who am i" 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

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.