以 root 身份运行 python 脚本 [英] Running python script as root

查看:124
本文介绍了以 root 身份运行 python 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下脚本:

#!/usr/bin/env python                                                           

import sys                                                                      
import pyttsx                                                                   

def main():                                                                     
        print 'running speech-text.py...'                                       
        engine = pyttsx.init()                                                  
        str = "Hi..."                                    
        if len(sys.argv) > 1:                                                   
                str = sys.argv[1]                                               
        engine.say(str)                                                         
        engine.runAndWait()                                                     

if __name__ == '__main__':                                                      
        main() 

我把它放在/usr/bin/speech-test.py

我还给了它可执行权限和所有权给 root:

I have also given it executable permissions and ownership to root:

sudo chown root:root /usr/bin/speech-test.py
sudo chmod 4755 /usr/bin/speech-test.py

但是,只有当我以 sudo Speec-test.py 运行时,此脚本才能正确运行.如果我尝试将它作为 speech-test.py 运行,它会抱怨找不到一堆 ALSA lib 文件.

However, this script will only run correctly if I run as sudo speec-test.py. If I try to run it as just speech-test.py it complains about not finding a bunch of ALSA lib files.

我是否遗漏了什么让我的脚本以 root 权限运行?

Am I missing something to have my script run with root privileges?

推荐答案

所以您希望脚本以 root 身份运行,即使没有 sudo?为此,您需要使用 sudo chmod u+s program 在脚本上设置 setuid 位.但是,出于安全原因,大多数 Unix 发行版只允许对二进制文件执行此操作,而不允许对脚本执行此操作.一般来说,这样做真的不是一个好主意.

So you want the script to run as root, even without sudo? For that you would need to set the setuid bit on the script with sudo chmod u+s program. However, most Unix distributions allow this only for binaries, and not for scripts, for security reasons. In general it's really not a good idea to do that.

如果您想以 root 身份运行此脚本,则必须以 sudo 身份运行.或者,您必须创建一个运行脚本的二进制文件,以便您可以在此二进制包装器上设置 setuid 位.这个相关问题解释了更多.

If you want to run this script as root, you will have to run as sudo. Or, you have to create a binary that runs your script, so that you can set the setuid bit on this binary wrapper. This related question explains more.

检查有效的 uid 也是一个好主意,如果不是 root 则停止运行.为此,请将其添加到顶部附近(感谢 @efirvida 的提示!)

It's also a good idea to check the effective uid, and if it's not root then stop running. For that, add this near the top (thanks @efirvida for the tip!)

if not os.geteuid() == 0:
    sys.exit("
Only root can run this script
")

原答案

也许你的用户和 root 使用不同版本的 python,不同的 python 路径和不同的库集.

Maybe your user and root use a different version of python, with different python path, and different set of libraries.

试试这个:

command -v python
sudo command -v python

如果这两个命令没有给出相同的结果,那么您要么需要更改用户的设置以使用相同版本的 python(具有 ALSA 库的版本),或者在脚本的第一行硬编码 python 版本.

If the two commands don't give the same result then you either need to change the setup of the users to use the same version of python (the one that has the ALSA libs), or hardcode the python version the first line of the script.

还尝试在脚本中添加 print sys.path 行,并与您的用户和 sudo 一起运行并进行比较.可能你会得到不同的结果.您可能需要调整用户的 PYTHONPATH 变量.

Also try adding a print sys.path line in the script, and run with your user and with sudo and compare. Probably you'll get different results. You may need to tweak the PYTHONPATH variable of your user.

不需要让脚本的所有者成为 root,并使用 sudo 运行它.你只需要正确配置pythonPYTHONPATH即可.

It shouldn't be necessary to make the owner of the script root, and to run it with sudo. You just need to configure python and PYTHONPATH correctly.

这篇关于以 root 身份运行 python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆