Nov 20 2008

Environment Variables and SSH, How to

Published by at 11:58 pm under SSH

Very few environment variables are defined when connecting to a remote host with an authorized SSH key. The env command that will list all environment variables available on the remote server shows this is the case (localhost is the target here).
 

SSH Environment Variables


Sometimes, you’d like to pass extra environment variables onto the host along the SSH command. For instance, to run a program calling libs from an unusual location, the LD_LIBRARY_PATH variable has to be set.


If You Have root Access on the Remote Host

This can be achieved in 2 simple steps:

– Add ‘PermitUserEnvironment yes‘ to the sshd_config file on the remote server and restart the SSH daemon.
PermitUserEnvironment default value is ‘No’ most of the time.

$ ssh localhost 'sudo sshd -T' | grep -i environment
permituserenvironment no


– Set environment variables in .ssh/environment in your home directory on the target host in the variable=value format.
eg LD_LIBRARY_PATH=/lib:/usr/lib:/usr/local/lib


Environment Variable as Part of the SSH Command

If changing the sshd configuration on the remote host is not an option, you can always pass the environment variable along the SSH command. It is less elegant, especially in the previous use case since the path depends on the path libraries have been installed in, on that specific host. The escaping might be tricky, but this solution does the job as well.

$ ssh localhost LD_LIBRARY_PATH=/lib:/usr/lib:/usr/local/lib env
LD_LIBRARY_PATH=/lib:/usr/lib:/usr/local/lib
SHELL=/bin/bash
SSH_CLIENT=127.0.0.1 53816 22
USER=dave
MAIL=/var/mail/dave
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
PWD=/home/dave
LANG=en_US.UTF-8
SHLVL=1
HOME=/home/dave
LOGNAME=dave
SSH_CONNECTION=127.0.0.1 53816 127.0.0.1 22
_=/usr/bin/env



One response so far

One Response to “Environment Variables and SSH, How to”

  1. Lakshmipathion 15 Mar 2010 at 2:19 pm

    Thanks,I was search for this piece of info 🙂

Comments RSS

Leave a Reply