sudo + x11 application does not work correctly

In general, please remember to include your OS (I imagine you are on Linux) and distribution.

By default, the X server does not allow other users (e.g. root in your case) to connect to an existing X instance. This can be overridden with the xhost + command, after which root can connect to your existing X session.

Exporting DISPLAY should not be necessary, are you sure that it was? You should be ok by simply running xhost + and then sudo gedit or whatever. If that is the case (and you don't mind the slight security risk) you can add xhost + to your ~/.bashrc.


As a horrible workaround, you could run sudo specifying the $DISPLAY variable:

sudo DISPLAY=:0 gedit

To automate, you could add these lines to your ~/.bashrc:

xhost + 
alias sudo='sudo DISPLAY=:0'

This is a horrible hack though, and I would like to know how you managed to get yourself in a situation where it is necessary :). As I said in my comment above, this really should work out of the box on Ubuntu (and most other modern desktop Linux systems). I last had to to fiddle with xhost and DISPLAY when I was working with RedHat about 10 years ago...