21

When I run sudo gedit /path/to/some/random/file I get the following error

** (gedit:6262): WARNING **: Command line `dbus-launch --autolaunch=7f8731869e1c690b8205727e0000000a --binary-syntax --close-stderr' exited with non-zero exit status 1: Autolaunch error: X11 initialization failed.
Cannot open display: 
Run 'gedit --help' to see a full list of available command line options.  

Using gksudo doesn't help either which returns

Cannot open display: 
Run 'gedit --help' to see a full list of available command line options.  

Any help guys. I need to have this capability to open files in Gedit as root.
I found this question with the same error but no solution Unable to launch gedit with sudo


EDIT
I found this post in Ubuntu Forums.
These are the instructions:

  1. echo $DISPLAY # you'll be needing this value 3 lines below
  2. sudo -i # or "su -" on older Slackwares
  3. xauth merge ~alien/.Xauthority # use your own username here instead of "alien"
  4. export DISPLAY=:0.0 # use the value of DISPLAY you've seen 3 lines before

On coming to command #3, I got this error
xauth: file /root/.Xauthority does not exist

Maybe this new info can shed more light.

4

6 Answers 6

22

Finally have found an answer. From this question Cannot connect to X Server when running app with sudo and especially this question Error when trying to run an app with sudo or gksudo.

I added the line Defaults env_keep+="DISPLAY" to the end of the visudo file at /etc/sudoers.

Yay, my sudo gedit is back!

EDIT

Got the same error again for sometime.

Did some more research, especially this answer https://askubuntu.com/a/137584/163331 and realized I had to add more options to the visudo file. Instead of Defaults env_keep+="DISPLAY", I used this line:

Defaults env_keep="XAUTHORIZATION XAUTHORITY TZ PS2 PS1 PATH LS_COLORS KRB5CCNAME HOSTNAME HOME DISPLAY COLORS"
1
  • Why add HOME to env_keep?
    – muru
    Jan 26, 2018 at 2:43
12

Running any graphical interface applications as root can result in file corruption, especially if the graphical interface application is a text editor. In addition to this gksu and gksudo don't work for starting graphical applications as root in Wayland in Ubuntu 17.10: Why don't gksu/gksudo work with Wayland? and the gksu package has been dropped from the default Ubuntu repositories in Ubuntu 18.04 and later.

Nautilus Admin (nautilus-admin) is a simple Python extension for the Nautilus file manager that adds some administrative actions to the right-click menu:

  • Open as Administrator: opens a folder in a new Nautilus window running with administrator (root) privileges.
  • Edit as Administrator: opens a file in a Gedit window running with administrator (root) privileges.

To install Nautilus Admin in all currently supported versions of Ubuntu open the terminal and type:

sudo apt install nautilus-admin  

While there are commands to run graphical applications as root, you are strongly advised to use nano text editor, which normally is run from the terminal, for editing files as root. Nano text editor is included in the default Ubuntu installation.

Nano is very easy to use. The instructions for using nano editor are always found at the bottom of every page. The only two nano keyboard shortcuts that you need to know are for WriteOut and Exit. Press the keyboard combination Ctrl+O and after that press Enter to save the file being edited. Press the keyboard combination Ctrl+X to exit nano.

If you get into the habit of editing files as root the right way, you will reduce the possibility of making mistakes that may be very time consuming to correct.

4
  • 2
    +1 for the nano editor. One of the best editor which anyone could find easier to operate.
    – AzkerM
    Feb 3, 2014 at 9:13
  • @Karel I finally used nano to make the changes but I need my root Gedit back.
    – Parto
    Feb 3, 2014 at 9:18
  • 3
    This is a good advice, but this is not the answer. Linux guys love terminal, but a GUI is awesome for complex tasks and has better UX.
    – azerafati
    Jan 9, 2018 at 14:25
  • You can customize the default editor of the sudoedit command (which is by default nano): How can I change the default editor of the sudoedit command to be vim? but starting GUI editors as root might cause problems in Ubuntu 17.10 and later with wayland.
    – karel
    Jan 9, 2018 at 14:32
8

In Ubuntu 17.10, at least, you can do this:

gedit admin:///path/to/some/random/file

You'll be prompted to enter your password, and then the file will be opened for editing as root.

3
  • Works under 18.04 as well...
    – Elder Geek
    Nov 5, 2019 at 22:01
  • Also works under Fedora 35
    – Daniel
    Apr 19, 2022 at 2:45
  • no success with wls 2 unbuntu 20.04. Unable to handle "admin:" locations May 31, 2023 at 16:08
4

For gedit, try this:

sudo -H env DISPLAY=:0 gedit
1
  • 1
    Getting the same error.
    – Parto
    Feb 3, 2014 at 9:26
1

I am trying to summarize all info in the other answers on this page:

Wayland:

The only option looks to used nano or vi/vim

Xorg:

try this

sudo -H env DISPLAY=:0 gedit

if it works it probably means you need to add some option to sudo configuration files

sudo visudo

Try to add this line at the end

Defaults env_keep="XAUTHORIZATION XAUTHORITY TZ PS2 PS1 PATH LS_COLORS KRB5CCNAME HOSTNAME HOME DISPLAY COLORS"`
2
  • Why add HOME to env_keep?
    – muru
    Jan 26, 2018 at 2:43
  • gedit works fine in Wayland. Please see the answer I just posted. Jan 26, 2018 at 2:50
0

I just had similar problem. I'm using Debian Jessie and to start gedit as root this line was then finally working:

sudo -H gedit

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .