gksu: Gtk-WARNING **: cannot open display: :0

221,495

Solution 1

If running Ubuntu 17.10 or newer, this issue can arise when an application has not been updated with full support for Wayland. As a workaround until the application is updated, you can run

xhost +SI:localuser:root

which will allow the root user to display applications on your desktop. Also see this Q&A for other possible workarounds: Why don't gksu/gksudo or launching a graphical application with sudo work with Wayland?

source

Solution 2

Try running xhost +localhost in your terminal, and then running the command 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):

#! /bin/bash
xhost +localhost &

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

I can't reproduce your problem on 14.04, but this has worked for me in the past when sudo / gksu threw this error.

Sources:

  1. http://www.nikhef.nl/~mjg/xhost_plus.html
  2. https://forums.opensuse.org/showthread.php/405624-sudo-doesnt-open-X-programs
  3. https://wiki.ubuntu.com/CustomXSession

Solution 3

Execute the following in your terminal:

nano /home/user/.bashrc # user = name of your user

Add the following line at the end.

export XAUTHORITY=$HOME/.Xauthority

Solution 4

xhost + fixed my problem

but Be aware that xhost + completely deactivates authentication and allows everyone to access all application on your screen...

xhost +si:localuser:root seems to work similar with proper authentication

Share:
221,495
d_inevitable
Author by

d_inevitable

Updated on September 18, 2022

Comments

  • d_inevitable
    d_inevitable over 1 year

    All of the sudden gksu stopped working for me:

    ~$ gksu gparted
    
    (gpartedbin:24252): Gtk-WARNING **: cannot open display: :0
    

    The same happens with gparted-pkexec:

    ~$ gparted-pkexec 
    No protocol specified
    
    (gpartedbin:25454): Gtk-WARNING **: cannot open display: :0
    

    What could possibly be causing this?

    I am not running this through SSH or VNC. This is localhost in a normal terminal window.

    • A.B.
      A.B. about 9 years
      You do not work in a graphical user interface or trying to run a program on a system (eg. via ssh) that no graphical interface provides.
    • d_inevitable
      d_inevitable about 9 years
      @A.B. I should clarify that this is not via ssh. It's on local host in a very normal terminal window. Infact opening gparted from the application menu has the same result.
    • d_inevitable
      d_inevitable about 9 years
      @muru when I run gparted without gksu I get Root privileges are required for running gparted.
    • muru
      muru about 9 years
      Ok, try gparted-pkexec (without gksu) or gksu gpartedbin.
    • d_inevitable
      d_inevitable about 9 years
      @muru both of them give me the same Gtk-Warning as above. Same for gksu gedit. I have already formatted my disk through the command line. So it is not specifically gparted that I am concerned about.
    • TheSchwa
      TheSchwa about 9 years
      What is the output of echo "$DISPLAY" ?
    • d_inevitable
      d_inevitable about 9 years
      @TheSchwa $DISPLAY is :0
  • d_inevitable
    d_inevitable about 9 years
    This does work indeed. So does that mean its an access control restriction? Any ideas what the cause of this could be?
  • jalkoby
    jalkoby about 9 years
    Yes, it's a security feature of X. It allows only connections from your user to the xserver until told otherwise, I think. I'm not super sure why it's required for some apps and not others, and I don't have a clue why it would be doing it for gparted of all things. Perhaps you were tinkering and tightened security somewhere?
  • d_inevitable
    d_inevitable about 9 years
    Well its happening with all apps, even gedit. Also I have noticed that it is only happening for one of my two users on the system, not the other. I've been tinkering with a lot of things, but not recently. A list of possible things that could cause this behaviour could help a lot.
  • jalkoby
    jalkoby about 9 years
    OK, not sure the cause, but I do have a fix. You need to add the xhost command to the .xinitrc file for the user so it runs when you start the session.
  • reexmonkey
    reexmonkey over 7 years
    Thanks it worked! Desperate was I but you saved my day!!!
  • Steven Roose
    Steven Roose over 7 years
    This did not work for me... Any other idea?
  • IgorGanapolsky
    IgorGanapolsky over 7 years
    This yields an error: xhost: unable to open display ""
  • rmin
    rmin over 6 years
    On redis, it was suggested to use xhost +local:. After that I was able to launch gparted from the terminal, but using xhost +localhost didn't work.
  • lobner
    lobner over 6 years
    Indeed! xhost +local: fixed mine! It yields non-network local connections being added to access control list which is what is needed.
  • Cutton Eye
    Cutton Eye over 6 years
    @MDMower Thx for editing, learned something new =)!
  • Cutton Eye
    Cutton Eye about 6 years
    @pomsky Thx for adding the link ;)!
  • Cutton Eye
    Cutton Eye about 6 years
    In 18.04 X-Org will be used again as default. So this problem should vanis then. see
  • Nathan B
    Nathan B about 6 years
    I get xhost: unable to open display ""
  • Amit Kumawat
    Amit Kumawat over 5 years
    Would be useful if you could add some explanation of how this solves the problem, and whether it has any adverse side effects?
  • Mecanik
    Mecanik over 3 years
    Same problem occurred with Ubuntu 20 and this solved it.