How to run a GUI app from ssh shell?

7

4

I can access my linux box by ssh and by vnc. I want to run a GUI application, but directly from ssh, I don't want to access through VNC and click around. So, after logging in using ssh, I want to issue a magic command, so that when I log in through VNC I will see my GUI app running. How can I do this?

edit:

The linux box have X server running on it. I need to automate restarting a GUI application. I want to do it without any kind of GUI interaction. What I need:

  1. login through ssh on SERVER
  2. run my GUI app by forcing it to bind to X server running on SERVER
  3. ???
  4. PROFIT!

karramba

Posted 2010-06-17T19:39:57.967

Reputation: 73

Answers

6

I've read the edited version of the question, and if I understand you correctly, you want to run a program from SSH without showing you the GUI... you just want to run the program and it depends on X Windows, so you need it to connect somehow to X Windows on the server itself.

There are two things you need to do. You need to allow connections from outside of X Windows, and then you need to tell the shell (in SSH) which X server to bind to.

First, allow incoming connections to the X server. Open up a terminal window in X Windows on the server machine. (You must have access to that, otherwise you cannot do this.)

Issue the following command:

xhost +

It should tell you "connections allowed from all hosts" or something to that effect.

Then, while still remaining in X Windows, issue:

echo $DISPLAY

This will tell you the display ID. Write it down or remember it. Typically it will be ":0" or ":0.0" but don't worry if it's different.

That's all you need to do from within X Windows itself.

Now SSH into the server from wherever you want. Issue the command:

export DISPLAY=[what-the-echo-command-gave-you]

And that should be it! Now you should be able to run any X windows from that SSH shell, and it will pop up on the local X Windows server.

Hope it helps!

Teekin

Posted 2010-06-17T19:39:57.967

Reputation: 365

4xhost + is an exceedingly bad idea. If your user started the Xserver then you should have the appropriate Xauthority file already, no need to mess around with the server host acls. – Geoff Reedy – 2010-06-17T20:55:36.390

@GeoffReedy I do not understand what you mean. Following Helgi's instructions, I managed to fire up an application from SSH. How could I do it without xhost +? What are the risks I'm taking (I had to login anyway)? – Camilo Martin – 2012-02-11T00:27:36.297

1

@CamiloMartin @Helgi xhost + turns off all access control for the x server. Anyone who can connect to the X server can snoop events, inject events, dump window contents, kill programs running on the X server, etc. These capabilities could be used as a denial of service, arbitrary program execution or other bad things. See http://www2.slac.stanford.edu/computing/security/xwindow/ for some more info on this. What you should do under most setups is run echo $XAUTHORITY instead. If it is not empty, then when you want to run a GUI program do export XAUTHORITY=<saved content of $XAUTHORITY>.

– Geoff Reedy – 2012-02-13T15:31:03.857

@GeoffReedy, thanks for the tip. I haven't used this trick myself for years, but next time I'll be sure to search for Xauthority instead. – Teekin – 2012-02-13T15:31:19.643

@GeoffReedy Thank you very quite a bit much some lot. Made it into a script! Put this gimme-xauth.sh in your /usr/bin for pleasure and Xstasy. http://pastebin.com/GXx2hwC5

– Camilo Martin – 2012-02-17T04:21:59.043

Oh and note that it only works for gdm, I'm a n00b so I dunno how to make it more agnostic. I also don't currently have a non-gdm3 box anymore... – Camilo Martin – 2012-02-17T04:28:51.423

Now, the script does not work correctly: export has to be typed in ssh anyway (which still helps, as I can type it since it's shown). Anyone knows why? – Camilo Martin – 2012-02-17T04:39:18.460

5

You have to forward X11 to your local machine (from the remote machine). Pass the -X or -Y flags when invoking ssh.

mipadi

Posted 2010-06-17T19:39:57.967

Reputation: 3 980

Note the question mentions that he would like to run the X-server on the server and access it VNC, not run the X-server locally. – heavyd – 2010-06-17T21:00:42.447

4

What display the app comes up on is dictated by the DISPLAY environment variable. do export DISPLAY=:0.0 to make it come up on the first display of the remote machine.

Daenyth

Posted 2010-06-17T19:39:57.967

Reputation: 5 742

0

First, this is not a programming question (s/b serverfault.com)

However, you need to run an X server on your system (something like XMing for example), then run the GUI program on the linux box, this will put the GUI on your machine.

KevinDTimm

Posted 2010-06-17T19:39:57.967

Reputation: 220

I don't want to run this program on my machine, I want it to run on remote host, but from ssh shell, not using vnc. I can't run it directly from ssh, bacause there is no x server running for that shell (?). – None – 2010-06-17T19:56:48.680

correct - the only way to see the GUI is an X server on your machine (if it's an X program). Otherwise, see mipadi's answer for forwarding X11 for an alternative (this doesn't always work) – KevinDTimm – 2010-06-17T20:08:25.437

there is no way to force an application to somehow "bind" to an X server, that is running on linux box? I don't need to interact with the GUI, just to run the app. I'd like to force it to use X available on the server. I updated my question to explain better. – None – 2010-06-17T20:23:37.927

0

FYI, you can do something like this in the Windows realm of things, such as launching a UNIX process, by using WinSSHD and/or Tunnelier .

If what you mean is launching the gui in a local X windows session then you need to set the DISPLAY variable to local ( Tunnelier may have a setting for that) and because your on Windows you need to combine that with running a x-window emulator on windows (such as cygwin-X )

djangofan

Posted 2010-06-17T19:39:57.967

Reputation: 2 459

0

I find it easiest to do this using a terminal multiplexer like screen or tmux. It requires no messing around with xhost. copying and pasting authorizations, or anything like that.

The basic idea:

  1. On the remote machine, open a virtual X terminal; in that virtual terminal, start a screen or tmux session. (This does mean clicking around in VNC, but this is the only time you have to do this.)
  2. Now, whenever you SSH into the remote machine, you can attach to this screen or tmux session.
  3. Once attached, you can run any GUI app and it should pop up on the remote machine as if you had run it from there.

GNU screen example:

In remote machine's virtual terminal (opened via VNC or physically):

you@remote:~$ screen -S myscreen

Then, when you SSH in, you can just attach to that session:

you@remote:~$ screen -x myscreen

And then, from within the screen session, run whatever apps you want:

you@remote:~$ myapp &

Statisfactions

Posted 2010-06-17T19:39:57.967

Reputation: 168