I am attempting to run nautilus as root but when I run "sudo nautilus" from the terminal, I get the following error:

error: XDG_RUNTIME_DIR not set in the environment.

(nautilus:9341): Gtk-WARNING **: cannot open display:

The issue does not occur when I attempt to run nautilus as non-root. I am using ubuntu 14.04. Does anyone know how I can fix this?

  • Just for the record, I ended up here after I had the same error come up when trying to do X11Forwarding via ssh. Solution: I had forgotten to use the -X option when starting my ssh session. – JW. May 15 '15 at 11:47

When you run software as another user you're in fact starting the new minimal and isolated environment that doesn't carry on some "excessive" variables (among others variables responsible for injecting libraries or setting certain privileges). Replace your sudo nautilus call with the following - it will carry on user-specific x server settings from the current session:


This is a one time low level solution but it will work on a malconfigured machine. If you want to permanently "fix your sudo" you need to find the issue with your environment configuration and correct it as described in other answers.

  • 1
    @Cyprian Guerra - This doesn't set in to the environment, this only lets you run one time. This is hardly helpful. Running sudo nautilus should work as normal from terminal when run. Normally whenever I do that from terminal on a fresh install it opens up as normal with sudo. – user94959 Mar 4 '15 at 10:57
  • @user94959 You don't understand the basic principle - the new environment is being set, the variables are carried over, the task runs, when the task exits this separate environment is being destroyed. Therefore yes, you need to set the variables for every new environment. If you wish more reading material try pkexec and sudo man pages as well as google for the related dispute. – cprn Mar 4 '15 at 16:43
  • @CyprianGuerra then how come when I try to open application from the desktop menu that requires elevated privileges can run after password input? obviously there's something missing in the configuration if it won't launch correctly because it's not set in the XDG environment like normal? the issue is when its saying Cannot open display means something is missing from it's configuration file because something modified it and removed it from the environment when it shouldn't of. So how can it be set back into the environment permanently then so I don't have to deal with this? – user94959 Mar 5 '15 at 11:58
  • 1
    @sherrellbc I'm telling explicitly policy kit to run env before running nautilus and the former takes care for setting the variables for me. You can check man env. – cprn Aug 21 '17 at 13:54
  • 1
    @cprn D'oh. I should have checked the man page first. env COMMAND ARG ... – sherrellbc Aug 21 '17 at 18:29

I also had the same problem on Ubuntu 14.04. Open terminal by pressing,

Ctrl + Alt+ T

then sudo visudo

change the line

Defaults env_keep="https_proxy"


Defaults env_keep += "https_proxy"

It worked like charm.


To make it easy - more explained the new booting of my system.

After all explanations here I came to the result - and "env" in terminal said already that is right for these session:

These two rows to use the environment variable:

for the tmp behavior I have chosen:

mkdir -pv ~/.cache/xdgr

For setting the environment variable:

export XDG_RUNTIME_DIR=$PATH:~/.cache/xdgr

After closing the terminal and a new open for the recall of env they tells:


note: it is ok for the user under ubuntu, root need more (last info by term - with 0700 permissions)


If you are getting this error in Docker ; this is what I do

# sudo xhost +
access control disabled, clients can connect from any host
# export DISPLAY=:0.0
# docker run -it --env DISPLAY=unix$DISPLAY --privileged  --volume /tmp/.X11-unix:/tmp/.X11-unix .. rest of your Docker arugments

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.