【问题标题】:www-data python run sudo command as another user error password requiredwww-data python run sudo 命令作为另一个用户错误密码需要
【发布时间】:2021-05-02 21:17:10
【问题描述】:

您好,我有一个托管在 Apache Ubuntu Google VM 上的 Django 项目。我使用 git 来更新服务器代码和备份数据库文件。为了避免不得不 ssh 并一遍又一遍地执行重复的 git 任务,我试图在管理页面上编写一些将运行 git 脚本的按钮。设置 git 的方式我需要以特定用户身份运行 git 以使用正确的 ssh 密钥。我的想法是允许 www-data 在非常有限的一组命令上以 tris(git 用户)的身份进行 sudo。我试图通过修改如下所示的 sudoers 文件来做到这一点。

# 
# This file MUST be edited with the 'visudo' command as root.
#
# Please consider adding local content in /etc/sudoers.d/ instead of
# directly modifying this file.
#
# See the man page for details on how to write a sudoers file.
#
Defaults        env_reset
Defaults        mail_badpass
Defaults        secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"

# 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

# See sudoers(5) for more information on "@include" directives:

@includedir /etc/sudoers.d

我按照要求做了,并在 /etc/sudeors.d/ 中创建了一个名为 git 的文件,其中包含以下内容并重新启动了虚拟机:

www-data ALL=(tris) NOPASSWD: /usr/bin/git pull
www-data ALL=(tris) NOPASSWD: /usr/bin/git add db.sqlite3
www-data ALL=(tris) NOPASSWD: /usr/bin/git commit -m "server sync"
www-data ALL=(tris) NOPASSWD: /usr/bin/git push

尝试运行这些命令的测试 python 脚本如下所示:

    commands = [
        'cd /home/tris/DjangoSite/;',
        '''sudo -i -u tris bash -c '/usr/bin/git add db.sqlite3; /usr/bin/git commit -m "server sync"; /usr/bin/git push;' '''
    ]
    command = ' '.join(commands)
    p = run(command,stdout=PIPE,stderr=PIPE,shell=True)
    results = f"Push\nargs:\n{p.args}\nstdout:{p.stdout.decode('utf-8')}\nstderr:\n{p.stderr.decode('utf-8')}"
    print(results)

最后这是它产生的错误:

[Sun May 02 21:11:48.190725 2021] [wsgi:error] [pid 1412:tid 140452225025600] [remote <IP>:62498] Push
[Sun May 02 21:11:48.190771 2021] [wsgi:error] [pid 1412:tid 140452225025600] [remote <IP>:62498] args:
[Sun May 02 21:11:48.190778 2021] [wsgi:error] [pid 1412:tid 140452225025600] [remote <IP>:62498] cd /home/tris/DjangoSite/; sudo -i -u tris bash -c '/usr/bin/git add db.sqlite3; /usr/bin/git commit -m "server sync"; /usr/bin/git push;'
[Sun May 02 21:11:48.190783 2021] [wsgi:error] [pid 1412:tid 140452225025600] [remote <IP>:62498] stdout:
[Sun May 02 21:11:48.190787 2021] [wsgi:error] [pid 1412:tid 140452225025600] [remote <IP>:62498] stderr:
[Sun May 02 21:11:48.190792 2021] [wsgi:error] [pid 1412:tid 140452225025600] [remote <IP>:62498] sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper
[Sun May 02 21:11:48.190796 2021] [wsgi:error] [pid 1412:tid 140452225025600] [remote <IP>:62498] sudo: a password is required

谁能帮助确定我做错或错过了哪一步?我一直在努力解决这个问题,如果有任何帮助,我将不胜感激,谢谢!

相关组成员身份:

tris@website:~/$ groups www-data
www-data : www-data
tris@website:~/$ groups tris
tris : tris adm dialout cdrom floppy audio dip video plugdev netdev lxd ubuntu google-sudoers

请求 ls -l /etc/sudoers.d

sudo ls -l /etc/sudoers.d
total 20
-r--r----- 1 root root 144 Apr 27 04:04 90-cloud-init-users
-r--r----- 1 root root 958 Feb 18 00:03 README
-rw-r--r-- 1 root root 295 Apr 30 06:33 git
-r--r----- 1 root root  34 Apr 27 06:42 google-oslogin
-r--r----- 1 root root  43 Apr 27 04:05 google_sudoers

【问题讨论】:

  • 我添加了 www-data 所在的组
  • 已添加,现在检查 www-data 是否出现在其中任何一个中
  • 它们都没有出现
  • chmod 已实现,同样的错误
  • 如果您手动尝试(而不是从 python 脚本),它会提示您输入密码吗?

标签: linux apache subprocess sudoers


【解决方案1】:

将所有命令合并到一个脚本中,然后将脚本添加到 sudoers 文件中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多