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

查看:707
本文介绍了以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程序在脚本上设置 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("\nOnly root can run this script\n")

原始答案

也许您的用户和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。脚本中的路径行,然后与您的用户一起运行,并与 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.

不必成为所有者脚本根目录,并使用 sudo 运行它。您只需要正确配置 python PYTHONPATH

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天全站免登陆