2

I want to pass enviromental variables to a ssh connection. They should be passed dynamically, therefore hard-coding them into the config file of ssh won't work for me.

I've tried this:

   AAA="tatata" ssh my_user@my_server.com
   echo $AAA
   ====> (empty)

How to do it?

1 Answers1

7

Ssh only passes only the TERM environment variable through (+ LANG and LC_* in many default configs ^1). Changing the server's configuration to accept any environment variables can have serious consequences (allowing a user to bypass their login shell and any ForceCommand), and you should not do it.

Instead of trying to pass them through ssh, you could set them on the right side, as part of the "remote command" argument(s), and start an interactive login shell explicitly:

ssh -t user@host AAA=tatata bash -l

The -t option is to let ssh allocate a pseudo-terminal, something it won't do by default when a remote command is specified. The -l option of bash is to let it run a login shell, i.e. to let it source ~/.bash_login, and simulate as closely as possible the way it's run when no remote command arguments are specified to ssh.

Since the values of the environment variables will be expanded twice, both on the local and on the remote side, you should double quote them, which may turn nasty fast:

ssh -t user@host PRICE=\\\$19 bash -l

But if your configuration allows the LC_* envvars through, you could just name your variables that way ;-)

LC_AAA=tatata ssh user@host

or, if your ssh client config does not include LC_* in SendEnv:

LC_AAA=tatata ssh -o SendEnv=LC_AAA user@host

^1 In order to be able to pass extra envvars through ssh, both AcceptEnv on the server and SendEnv on the client are needed. Look for those directives in ssh*_config to see what their defaults are on your system(s).