SSH XForwarding – How to Run X Applications on Client

opensshsshxforwardingxorg

I have the reverse problem that most people seem to be having. If I start an X application while logged into ssh, it displays on the server(host) machine instead of the client(local). This is the command I use

$ ssh -X -p 6623 pinker@192.168.0.200

My $DISPLAY variable appears correct on the client.

$ echo $DISPLAY
:0

I want X applications from the server to display locally on the client (the machine I'm physically at). I don't know what is causing this.

Best Answer

For the sake of this conversation lets say there are 2 machines named lappy and remotey. The lappy system is where you'd be running your ssh commands from. The system you're connecting to is remotey.

1. Display GUIs from remotey on lappy

        lappy               .-,(  ),-.    
           __  _         .-(          )-.          remotey 
          [__]|=|  ---->(    network     )------> ____   __ 
          /::/|_|        '-(          ).-'       |    | |==|
                             '-.( ).-'           |____| |  |
                                                 /::::/ |__|

      NOTE: on lappy, `ssh` to remotey, run GUI, see GUI on lappy

Your shell's configuration files are likely setting the environment variable DISPLAY=:0. You can grep for this like so:

 $ grep DISPLAY $HOME/{.bash*,.profile*}

If that doesn't return anything back then the system you're logging into is probably the culprit. Take a peek in this directory as well.

 $ grep DISPLAY /etc/profile.d/* /etc/bash*

If you'd rather just leave this be you can override this behavior by instructing ssh to redirect X traffic back to your client system, like so:

$ ssh -X user@remoteserver

Example

Here I have a remote server that has $DISPLAY getting set to :0 similar to yours.

$ ssh -X skinner "echo $DISPLAY"
:0

But no matter, I can still invoke X applications and have them remote displayed to my system that's doing the ssh commands. I don't even have to login, I can simply run GUI's directly like so:

$ ssh -X skinner xeyes

As a bonus tip you'll probably want to change which ciphers are being used, to help improve the performance of your X11 traffic as it passes over your SSH tunnel.

$ ssh -c arcfour,blowfish-cbc -X skinner xeyes

2. Displaying GUIs on remotey

        lappy               .-,(  ),-.    
           __  _         .-(          )-.          remotey 
          [__]|=|  ---->(    network     )------> ____   __ 
          /::/|_|        '-(          ).-'       |    | |==|
                             '-.( ).-'           |____| |  |
                                                 /::::/ |__|

      NOTE: on lappy, `ssh` to remotey, run GUI, see GUI on remotey

If you're SSH'ing into remotey from lappy but would like to keep the GUIs from being displayed on lappy, then simply drop the -X switch from your ssh invoke.

$ ssh -p 6623 pinker@192.168.0.200

3. Eliminating $HOME/.ssh/config

Often times a user's $HOME/.ssh directory can introduce unknowns as to what's going on. You can temporarily silence the use of the config file in this directory like so when performing tests.

$ ssh -F /dev/null -p 6623 pinker@192.168.0.200

4. Eliminating remote shell's configs

You can use the following test to temporarily disable the shell's configuration files on remotey like so:

$ ssh -t -X -p 6623 pinker@192.168.0.200 "bash --norc --noprofile"

With the above, none of the setup should be getting sourced into this Bash shell, so you should be able to either set DISPLAY=:0 and then display GUIs to remotey's desktop.

You can use the following trick to help isolate the issue, by first removing --noprofile and trying this command:

$ ssh -t -X -p 6623 pinker@192.168.0.200 "bash --norc"

Then followed by this command:

$ ssh -t -X -p 6623 pinker@192.168.0.200 "bash --noprofile"

The first version will tell you if the problem lies in your /etc/bashrc & $HOME/.bashrc chain of configuration files, while the second version will tell you if the problem lies in the $HOME/.bash_profile configuration file.

Related Question