Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
    
Have you tried gksudo gedit /path/to/the/file/name instead? –  AzkerM Feb 3 at 9:09
    
@AzkerMohamed Yah, as my question says, using gksudo or gksu returns the same error. –  Parto Feb 3 at 9:15
    
@Sneetsher Still same error. –  Parto Feb 3 at 9:43
1  
Seems same case as this askubuntu.com/questions/175611/… –  Sneetsher Feb 3 at 9:47

3 Answers 3

up vote 3 down vote accepted

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 just 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 http://askubuntu.com/a/137584/163331 and realised I had to add more options to the visudo file. I deleted the first line Defaults env_keep+="DISPLAY" and added this one:

Defaults env_keep="XAUTHORIZATION XAUTHORITY TZ PS2 PS1 PATH LS_COLORS KRB5CCNAME HOSTNAME HOME DISPLAY COLORS"`
share|improve this answer

Running any graphical interface applications as root can result in file corruption, especially if the graphical interface application is a text editor. While there are commands to run graphical applications as root, you are strongly advised to use the 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 the 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.

share|improve this answer
1  
+1 for the nano editor. One of the best editor which anyone could find easier to operate. –  AzkerM Feb 3 at 9:13
    
@Karel I finally used nano to make the changes but I need my root Gedit back. –  Parto Feb 3 at 9:18

For gedit, try this:

sudo -H env DISPLAY=:0 gedit
share|improve this answer
    
Getting the same error. –  Parto Feb 3 at 9:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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