Using policykit+dbus instead of gksu to run graphical application
I'm searching for a command that does gksu or beesu job, but depends on policykit.
The policykit and dbus documentation is somehow very complicated a开发者_如何转开发nd not clear.
I found "pkexec" but it shows errors when trying to run a graphical application
pkexec gedit
results:
(gedit:7243): Gtk-WARNING **: cannot open display:
You need to create an action definition file
in /usr/share/polkit-1/actions
written in XML
.
Copy this to /usr/share/polkit-1/actions/com.example.gedit
:
<?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/policyconfig.dtd">
<policyconfig>
<action id="com.example.gedit">
<message gettext-domain="gedit">Authentication is required to run gedit as root</message>
<icon_name>accessories-text-editor</icon_name>
<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>
The value org.freedesktop.policykit.exec.allow_gui = true
will allow gedit to connect to your Xorg server. It'll automatically pass XAUTHORITY
and DISPLAY
to it.
Most of graphical programs need dbus so even if you manage to run it, it might fail.
For gnome programs at least following variables need to be set
'SESSION_MANAGER', 'DBUS_SESSION_BUS_ADDRESS', 'DISPLAY', 'GNOME_DESKTOP_SESSION_ID', 'XAUTHORITY'
. For kde programs you need 'SESSION_MANAGER', 'DBUS_SESSION_BUS_ADDRESS', 'DISPLAY', 'KDE_FULL_SESSION', 'XAUTHORITY'
I thing pkexec needs at least XAUTHORITY and DISPLAY to be able to connect to xorg server.
精彩评论