37

When I am running some software using sudo command it showing error like

cannot connect to X server

For example if I run the following command:

$ sudo gedit /etc/profile

I am getting following command

(gedit:6758): WARNING **: Command line `dbus-launch --autolaunch=84b871d735f31ffe014dc9ba00000009 --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.

Or if I am running super-boot-manager I am getting following error

buc: cannot connect to X server

Please help.

Peachy
  • 7,077
  • 10
  • 37
  • 45
Apurba
  • 1,428
  • 2
  • 16
  • 29
  • Where do you run `super-boot-manger` from? – Thor Aug 15 '12 at 09:49
  • First I have tried to run `super-boot-manager` directly from unity luncher, it did not worked. Then I have tried it from terminal, then I got those error massage. – Apurba Aug 15 '12 at 13:21
  • As in a terminal within the graphical user environment? What does: `echo $DISPLAY` say? You should run it without `sudo` as `super-boot-manager` runs `sudo` internally. – Thor Aug 15 '12 at 15:23
  • `echo $DISPLAY` is giving `:0.0`. And I am not running `super-boot-manager` as `sudo`. I am running just using command `super-boot-manager` from terminal. – Apurba Aug 16 '12 at 19:36
  • also If I am not using the part `DISPLAY=$DISPLAY` in command `gedit` is not opening using `sudo` – Apurba Aug 16 '12 at 19:40
  • `buc` might be running as a different user, try removing the X restrictions with `xhost +`. – Thor Aug 16 '12 at 19:51
  • 2
    Finally the problem is solved. I have just added the line: `Defaults env_keep="DISPLAY XAUTHORITY"` at the end of `visudo`. Thanks to all, specially @Thor for kind help. – Apurba Aug 17 '12 at 08:03
  • Nice solution, forgot about `sudoers` `env_keep`. – Thor Aug 17 '12 at 08:09
  • You should add your solution as an answer, it would make it easier for others to find. – Thor Aug 27 '14 at 09:34
  • I've made a script for this purpose: gist.github.com/wachambo/bd22e12db2d5a46dc109bd0d553733be – Alejandro Blasco Oct 09 '18 at 12:42
  • Can you please hep me with this issue https://stackoverflow.com/questions/71464381/java-runtime-execution-not-working-in-cron-job-reboot-showing-error-unable-to – rickoonidioser Mar 20 '22 at 12:42

5 Answers5

52

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
Thor
  • 3,513
  • 25
  • 27
  • That solved the problem with opening `gedit` as `sudo`, but still can't lunch `super-boot-manager`. It is showing same error – Apurba Aug 15 '12 at 05:44
  • 1
    [I've just learned that](https://help.ubuntu.com/community/RootSudo#General_workarounds) `sudo -H` should be used when starting graphical applications, to avoid file corruption of X-related files. – jpaugh May 23 '20 at 09:44
  • setting the DISPLAY in sudo was not even required for me - Debian Buster. – Joerg S Oct 27 '20 at 23:02
  • How can I make this permanent? – user508402 Oct 03 '21 at 19:56
  • @user508402: that depends on many things. You should probably post this as a new question, where you detail your current setup, i.e. shell, window manager etc. – Thor Oct 05 '21 at 12:59
20

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

Apurba
  • 1,428
  • 2
  • 16
  • 29
  • 2
    Defaults env_keep="DISPLAY" works for me. – sugab Mar 09 '15 at 16:28
  • I have been searching high and low for this answer forever. I wish I could upvote both of you multiple times for your answers. Thank you so very much! :) – Ev- Dec 28 '16 at 06:38
5

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

adabru
  • 230
  • 4
  • 7
0

Actually the code you are running has cv2.imshow() or any other display commands, for remote running of code you have to comment them, and better save for visualization.

Zubair Khan
  • 101
  • 1
0

set DISPLAY and XAUTHORITY variable environment before running your scrip. for example in systemctl service file:

[Unit]
Description=Start Clock

[Service]
Environment=DISPLAY=:0
Environment=XAUTHORITY=/home/pi/.Xauthority
ExecStart=/usr/bin/python3 /home/pi/clock.py
Restart=always
RestartSec=10s
KillMode=process
TimeoutSec=infinity

[Install]
WantedBy=graphical.target
Hamid
  • 101
  • 2