In shared environment where multiple users have sudo account, I want to find out underlying user id (not a sudo account) details who has invoked particular script. Thanks.

I tried below but it does not fulfill my requirement : echo $USERNAME gives me sudo account details but I am looking for login id

  • ps -p $(ps -C script.sh -o ppid=) -o ruser=
    – nezabudka
    Commented Sep 29, 2023 at 4:22

2 Answers 2


sudo sets the SUDO_USER environment variable to a username corresponding to the real user id of the process that executed it, so that will typically be the username of the user who invoked sudo.

It also sets the USER environment variable to the target user:

$ id -un
$ sudo printenv SUDO_USER USER

So within a script invoked as sudo the-script, you can find the original user name in $SUDO_USER.

And on Linux, you can find that information for a given process by looking into /proc/<pid>/environ:

$ sudo sleep 3m &
[1] 97380
$ sudo grep -z '^SUDO_USER=' /proc/[1-9]*/environ | tr '\0' '\n'
$ ps -fp 97382
UID          PID    PPID  C STIME TTY          TIME CMD
root       97382   97381  0 12:40 pts/8    00:00:00 sleep 3m

In some configurations including mine above, sudo will run the target command in a child process and hang around waiting for its termination instead of running it in the same process. You can determine the user who invoked that sudo command from the real user id of the process:

$ ps -o ruser= -p 97380

View the entire chain of possible user changes:

pstree -sul $(ps -C script.sh -o pid=)

-s show parents.
-u uid changes.

Not the answer you're looking for? Browse other questions tagged .