Whenever I open gedit,nautilus and other files in terminal using sudo command, I get the error -->

Unable to init server: Could not connect: Connection refused

(gedit:22993): Gtk-WARNING **: cannot open display: :0

The startx command and changing the gnome theme(example Wayland,Xorg) does not work too with this error


The solution for you would be to simply use gnomesu instead of sudo. As written here,

it is a normal security issue. You have a multi user system where many users can login, either local or remote, in a GUI session. What if other logged in people would be able to open windows on your desktop without your allowance? Inconvenient at least, but the would also be able to create a window such that thay could read all your keyboard typing including your bank accounts. Thus the need for giving allowance with xhost.

There are multiple ways to achieve this, however this is the most simple way so I'd suggest you doing this. If you want to use the normal sudo(for I don't know what reason) anyway, you can do a change stated in this question.

Try running xhost +localhost in your terminal, and then running the sudo again. This lets all users on your system (i.e. root) open windows on your screen. Make sure to use +localhost and not simply +, as it's more secure to allow connections from only localhost than from anywhere.

To make this permanent, edit the ~/.xinitrc file like this:

Run gedit ~/.xinitrc

Edit the file to look like this (it should be empty at the start):

xhost +localhost &

Now save the file, log out and log in. Now everything should run just fine with sudo.

  • xhost +localhost is not a good idea, and adding it to .xinitrc does not work for most users because most users don't run startx. – Gilles 'SO- stop being evil' Jun 30 '17 at 21:13
  • Why is it a bad idea? – ADDB Jun 30 '17 at 21:17
  • As explained in the first part of your answer, it allows any process running as any user on your machine to interact with the programs you run. – Gilles 'SO- stop being evil' Jun 30 '17 at 21:29
  • But only from your localhost so as long as it is for personal use there shouldn't be any problem. – ADDB Jun 30 '17 at 21:31

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.