Automatically set the DISPLAY environment variable in SSH connection

14. January 2012 10:32

 

This is a quick tip if you use an xserver that is running remotly and you want to set the DISPLAY enviroment variable when you login to the machine using ssh. It is actually very simple to do.

 

When you use ssh it will automatically set the SSH_CLIENT enviroment variable to contain the client connection details eg the ip address port number etc.. to get this to work you will need to paste the following into your .bashrc file.

 

 

if [ ! $DISPLAY ] ; then
	if [ "$SSH_CLIENT" ] ; then
		export DISPLAY=`echo $SSH_CLIENT|cut -f1 -d\ `:0.0
	fi
fi

 

 

the above will check that the DISPLAY enviroment variable is not currently set and that the SSH_CLIENT variable is set. It will then extract the ip address from SSH_CLIENT

E-mail Kick it! DZone it! del.icio.us Permalink


Comments (3) -

6/20/2012 11:23:38 PM #

Handy tip!  I've added it to some of my remote dotfiles.

Mike United States |

9/6/2012 3:35:28 PM #

Unfortunately, my SSH_CLIENT was not my display either.
I am searching for a way to forward my old DISPLAY env to the new shell.

Feng Chen United States |

9/6/2012 3:44:09 PM #

"ssh -X remote_ip" works nicely for me, for:
DISPLAY_XWIN --ssh--> machine1 --"ssh -X"--> machine2

Feng Chen United States |