简体   繁体   English

如何为我的应用程序获取root权限?

[英]How to get root premissions for my app?

My app needs to do some privileged work. 我的应用程序需要做一些特权工作。 I've been looking everywhere, but I can't find anything useful. 我一直在寻找,但我找不到任何有用的东西。 I know I want to use Policykit1 and dbus because all the other alternatives I've found aren't used anymore. 我知道我想使用Policykit1和dbus,因为我找到的所有其他替代品都不再使用了。

This is the code I got so far: 这是我到目前为止的代码:

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')

I can't make the directory, what am I doing wrong? 我无法制作目录,我做错了什么?

Filesystem security is stopping you because your user doesn't have write permissions to /usr/local/share/somefolder . 文件系统安全性阻止了您,因为您的用户没有对/usr/local/share/somefolder写入权限。 You could use sudo to temporarily escalate permissions for that directory creation. 您可以使用sudo临时升级该目录创建的权限。 But it doesn't stop there if you need to perform more operations as superuser. 但是,如果您需要以超级用户身份执行更多操作,它并不止于此。

If you need to write to something that isn't in user space, the entire program might be better of run as root (under sudo of course), such as sudo ./myscript.py . 如果你需要写一些不在用户空间的东西,那么整个程序最好以root身份运行(当然在sudo下),比如sudo ./myscript.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM