Ubuntu – “error: XDG_RUNTIME_DIR not set in the environment.” when attempting to run nautilus as root

nautilussudo

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?

Best Answer

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:

pkexec env DISPLAY=$DISPLAY XAUTHORITY=$XAUTHORITY nautilus

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.