I'm trying to run gedit as root, and heard that it is better to use pkexec over sudo -i.

However, when I run pkexec gedit I see this:

Unable to init server: Could not connect: Connection refused

(gedit:18425): Gtk-WARNING **: cannot open display: 

Why does this happen, and how can I use pkexec with gedit?

  • Thanks, but it doesn't work. After creating the file I get the same message as "user3". What could be wrong? – user3110 Nov 15 '15 at 15:52

According to pkexec man:

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.

Thus to make it work - create a policy file:


And put inside following text:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policyconfig PUBLIC
 "-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN"

  <action id="org.freedesktop.policykit.pkexec.run-gedit">
    <description>Run Gedit</description>
    <message>Authentication is required to run Gedit</message>
    <annotate key="org.freedesktop.policykit.exec.path">/usr/sbin/gedit</annotate>
    <annotate key="org.freedesktop.policykit.exec.allow_gui">TRUE</annotate>


Note, this is a policy config only for gedit, it contains path to the executable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy