I am trying to run gedit from an SSH terminal on Windows, connected to an Amazon EC2 instance running Ubuntu 12.04 Server and I am getting this error:

Cannot open display:

How do I fix it?


Since you are using Windows on your side, you will need an SSH-client that runs a local X server on Windows to run graphical applications from the server remotely on your screen.

From experience, I would recommend the free MobaXTerm which has this functionality built-in. Please download it and give it a try:

enter image description here

If you get any errors, that's because sometimes a minimal server image may not be set up to allow X-forwarding into it. In that case, please comment and I'll help you solve it; I've done this with lots of minimal VPSs running Ubuntu/Debian.

| improve this answer | |

So you are connecting from your home pc (let's call it your machine) to a server (let's call it server) via ssh? You want to start a program with a graphical window on the server via your machine? If that is correct than by all means read on.

Let's call the program to start gedit but the process is the same for all. Depending on where you want to see the graphical window skip to the right heading.

Window on your machine's screen

If you want to see and use gedit's window on your remote machine but start in on the server, you will want your shell session to look like this:

user@remote-machine:~$ ssh -X user@server
Last login: Tue Aug 14 13:47:04 2012 from clientXX
M O T D (Message Of The Day)
user@server:~$ gedit .bash_aliases 

** (gedit:7851): WARNING **: Could not load theme icon gtk-home: Icon 'gtk-home' not present in theme

(gedit:7851): Gtk-WARNING **: Attempting to store changes 

Don't mind the warnings, they are normal - the gedit window will pop up on your remote machine's screen like it would look on the server. The trick is to use the "-X"-option as in ssh -X which enables X-tunnelling and sets the DISPLAY variable right.

This assumes that you're using an SSH client that supports X11 and that your server has an X-server running (some without a need for GUIs don't run an X-server). Especially on windows this might be an issue as most windows terminal programs don't have X. As izx stated the MobaXTerm is one of the many SSH clients for windows that has an X-Server though.

For a server lacking X11 however, you'd have to install it.

On the server's screen

You can also start an instance on the screen of your server by calling gedit like this:

ssh user@server
DISPLAY=:0.0 gedit

You have to substitute the right value instead of :0.0 though to get it on the right X-screen (only important if multiple screens are connected to your server or if it's somewhat peculiar in its configuration).

Here the most common scenario for that not to work would be if the Server lacked an X11-Server but than it wouldn't have a monitor connected to it or have no graphical user interface and you'd not be reading this :D.

A bit of theory

I'm no expert in this, but the way I think it works is, that each physical screen, i.e. each monitor connected to an individual computer gets a running number in the form of 0.0, 1.0, 2.0,...

This number is assigned by the X-Window-System aka X11. That is a layer to provide a network and basic graphical interface that Unity, Gnome, KDE and the like can build upon.

You can see how many displays a given machine has and what numbers are in use by running for x in /tmp/.X11-unix/X*; do echo ":${x##*X}"; done on the machine in question.* Then each computer that is configured to make its screens available and runs an X server has a hostname. You can use a hosts screens from any machine in the same network by setting your DISPLAY variable accordingly to DISPLAY=hostname:0.0. Ssh does this for you in a sensible way if you provide the -X option.

*Note: I suppose there is a better way than this for-loop.

If all else fails

Use a shell text editor. vi, vim, nano and emacs are the most common and at least one of them will be installed and operational. Of them nano is the easiest and most self explanatory.

| improve this answer | |
  • @coding crow: If you're SSHing from Windows it depends on the client whether or not that will work. Some clients don't communicate with X11 and thus no X-forwarding - for some though it just works fine. If not working change the SSH client for Windows or use nano, vim or emacs as xyious suggested in his answer. – con-f-use Aug 14 '12 at 12:19
  • Yes I got a down vote, saw it in my profile and wondered why there is no comment or anything. Not so important didn't have to do with you, just wondered - glad you problem is solved and I could be of a little help here. – con-f-use Aug 14 '12 at 14:27
  • I have modified the for loop a bit..it will now show the display address only e.g. :0 :0.0. Feel free to revert back if you think its not what you want.. – heemayl Apr 24 '15 at 8:15

What con-f-use said.

Basically you have a terminal through SSH and Gedit is a GUI program. con-f-use's answer will make it work from a Linux GUI client session.

The other option would be to use a non GUI editor like nano, Vi or numerous others.

| improve this answer | |

Well, I had this issue of being unable to use X forward on my vagrant instance. Solution for that was pretty straight forward. Simply install the xauth package. If it still doesn't works then the solution is to enable X11Forwarding and related settings in the respective /etc/ssh/ssh_config and /etc/ssh/sshd_config` These are the only issues that usually need fixing in terms of X forwarding.

Also, one of the most crucial method to fixing the ssh related issues is to use ssh -v the verbose logging allows you to see what is going on.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.