为什么对于图形应用程序,pkexec优于gksudo?


10

请提供驳斥此内容的Ubuntu文档:https : //help.ubuntu.com/community/RootSudo为什么在我完全更新的13.04系统上,pkexec无法正常运行?

$ pkexec gedit somefile.txt
No protocol specified

** (gedit:13135): WARNING **: Could not open X display
Cannot open display: 
Run '/usr/bin/gedit --help' to see a full list of available command line options


pkexec并不是gksu的替代品。它不适合您手动运行。
psusi

2
我既没有询问也没有提及gksu。
chili555 2013年

关于pkexec不配置运行GUI的一个很好的答案:askubuntu.com/a/332847/89385
akostadinov

安装policykit-1-gnome。有关详细信息和说明,请参见此处
user303371

Answers:


7

为什么不起作用?

默认情况下pkexec,不允许您运行图形(X11)应用程序。从手册页:

 The environment that PROGRAM will run it, 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.
     As a result, pkexec will not allow you to run X11 applications
     as another user since the $DISPLAY and $XAUTHORITY environment
     variables are not set.
 These two variables will be retained if the
 org.freedesktop.policykit.exec.allow_gui annotation on an action is set
 to a nonempty value; this is discouraged, though, and should only be
 used for legacy programs.

正如手册页中所述,尽管我真的不知道这在某种程度上是危险的还是值得推荐的,但是您可以使其正常运行。

例如,要启用gedit,您可以创建/usr/share/polkit-1/actions/com.ubuntu.gedit.policy以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policyconfig PUBLIC
 "-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN"
 "http://www.freedesktop.org/standards/PolicyKit/1.0/policyconfig.dtd">
<policyconfig>
  <vendor>gedit</vendor>
  <vendor_url>gedit</vendor_url>
  <icon_name>accessories-text-editor</icon_name>
  <action id="org.freedesktop.policykit.pkexec.gedit">
   <description>Run "gedit"</description>
   <message>Authentication is required to run Text Editor</message>
   <defaults>
     <allow_any>auth_admin</allow_any>
     <allow_inactive>auth_admin</allow_inactive>
     <allow_active>auth_admin</allow_active>
   </defaults>
     <annotate key="org.freedesktop.policykit.exec.path">/usr/bin/gedit</annotate>
     <annotate key="org.freedesktop.policykit.exec.allow_gui">true</annotate>
   </action>  
</policyconfig>

然后pkexec gedit应按预期工作:

在此处输入图片说明

如您所料,这只会gedit起作用。从理论上讲,如果您添加allow_gui到“ org.freedesktop.policykit.exec”(默认操作),则该方法应适用于所有应用程序,但是在我的测试中,我得到的结果与您的相同。

为什么首选pkexec?

在这里,您可以找到有关的优势的讨论pkexec


3
因此,对于图形应用程序,gksudo是首选而不是pkexec吗?
chili555 2013年

1
大概吧。如果gksudo可用,则使用它要容易得多,而不是对需要使用的每个应用程序创建单个操作(如果没有“全局”方法可以做到)。
2013年

1
@ Salem-然后,我想知道为什么我的答案在不打算用于图形应用程序且您无法阅读时却被编辑并批准使用pkexec。askubuntu.com/questions/313619/…肯定有些我不理解的东西。
chili555

@ chili555如果该修改是引起您问题的动力,则应在Meta处提出。在13.04中,您必须安装gksudo或配置pkexec(也可以使用,sudo -i但不适用于Alt + F2,也称为“运行”)。我发现第一个要容易得多。如果有人认为不是很好...对我来说,建议某些无法按预期工作的方法似乎是错误的,但我可能会遗漏某些东西……
Salem 2013年

1
pkexec实际上无需配置即可运行GUI:askubuntu.com/a/332847/89385
akostadinov
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.