Search code examples
pythonlinuxrootsudodbus

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 and 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: 

Solution

  • 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.