sudo 环境变量消失了

sudo 环境变量消失了

症状:

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

文件/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"

视觉:

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

附加检查:

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

为什么环境变量在 sudo 之后立即消失?我已经在 visudo 上设置了 env_keep,但 /etc/environment 文件仍然无法运行。

相关内容