How to Run an X Program from Outside the X Session (E.G. from the Console or Ssh)

How to run an X program from outside the X session (e.g. from the console or SSH)

The short answer is that you have to set the DISPLAY environment variable, and then the app will run.

The long answer is that we've got Xauth, and unless you're running as the same user on the same machine that's probably not going to work unless you export the Xauth credential from the account running the X server to the account running the X client. ssh -X handles this for you, which is why it's awesome, but the manual procedure involves running xauth extract - $DISPLAY on the X server account and feeding that data into xauth merge - on the client account. (Warning: the data is binary.)

On modern Linux systems, there is one X session at :0 and the X11 authority data file is always $HOME/.Xauthority so you can most often set two environment variables, for example, in Bash:

export XAUTHORITY=/home/$your_username/.Xauthority
export DISPLAY=':0'

How to run a GUI app from ssh shell?

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!

Running a GTK+ application on a Linux machine, from Windows

You must set the DISPLAY variable in your telnet session to the IP address or DNS name of your Windows machine and append ":0"

export DISPLAY=windows.your.domain:0

And you must start an X server on the Windows machine (comes with Cygwin but you must start it).

How can I use PHP to setup an interactive SSH session?

You should use public key authentication rather than trying to programmatically bypass interactive password authentication.

The password prompt is supposed to be used from a tty and I believe it was made intentionally difficult to use otherwise. Also the -t -t argument only takes effect once you are connected to the remote host. And I don't believe the PHP function proc_open() can run a command inside a virtual terminal.

To setup public key authentication:

# Generate keypair
ssh-keygen -t rsa

# Copy public key to server
scp ~/.ssh/id_rsa.pub example.com:.ssh/authorized_keys

# Now you shouldn't be prompted for a password when connecting to example.com
# from this host and user account.
ssh example.com

# Since the web server (and thus PHP) probably has its own user account...
# Copy the ~/.ssh/id_rsa file somewhere else
cp ~/.ssh/id_rsa /some_path/id_rsa

# Change ownership of the file to the web server account
chown www-data:www-data /some_path/id_rsa

# Fix the file permissions (ssh ignore the keyfile if it is world readable)
chown 600 /some_path/id_rsa

# Try connecting to the server through the web server account
su -c "ssh -i /some_path/id_rsa -o UserKnownHostsFile=/some_path/known_hosts example.com" www-data

# Add the host to the known hosts file when prompted


Alternately, you could use plink (part of PuTTY for Linux) instead of OpenSSH as it can take the password on the command line plink -pw password example.com. But doing so presents a security risk as anyone who runs ps aux on the server can see the password in the process list.

There is also a program called sshpass that takes the password from an environment variable or command argument and passes it to ssh.

How to forward X over SSH to run graphics applications remotely?

X11 forwarding needs to be enabled on both the client side and the server side.

On the client side, the -X (capital X) option to ssh enables X11 forwarding, and you can make this the default (for all connections or for a specific connection) with ForwardX11 yes in ~/.ssh/config.

On the server side, X11Forwarding yes must be specified in /etc/ssh/sshd_config. Note that the default is no forwarding (some distributions turn it on in their default /etc/ssh/sshd_config), and that the user cannot override this setting.

The xauth program must be installed on the server side. If there are any X11 programs there, it's very likely that xauth will be there. In the unlikely case xauth was installed in a nonstandard location, it can be called through ~/.ssh/rc (on the server!).

Note that you do not need to set any environment variables on the server. DISPLAY and XAUTHORITY will automatically be set to their proper values. If you run ssh and DISPLAY is not set, it means ssh is not forwarding the X11 connection.

To confirm that ssh is forwarding X11, check for a line containing Requesting X11 forwarding in the output of ssh -v -X. Note that the server won't reply either way, a security precaution of hiding details from potential attackers.

Transfer files to/from session I'm logged in with PuTTY

This is probably not a direct answer to what you're asking, but when I need to transfer files over a SSH session I use WinSCP, which is an excellent file transfer program over SCP or SFTP. Of course this assumes you're on Windows.

Executing a Ruby method inside a Net::SSH session

Ruby code running inside the net::ssh block still runs on your computer (this includes methods that run commands like system or backticks)

To execute a command on the remote server you need to use session.exec or session.exec! (the latter is blocking, the former requires you to run the ssh event loop). You can also open a channel explicitly and execute a command there - these methods are conscience wrappers.

There is no special support for running ruby remotely. You can of course use exec! to run ruby on the other machine (assuming it is installed) but that's it



Related Topics



Leave a reply



Submit