Once sudo su'd to root, is there a command to see my username?

5,777

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
Share:
5,777

Related videos on Youtube

Michael Campbell
Author by

Michael Campbell

Updated on September 17, 2022

Comments

  • Michael Campbell
    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
    bahamat over 13 years
    And sudo -si gives you a shell with SUDO_USER set.
  • Gilles 'SO- stop being evil'
    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
    phunehehe over 13 years
    who am i is not a command on my system, and whoami returns "root"
  • rubik
    rubik over 13 years
    On my system it works: whoami returns 'root', who am i returns my username
  • bahamat
    bahamat over 13 years
    Yes, you're right. That wasn't in the man page, but when executed that's the message that comes out.
  • clerksx
    clerksx over 12 years
    @rubik That's just because it's the who command.
  • William
    William almost 10 years
    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'.