Linux – Once sudo su’d to root, is there a command to see the username

linuxrhelsudousers

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?

Best Answer

The shell's parent process is su -, and the parent of that is the sudo su -. So you need to find out the user running sudo su -'s parent process by searching back in two steps with ps:

ps -o user= $(ps -o ppid= $(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
Related Question