允许用户“git”通过 sudo 以“www-data”身份运行“git pull”

允许用户“git”通过 sudo 以“www-data”身份运行“git pull”

我想允许 git 以用户“www-data”的身份运行“git pull”。据我了解

    git ALL=(www-data) git pull

在 /etc/sudoers 中应该可以做到。

遗憾的是,我收到此行的语法错误,并且 visudo 语法突出显示在“www-data”中的“-”之后中断

找不到有关 /etc/sudoers 用户名中禁止使用“-”的信息。有什么建议吗?

答案1

您需要使用“git”命令的完整路径名,以下几行不会在 visudo 中产生语法错误并且可以正常工作。

git ALL = (www-data) /usr/bin/git pull

答案2

请注意,我正在使用git用户名,因此,如果你使用吉托西斯或任何其他用户名,只需填写您的用户名!

在控制台中用户执行此命令:

visudo

将打开“vi”编辑器。添加以下行:

Defaults:git    !authenticate
git ALL=(www-data) ALL

结果文件(通过调用“visudo”在“vi”编辑器中打开)应该如下所示:

# /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
Defaults:git    !authenticate

# Host alias specification

# User alias specification

# Cmnd alias specification

# User privilege specification
root    ALL=(ALL) ALL
git ALL=(www-data) 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

然后按 CTRL+O 保存文件,然后按 Enter 接受文件名(bla bla bla),然后按 CTRL+X 关闭“vi”编辑器。

瞧!现在git用户可以执行以下命令www-数据用户:

sudo -u www-data git pull origin master

相关内容