Cannot connect to X Server when running app with sudo
You need to allow the root user access to the X server:
xhost local:root
And point the command to the right DISPLAY
:
sudo DISPLAY=$DISPLAY gedit /etc/profile
Finally the problem is solved. I have just added the line:
Defaults env_keep="DISPLAY XAUTHORITY"
at the end of visudo
.
To do that you have to run
sudo visudo
it will open the file then add the above line at the end.
Thanks to all, specially @Thor for kind help
Supplementing @Thor's answer:
Run xhost local:root
before sudo gedit
. To make this permanent, add the line
xhost local:root
to the file ~/.xinitrc 1. Create the file if it doesn't exist.
[1] https://askubuntu.com/a/720120/452398