我修改了 /etc/sudoers(使用 visudo)以保留环境变量:
Defaults !env_reset
# Host alias specification
# User alias specification
# Cmnd alias specification
# User privilege specification
root ALL=(ALL:ALL) ALL
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL
# Allow members of group sudo to execute any command
%sudo ALL=(ALL:ALL) ALL
#includedir /etc/sudoers.d
www ALL = NOPASSWD: /usr/bin/env
Run Code Online (Sandbox Code Playgroud)
这几乎工作得很好:保留所有环境变量,除了 PYTHONPATH !
www@myhost:~$ env | grep PYTHON
PYTHONPATH=/home/www/python_commons:/home/www/python_commons/django_commons/apps:/home/www
www@myhost:~$ sudo env | grep PYTHON
www@myhost:~$
Run Code Online (Sandbox Code Playgroud)
我试图在 sudoers 中添加:
Defaults env_keep += "PYTHONPATH" …Run Code Online (Sandbox Code Playgroud)