How do I run a graphical application on a remote server when logged in through SSH?

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.


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.

Tags:

Ssh

Xorg