python - 通过 pkexec 执行时缺少环境变量

标签 python linux

如果我在没有 pkexec 的情况下执行 print os.environ,我会得到很多有用的系统环境变量,但是,一旦我执行了,它们中的大部分都消失了。我怎样才能使用 pkexec 找回它们?我知道有一个使用 sudo 的解决方法,但还没有找到 pkexec 的解决方法

最佳答案

简而言之,你不能。来自pkexec man page :

The environment that PROGRAM will run in will be set to a minimal known and safe environment in order to avoid injecting code through LD_LIBRARY_PATH or similar mechanisms. In addition the PKEXEC_UID environment variable is set to the user id of the process invoking pkexec.

您可以使 pkexec 保留一些环境变量(即通过保留 $DISPLAY$XAUTHORITY 允许 X11 程序工作)使用org.freedesktop.policykit.exec.allow_gui 注释。但是,保留所有环境变量似乎是故意拒绝的决定。

关于python - 通过 pkexec 执行时缺少环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42960140/

相关文章:

python - Scrapy 不启用我的 FilePipeline

python - 链式 AND 条件给出不明确的结果

c++ - 为什么以及何时应该使用 IPC_NEW 创建信号量?

linux - 来自非 root 用户的单位(用于命令启动/停止/启用/禁用)

c++ - 从 Visual Studio 2017 为 linux 指定外部库

linux - syslog-ng perl 管道丢弃事件

python - 双正斜杠在 Mechanize 中消失

python - 遍历列表以获取 python 中给定范围的最大总和

python - 类型错误 : <built-in function id> is not JSON serializable

ruby-on-rails - 无法在 Linux 上安装 rabbitmq-server(Oracle Linux Server 版本 6.7/RHEL)