【问题标题】:How to get root premissions for my app?如何获得我的应用程序的 root 权限?
【发布时间】:2010-07-10 01:25:13
【问题描述】:

我的应用需要做一些特权工作。我一直在寻找任何地方,但我找不到任何有用的东西。我知道我想使用 Policykit1 和 dbus,因为我发现的所有其他替代品都不再使用了。

这是我目前得到的代码:

import dbus
import os

bus = dbus.SystemBus()
proxy = bus.get_object('org.freedesktop.PolicyKit1', '/org/freedesktop/PolicyKit1/Authority')
authority = dbus.Interface(proxy, dbus_interface='org.freedesktop.PolicyKit1.Authority')

system_bus_name = bus.get_unique_name()

subject = ('system-bus-name', {'name' : system_bus_name})
action_id = 'org.freedesktop.policykit.exec'
details = {}
flags = 1            # AllowUserInteraction flag
cancellation_id = '' # No cancellation i


result = authority.CheckAuthorization(subject, action_id, details, flags, cancellation_id)

os.makedirs('/usr/local/share/somefolder')

我无法制作目录,我做错了什么?

【问题讨论】:

    标签: python linux dbus


    【解决方案1】:

    文件系统安全正在阻止您,因为您的用户没有对/usr/local/share/somefolder 的写入权限。您可以使用sudo 临时升级该目录创建的权限。但是,如果您需要以超级用户身份执行更多操作,它并不止于此。

    如果您需要写入不在用户空间中的内容,则整个程序最好以 root 身份运行(当然在 sudo 下),例如 sudo ./myscript.py

    【讨论】:

    • 重点是通过使用 dbus 和 policykit 作为获取权限的手段来避免使用“sudo ./myscript.py”之类的东西。到目前为止,这行代码:result = authority.CheckAuthorization(subject, action_id, details, flags, cancellation_id) 能够请求身份验证,但是即使我完美地输入了我的root密码,应用程序的权限也不会上升。跨度>
    • 如果你想通过调用 PolicyKit D-Bus 方法来提升权限(这是不可能的),这与在sudo 下运行没有区别,因为用户必须信任你的整个应用程序。 PolicyKit 只允许调用特权系统服务的预配置方法。
    • 您是否考虑过使用gksudo 并使用执行gksudo myscript.py 的简单bash 脚本打包您的应用程序?这肯定是一个简单的解决方法。
    猜你喜欢
    • 2011-08-21
    • 1970-01-01
    • 2015-12-17
    • 2013-05-29
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 2014-12-08
    相关资源
    最近更新 更多