Skip to content

Instantly share code, notes, and snippets.

@ThinGuy
Last active October 2, 2022 18:42
Show Gist options
  • Save ThinGuy/be9ab38a60e9e099b72c79f4e567d163 to your computer and use it in GitHub Desktop.
Save ThinGuy/be9ab38a60e9e099b72c79f4e567d163 to your computer and use it in GitHub Desktop.
Illustrate sudo preserving the users's environment ($HOME is this case)
# Show value of $HOME when user does not preserve the environment
sudo bash -c 'echo $HOME'
# Show the value of $HOME when -E to preserve the sudo user's environment
sudo -E bash -c 'echo $HOME'
# Tip: If both are the same:
#
# - You ran this command directly as root (naughty!)
#
# - sudo has HOME listed under "env_keep"
# - - Check /etc/sudoers and /etc/sudoers.d/* for sudoers files with env_keep set
# - - You may check the current value of env_keep with the following command:
# (sudo sudo -V|awk '/preserve:[\r]?$/,/^Loc/{if (/^E|^Lo/) next;gsub(/\t|\r/,"");print}'|sort -uV)
# # Note: repeated sudo command is intentional
#
#
@ThinGuy
Copy link
Author

ThinGuy commented Oct 2, 2022

$ sudo bash -c 'echo $HOME'
/root
$ sudo -E bash -c 'echo $HOME'
/home/ubuntu
$ (sudo sudo -V|awk '/preserve:[\r]?$/,/^Loc/{if (/^E|^Lo/) next;gsub(/\t|\r/,"");print}'|sort -uV)
COLORS
DISPLAY
DPKG_COLORS
HOSTNAME
KRB5CCNAME
LS_COLORS
PATH
PS1
PS2
XAUTHORITY
XAUTHORIZATION
XDG_CURRENT_DESKTOP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment