Once sudo su'd to root, is there a command to see my username?
Solution 1
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
Solution 2
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.
Solution 3
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
Related videos on Youtube
![Michael Campbell](https://i.stack.imgur.com/heiSq.png?s=256&g=1)
Michael Campbell
Updated on September 17, 2022Comments
-
Michael Campbell almost 2 years
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? -
bahamat over 13 yearsAnd
sudo -si
gives you a shell withSUDO_USER
set. -
Gilles 'SO- stop being evil' over 13 years@bahamat: Just
sudo -i
gives you a shell. In fact:sudo: you may not specify both the `-i' and `-s' options
-
phunehehe over 13 years
who am i
is not a command on my system, andwhoami
returns "root" -
rubik over 13 yearsOn my system it works:
whoami
returns 'root',who am i
returns my username -
bahamat over 13 yearsYes, you're right. That wasn't in the man page, but when executed that's the message that comes out.
-
clerksx over 12 years@rubik That's just because it's the
who
command. -
William almost 10 yearsI tried to edit this question to say
whoami
instead ofwho 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'.