为什么 pkexec 比 gksudo 更适合图形应用程序?

chi*_*555 10 command-line

请提供反驳这一点的 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
Run Code Online (Sandbox Code Playgroud)

Sal*_*lem 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.
Run Code Online (Sandbox Code Playgroud)

正如手册页中所述,您可以使其工作,尽管我真的不知道这是否有某种危险或推荐

例如,要启用 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>
Run Code Online (Sandbox Code Playgroud)

然后pkexec gedit应该按预期工作:

在此处输入图片说明

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

为什么首选 pkexec?

在这里您可以找到有关pkexec.

  • 那么,对于图形应用程序,是首选 gksudo 而不是 pkexec? (3认同)