Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!.
linked to the sudo man page and updated the snippet from it
Source Link
Cristian Ciupitu

You can use the SUDO_USERSUDO_USER variable:

sudo bash -c 'echo $SUDO_USER'

sudo bash -c 'echo $SUDO_USER'

From the sudosudo man page:

if sudo is run by root andsudo utilizes the SUDO_USER following environment variable is set, sudo will use this value to determine whovariables. The security policy has control over the actual user iscontent of the command's environment. This can [...]

SUDO_UID be used by a userSet to log commands throughthe user ID of the user who invoked sudo even when a root shell.

SUDO_USER has beenSet to the login name of the user who invoked sudo.

You can use the SUDO_USER variable:

sudo bash -c 'echo $SUDO_USER'

From the sudo man page:

if sudo is run by root and the SUDO_USER environment variable is set, sudo will use this value to determine who the actual user is. This can be used by a user to log commands through sudo even when a root shell has been invoked.

You can use the SUDO_USER variable:

sudo bash -c 'echo $SUDO_USER'

From the sudo man page:

sudo utilizes the following environment variables. The security policy has control over the actual content of the command's environment. [...]

SUDO_UID Set to the user ID of the user who invoked sudo.

SUDO_USER Set to the login name of the user who invoked sudo.

Source Link
Kyle Brandt

You can use the SUDO_USER variable:

sudo bash -c 'echo $SUDO_USER'

From the sudo man page:

if sudo is run by root and the SUDO_USER environment variable is set, sudo will use this value to determine who the actual user is. This can be used by a user to log commands through sudo even when a root shell has been invoked.

lang-bash