Ubuntu – Why is pkexec preferred over gksudo for graphical applications

command line

Please provide Ubuntu documentation that refutes this: https://help.ubuntu.com/community/RootSudo Why, on my fully updated 13.04 system, does pkexec not function?

$ 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

Best Answer

Why it doesn't work?

By default pkexec does not allow you to run graphical (X11) applications. From the man page:

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

As stated in the man page, you can make it work albeit I really don't know if this is somehow dangerous or recommended.

To enable gedit for example you can create /usr/share/polkit-1/actions/com.ubuntu.gedit.policy with the following content:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policyconfig PUBLIC
 "-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN"
  <action id="org.freedesktop.policykit.pkexec.gedit">
   <description>Run "gedit"</description>
   <message>Authentication is required to run Text Editor</message>
     <annotate key="org.freedesktop.policykit.exec.path">/usr/bin/gedit</annotate>
     <annotate key="org.freedesktop.policykit.exec.allow_gui">true</annotate>

Then pkexec gedit should work as expected:

enter image description here

As you can guess, this will only make gedit work. In theory, if you added allow_gui to "org.freedesktop.policykit.exec" (the default action) this should work for all applications, but in my tests I got the same result as yours.

Why is pkexec prefered?

Here you can find a discussion about the strengths of pkexec.

Related Question