How to start X application from SSH

128,476

Solution 1

If you don't care to see what the application is doing, you can supply it with a "virtual" x server with xvfb:

Xvfb provides an X server that can run on machines with no display hardware and no physical input devices. It emulates a dumb framebuffer using virtual memory. The primary use of this server was intended to be server testing, but other novel uses for it have been found, including testing clients against unusual depths and screen configurations, doing batch processing with Xvfb as a background rendering engine, load testing, as an aid to porting the X server to a new platform, and providing an unobtrusive way to run applications that don't really need an X server but insist on having one anyway.

After installing it, you can start it with:

sudo Xvfb :10 -ac -screen 0 1024x768x24 &

it'll run in the background, then you start your clients with:

DISPLAY=:10 your-client

Solution 2

A short command:

ssh -X <username>@<host> gedit &

from man ssh

-X      Enables X11 forwarding.
        This can also be specified on a per-host basis in a configuration file.

        X11 forwarding should be enabled with caution.  Users with the
        ability to bypass file permissions on the remote host (for the
        user's X authorization database) can access the local X11
        display through the forwarded connection. An attacker may then
        be able to perform activities such as keystroke monitoring.

  1. Check /etc/ssh/sshd_config on the server side:

    sudo nano /etc/ssh/sshd_config
    

    for the lines below:

    X11Forwarding yes
    X11UseLocalhost no
    

    Restart the ssh srever, if you have made changes:

    sudo service ssh restart
    
  2. Check /etc/ssh/ssh_config on the client side:

    sudo nano /etc/ssh/ssh_config
    

    for the lines below

    ForwardX11 yes
    ForwardX11Trusted yes
    

Solution 3

You just need to run export DISPLAY=:id# in your ssh session and programs run will run on the remote display. A quick example:

maythux@maythuxPC:~$ ssh testSSH@myServer
maythux@maythuxPC:~$ export DISPLAY=:0
maythux@maythuxPC:~$ gedit

Now gedit will run on the user named testSSH display

You can shorten this all down into single command:

ssh testSSH@myServer "DISPLAY=:0 nohup gedit"
Share:
128,476

Related videos on Youtube

Paweł Wojtal
Author by

Paweł Wojtal

Updated on September 18, 2022

Comments

  • Paweł Wojtal
    Paweł Wojtal almost 2 years

    I've tried to search this topic in google, but without any significant results.

    I need to start some GUI app from terminal, but I don't care what this app is showing me. In addition, I need to start few instances of this application in different sessions. It's connected with some GUI automated tests, so I know what I want and I'm looking for an answer :).

    I think the proper steps should be:

    1. Init new X window session
    2. Get my new session id
    3. export it to env variable (export DISPLAY:13.0)
    4. run my app

    but I'm stuck at first step. does anybody here had similar problem?

    --- EDIT: 1) I don't care about GUI output - i need only stdout and stderr from my app. - that's why this question is not duplicate - know google well :) . I don't want to redirect output to my own GUI.

    • Tim
      Tim about 9 years
      I think you can just start an X and then run it.
    • Rmano
      Rmano about 9 years
      It's not clear. Do you have a graphic desktop running locally? Or do you want to run an X application with no real display, neither locally nor remotely?
    • Rmano
      Rmano about 9 years
      Look at tldp.org/HOWTO/Remote-X-Apps.html --- maybe it can help.
    • Paweł Wojtal
      Paweł Wojtal about 9 years
      Rmano: I need to run app and click into it with robot-framework + selenium library - but I don't need to see any output from GUI, just logs from my scripts - is it an answer for your question?
  • daltonfury42
    daltonfury42 about 9 years
    it doesn't work for me. This is what I get.
  • Rmano
    Rmano about 9 years
    If the remote myServer is running X, the default DISPLAY is :0, not :1. It will only work if the user you connect to (testSSH) is the same one that is running the remote X server, otherwise auth tricks are needed.
  • Rmano
    Rmano about 9 years
    See also unix.stackexchange.com/questions/10121/… about the auth tricks.
  • Maythux
    Maythux about 9 years
    @Rmano It was just example and not real value, The OP must put the correct number
  • terdon
    terdon about 9 years
    You need ssh -Y or ssh -X. Exporting the display won't be enough.
  • Maythux
    Maythux about 9 years
  • Paweł Wojtal
    Paweł Wojtal about 9 years
    There is also nice script provided with Xvfb - xvfb-run which is doing exactly this thing which I need: create virtual display and execute command passed as argument.
  • Ivan Ferrer Villa
    Ivan Ferrer Villa over 7 years
    perfect, thanks! to not block current session waiting to command return, add & after the command. To run a command in a remote Raspberry I used lxterminal --working-directory=path-to-serverjs -e node server.js. It opened Node in a new terminal on remote desktop. Eureka! Kisses.
  • Abdull
    Abdull about 7 years
    command prompt is maythux@maythuxPC, even when on testSSH@myServer?
  • Scott Manley
    Scott Manley over 5 years
    ssh -Y <username>@<host> is what I prefer. superuser.com/a/123816
  • mchid
    mchid over 5 years
    @rehctawrats You can also use compression: ssh -CY <username>@<host> but I haven't tried this out yet.