0

Symptom:

user1@ubuntu:~$ printenv PATH
/system/company/root/bin:/usr/lib/icecc/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/system/company/scripts:/opt/puppetlabs/bin
user1@ubuntu:~$ sudo su - user2
[sudo] password for user1: 
user2@ubuntu:~$ printenv PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/opt/puppetlabs/bin

file /etc/environment:

PATH="/system/company/root/bin:/usr/lib/icecc/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/system/company/scripts"
COMPANY_ROOT="/system/company/root"
COMPANY_SCRIPTS_ROOT="/system/company/scripts"

visudo:

Defaults        env_reset
Defaults        env_keep += "PATH"
Defaults        env_keep += "COMPANY_ROOT"
Defaults        env_keep += "COMPANY_SCRIPTS_ROOT"
Defaults        mail_badpass
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

~/.profile:

if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

Additional check:

user1@ubuntu:~$ echo $COMPANY_ROOT
/system/COMPANY/root
user1@ubuntu:~$ sudo bash -c 'echo $COMPANY_ROOT'
[sudo] password for user1: 
/system/COMPANY/root

Why environment variables disappear right after sudo? I already set env_keep on visudo and /etc/environment file still not go.

5
  • 1
    Is COMPANY_ROOT in the environment already? Whats the output of echo $COMPANY_ROOT; sudo bash -c 'echo $COMPANY_ROOT'?
    – heemayl
    Apr 20 '17 at 3:29
  • Running on what user? User1 or user2? Apr 20 '17 at 3:37
  • Do you have anything set in ~/.profile? In Debian, I have also had global startup scripts overwrite paths. Apr 20 '17 at 3:45
  • Also, are either of those paths soft linked? Apr 20 '17 at 3:51
  • added ~/.profile content; What's soft link mean? it's variable. Apr 20 '17 at 4:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.