I have a use case where I want to sudo su - user1
to some user and then run script and in the script I want to fetch SUDO_USER
. However the SUDO_*
environment variables do not get set when I use sudo su - user1
instead of sudo su app
.
sudo su user1
[sudo] password for rbhanot:
$ env | grep -i sudo
SUDO_COMMAND=/bin/su user1
SUDO_GID=4361
SUDO_UID=4361
SUDO_USER=rbhanot
If I invoke su
with login shell these variables are not set
sudo su - user1
$ env|grep -i sudo
$
It works if I use a slightly different command with sudo -i
sudo -i -u user1
$ env|grep -i sudo
SUDO_COMMAND=/bin/ksh
SUDO_GID=4361
SUDO_UID=4361
SUDO_USER=rbhanot
So why does these SUDO_*
variables do not get with su - user1
specifically.
-
is to "provide an environment similar to what the user would expect had the user logged in directly" and i wouldn't expect those variables to be set on a direct login – Fox Dec 17 '20 at 05:38sudo -i
instead, or not rely on those variables being set. Or, you could change your question to not be aboutsudo
at all and ask whysu -
clears the environment. – Kusalananda Dec 17 '20 at 08:13sudo su
Considered Harmful. Readman sudo sudoers su
. – waltinator Dec 17 '20 at 20:11