Can't open Gedit as root

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"

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.


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.

Tags:

Sudo

Gedit