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
SSH_CLIENT= 53816 22

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