150

Sometimes I need to start XMBC media player or other GUI software on one of my remote PC (small Xubuntu PC used as a media center).

Usually, I do this by starting an X11vnc server on the remote PC via SSH and then connecting with an Xvnc client to the Xfce desktop.

Is there a way to start a GUI software on a remote Linux PC via SSH?

Thanks!

Raben
  • 1,603
  • Can confirm that the approach in chosen answer works if the remote client is a Mac, too. Working successfully with macOS Sierra. – Benjamin R Feb 15 '18 at 06:01

5 Answers5

181

Yes. You just need to run export DISPLAY=:0 (or whatever the remote display is numbered as) in your ssh session and programs run will run on the remote display. A quick example:

oli@bert:~$ ssh tim
oli@tim:~$ export DISPLAY=:0
oli@tim:~$ firefox

Firefox is now running on tim's display.

However when you close your ssh session, most of the time the remote application will close. If you want to disconnect from ssh but leave the application running you need to launch it in a special way using something like screen (keeps the ssh session running in the background) or nohup, or another method. For more information on this there was recently another question on it.

You can shorten this all down into one command that will connect, export the display in-line and start the application in a way that won't close it after the ssh session dies:

ssh tim "DISPLAY=:0 nohup firefox"
Oli
  • 293,335
  • It said No DISPLAY: this may not be what you want. when i tried it (I tried it from abiword by the way. ` – Christopher King Nov 29 '14 at 17:14
  • 2
    @PyRulez Note the "or whatever the remote display is numbered as" in the first line. If it's not the first graphical server it might be :1 or higher. Run w to see who's logged in and where. That will tell you the DISPLAY number too. – Oli Nov 29 '14 at 17:20
  • I do have a display 0, and it is the only display and it is physically on and working. – Christopher King Nov 29 '14 at 17:26
  • 3
    How do I turn it back off – akxer Mar 16 '16 at 06:42
  • @akabhirav How do you mean? – Oli Mar 16 '16 at 08:04
  • I have turned it on and everything I do opens on my computer's screen. I want it to go back to normal and stop doing it, without opening a new shell – akxer Mar 16 '16 at 08:27
  • 3
    unset DISPLAY – Oli Mar 16 '16 at 08:30
  • 8
    sometimes you want to do the opposite and run the X app locally just connect using -Y and then run your app ssh -Y <remoteip> – Postadelmaga Mar 27 '16 at 15:08
  • I have only mouse and starting the keyboard remotely from the command line is reaaaally helpful! :) ssh user@localhost "DISPLAY=:0 nohup onboard" – GTodorov Jun 06 '17 at 00:34
  • what is you want to start the whole Uniy GUI Session? – Lucas Pottersky Oct 19 '17 at 17:57
  • Tried ssh-ing into macOS (Sierra) laptop from my linux box, works exactly as expected. Can get mpv to play a video file on my mac laptop AND it passes along the keyboard shortcuts as well if the term session is the active window. This is damned awesome, thank you! – Benjamin R Feb 15 '18 at 05:59
  • I just get No protocol specified Unable to init server: Could not connect: Connection refused Error: cannot open display: :0 when trying to use this. – Luca Cappelletti May 14 '19 at 11:49
  • FYI, the value of $DISPLAY should be :0 . the : is not part of the export syntax. – Tejas Kale Dec 15 '19 at 22:38
21

Depends on where you want to see the application displayed

To display the application on your local PC

You first ssh to the remote computer with the additional -Y option and the run the application (e.g. firefox):

ssh -Y ...
firefox

If -Y doesn't work check you sshd config on the remote PC (see Denis Lukinykh's answer). Another similar option is -X. Google for the differences.

To display the application on an existing session at the remote PC

You need to login with user A at the remote PC and leave the session open. Afterwards you can ssh with the same user A and start the application (e.g. firefox) like this:

ssh A@...
DISPLAY=:0 nohup firefox

To display the application nowhere

You need to install & start xvfb. xvfb will create an invisible X session at DISPLAY 10. Then you start your application directing its output to that DISPLAY:

sudo apt install xvfb
sudo Xvfb :10 -ac -screen 0 1024x768x24 &
DISPLAY=:10 firefox
ndemou
  • 2,090
  • 1
  • 14
  • 18
7

A modern solution that should work with Wayland sessions as well, set up all the environment variables used in modern sessions (XDG_RUNTIME_DIR, GTK_MODULES, XDG_DATA_DIRS, XAUTHORITY, SESSION_MANAGER etc.), forward the application's console output to the journal, and run it in the background without stealing your ssh shell or quitting when you close the ssh session:

ssh tim 
export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$UID/bus
systemd-run --user firefox
  • 1
    in my case the DBUS_SESSION_BUS_ADDRESS envar was already correct, so it's even easier. – Will S Apr 23 '22 at 13:48
  • Is it possible to assign it some id to make it possible to ask systemd to stop it later? – Andreas Jun 09 '23 at 19:37
  • @Andreas The systemd-run command outputs Running as unit: run-u544.service (544 being the ID), which you could kill with systemctl --user stop run-u544.service – Eric Reed Sep 10 '23 at 04:45
  • 1
    @EricReed After discovering the --unit= option this became truly useful and is exactly what I need: I can now start and stop applications from static environments where I cannot dynamically persist any data, like in iOS Shortcuts, simply with systemctl --user start kodi || systemd-run --user --unit=kodi kodi and systemctl --user stop kodi. – Andreas Oct 26 '23 at 12:15
  • This is the only command that worked for me (all the other answers failed with "Unable to init server"). Thanks!! – m3rosss Mar 18 '24 at 10:46
0

Make this settings on remotehost:

ssh remotehost 'grep -i x11 /etc/ssh/sshd_config'
   X11Forwarding yes
   X11DisplayOffset 10 

After that, you can run GUI application:

ssh -Y -t remotehost 'sudo gparted'

or

ssh -Y remotehost
sudo gparted
0

Let's say you want to run gnome-disks.

You need 2 ssh sessions. One is used to run the software ( in this case gnome-disks). In the second one you do whatever you want to do.

In the first session execute these commands:

    export DISPLAY=:0
    gnome-disks
BHP
  • 433