使用 NOPASSWD 时 sudo 仍然要求输入密码

Mar*_*náš 2 sudo

我想运行这个脚本(它是 git 存储库的 post-receive 钩子):

#!/bin/sh
echo "Executing post-receive hook as user `id` using `which sudo`"
sudo -l
sudo -n bash /var/project/autotest/autotest.sh
Run Code Online (Sandbox Code Playgroud)

它应该在不要求密码的情况下运行,但我得到了以下输出:

Executing post-receive hook as user uid=1005(martinjonas) gid=1001(martinjonas)
groups=1001(martinjonas),1009(lifeweb),1017(project) using /usr/bin/sudo
Matching Defaults entries for martinjonas on this host:
    env_reset

User martinjonas may run the following commands on this host:
    (root) NOPASSWD: /var/project/autotest/autotest.sh
sudo: sorry, a password is required to run sudo
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,根据 sudo -l 的输出,我应该可以在没有密码的情况下运行 /var/project/autotest/autotest.sh,但是 sudo 仍然要求输入密码。

这是我的 sudoers 文件(它是整个文件,没有其他条目):

# /etc/sudoers
#
# This file MUST be edited with the 'visudo' command as root.
#
# See the man page for details on how to write a sudoers file.

Defaults        env_reset

# Host alias specification
# User alias specification
# Cmnd alias specification

# User privilege specification
root    ALL=(ALL) ALL

# Allow members of group sudo to execute any command
# (Note that later entries override this, so you might need to move
# it further down)
%sudo ALL=(ALL) ALL
#
#includedir /etc/sudoers.d

www-data        ALL=(root) NOPASSWD: /usr/bin/svn up
%project       ALL=(root) NOPASSWD: /var/project/autotest/autotest.sh
Run Code Online (Sandbox Code Playgroud)

我试图寻找答案,但我发现的唯一可能的问题是错误的 sudo 使用或错误的 sudoers 中的整体顺序,我都反对。

我们在这台服务器上使用 Debian 6.0 2.。

小智 8

您有 的NOPASSWD条目/var/project/autotest/autotest.sh,但您的命令正在运行bash /var/project/autotest/autotest.sh- 这是不同的命令,这就是它被禁止的原因

删除bash,一切都应该正常工作。