如何允许用户使用sudo保存环境?


17

我遇到以下错误,试图在运行时允许某些环境变量传递给新环境sudo

sudo: sorry, you are not allowed to preserve the environment

一些可能有助于调试的信息:

[deploy@worker1 ~]$ sudo -l
    Matching Defaults entries for deploy on this host:
    requiretty, !visiblepw, always_set_home, env_reset, env_keep="COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR LS_COLORS", env_keep+="MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS LC_CTYPE",
    env_keep+="LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES", env_keep+="LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE", env_keep+="LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET
    XAUTHORITY", secure_path=/sbin\:/bin\:/usr/sbin\:/usr/bin, env_keep+="GIT_WORK_TREE GIT_DIR", !requiretty

User deploy may run the following commands on this host:
    (ALL) NOPASSWD: /usr/bin/git, (ALL) /etc/init.d/httpd*, (ALL) /sbin/service, (ALL) /usr/bin/make, (ALL) /bin/echo

我正在运行的示例:

[deploy@worker1 ~]$ export GIT_DIR="/home/ashinn/testing"
[deploy@worker1 ~]$ sudo -E sh -c 'echo "$GIT_DIR"'
sudo: sorry, you are not allowed to preserve the environment

我的sudoers.d文件用于此特定配置:

Defaults:deploy         env_keep += "GIT_WORK_TREE GIT_DIR", !requiretty
deploy  ALL=(ALL)       NOPASSWD: /usr/bin/git, /etc/init.d/httpd*, /sbin/service, /usr/bin/make, /bin/echo

我也尝试添加!env_reset到默认值,但仍然失败,并出现相同的错误。我觉得我可能缺少一些明显的东西,需要重新审视。我在这里想念什么?


1
我相信大约有93%的人理解您的问题的重点,但是您的示例命令不是很好。  ,如果没有完全失败,将显示的pre- value ,因为shell 甚至在调用之前就在命令行中扩展了变量。更好的测试是or 或,我们实际上是在查看特权运行的进程的环境。sudo (option(s)) echo $GIT_DIRsudo$GIT_DIRsudosudo printenv GIT_DIRsudo env | grep GIT_DIRsudo sh -c 'echo "$GIT_DIR"'
G-Man

Answers:


25

您可以SETENVsudoers文件中使用“标签” ,如:

deploy  ALL=(ALL)       SETENV: /usr/bin/git, /etc/init.d/httpd*, /sbin/service, /usr/bin/make, /bin/echo

或者,将其与NOPASSWD结合使用:

deploy  ALL=(ALL)       NOPASSWD:SETENV: /usr/bin/git, /etc/init.d/httpd*, /sbin/service, /usr/bin/make, /bin/echo

sudoers男子的相关摘录:

SETENV和NOSETENV

这些标记会根据每个命令覆盖setenv选项的值。请注意,如果已经为命令设置了SETENV,则用户可以通过-E选项从命令行禁用env_reset选项。此外,在命令行设置环境变量都没有受到规定的限制env_checkenv_deleteenv_keep。这样,仅允许受信任的用户以这种方式设置变量。如果匹配的命令是ALL,则该命令隐含SETENV标签;可以使用NOSETENV标签覆盖此默认设置。


1

不指定-E选项。用-E你的话说,deploy应该保留用户的所有环境变量,而不仅仅是GIT_DIR

sudo echo $GIT_DIR由于您已添加GIT_DIRenv_keep列表中,因此运行应该可以

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.