在Raspberry Pi上,我希望来宾用户能够运行一个需要sudo权限的python脚本,而不需要使用密码就不需要sudo权限。
我使用viuser编辑了/etc/sudoer文件,但未能获得所需的结果。当我以“guest”身份登录并尝试运行脚本时,计算机会要求超级用户密码。
编辑后的/etc/sudoer文件如下:

#
# 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:$

# Host alias specification

# User alias specification
User_Alias      GROUPONE = guest

# Cmnd alias specification
Cmnd_Alias      SCRIPT = /home/guest/test.py
GROUPONE        ALL = SCRIPT

# User privilege specification
root    ALL=(ALL:ALL) ALL
GROUPONE    ALL = NOPASSWD: /home/guest/test.py

# 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

python脚本是:
import subprocess
def Go():
    subprocess.call(['sudo','echo', 'Test string'])
if __name == '__main__':
    Go()

“python test.py”的预期结果是控制台上的“test string”。
相反,计算机请求来宾sudo密码。

最佳答案

请试试看

modify user <GUEST> to add additional group <sudo>, I think <usermod --group GRP1...>

change group of the script to <sudo> and make group executable <chmod g+x SCRIPT> & < chgrp sudo SCRIPT>

我还没有测试过,但这应该行得通。

关于linux - 如何为唯一的python脚本创建 guest sudo特权?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56500778/

10-12 01:29
查看更多