56

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.

8
  • 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.
    – A.B.
    Commented Apr 26, 2015 at 16:50
  • 1
    @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. Commented Apr 26, 2015 at 16:53
  • You shouldn't have to use gksu to run GParted. gparted uses polkit to get elevated privileges.
    – muru
    Commented Apr 26, 2015 at 16:59
  • @muru when I run gparted without gksu I get Root privileges are required for running gparted. Commented Apr 26, 2015 at 17:17
  • Ok, try gparted-pkexec (without gksu) or gksu gpartedbin.
    – muru
    Commented Apr 26, 2015 at 17:39

6 Answers 6

89

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

4
  • @MDMower Thx for editing, learned something new =)!
    – Cutton Eye
    Commented Dec 15, 2017 at 7:46
  • 1
    @pomsky Thx for adding the link ;)!
    – Cutton Eye
    Commented Feb 26, 2018 at 10:49
  • In 18.04 X-Org will be used again as default. So this problem should vanis then. see
    – Cutton Eye
    Commented Mar 7, 2018 at 13:25
  • Same problem occurred with Ubuntu 20 and this solved it.
    – Mecanik
    Commented Sep 5, 2020 at 7:16
13

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
10
  • 1
    This does work indeed. So does that mean its an access control restriction? Any ideas what the cause of this could be? Commented Apr 27, 2015 at 22:21
  • 2
    This yields an error: xhost: unable to open display "" Commented Jan 30, 2017 at 18:33
  • 5
    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.
    – rmin
    Commented Sep 6, 2017 at 1:40
  • 1
    Indeed! xhost +local: fixed mine! It yields non-network local connections being added to access control list which is what is needed.
    – lobner
    Commented Nov 10, 2017 at 8:16
  • 1
    I get xhost: unable to open display ""
    – Nathan G
    Commented May 6, 2018 at 16:55
8

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
1
  • 5
    Would be useful if you could add some explanation of how this solves the problem, and whether it has any adverse side effects?
    – Geoff
    Commented Jan 1, 2019 at 23:25
2

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

0

I solve it by simply running in the terminal ,

sudo gparted

Looks like GTK+ root password dialog confused with x server settings

0

Just to add to possible issues that results in "Gtk-WARNING **: cannot open display: :0".

I got it after deleting /tmp/.X11-unix/X0. Solution I know of as of now: restart, creating socket by python -c "import socket as s; sock = s.socket(s.AF_UNIX); sock.bind('/tmp/.X11-unix/X0')" from https://serverfault.com/questions/358866/create-unix-named-socket-from-the-command-line did not help to resolve the error.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .