Server Fault is a question and answer site for system and network administrators. 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 a script that I call with sudo -- sudo script, and I want to be able to see the username of the user running the script (not root).

Sticking whoami or id -un inside the script just gives me root.

share|improve this question
up vote 10 down vote accepted

sudo creates an environment variable called SUDO_USER. You can use it in your script like so:


share|improve this answer

You can look at the SUDO_USER, SUDO_UID or SUDO_GID environmental variables to find this information.

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.