When I ssh into a server, how can I pass an environment variable from the client to the server? This environment variable changes between different invocations of ssh so I don't want to overwrite $HOME/.ssh2/environment every time I do an ssh call. How can I do this?
|
feedback
|
|
Of course, you can set the environment variable inside the command, however you'll have to be careful about quoting: remember that your shell is going to parse your local command line, and then the remote shell will have a go on the string it receives. If you want a variable to get the same value on the server that it has on the client, try the ssh -o SendEnv=MYVAR server.example.com mycommand This requires support from the server, though. With OpenSSH, the variable name has to be authorized in If the server only allows certain specific variable names, you can work around that; for example a common setup allows ssh -o SendEnv=LC_MYVAR server.example.com 'MYVAR=$LC_MYVAR; unset LC_MYVAR; export MYVAR; mycommand' If even
env TERM="extra information:$TERM" ssh -t server.example.com 'MYVAR=${TERM%:*}; TERM=${TERM##*:}; export MYVAR; mycommand'
Ingenious! But someone points out to me that you can just define your variable directly in the command: ssh -t server.example.com 'export MYVAR="extra information"; mycommand' Or if passing a local variable: ssh -t server.example.com 'export MYVAR="'$LOCALVAR'"; mycommand' | ||||
|
feedback
|
|
So, on your client, you have some environment variable, and you want that to be available to the remote command? I don't think there's a way to have ssh magically pass it along, but you can probably do something like this. Instead of using, say:
You can do this:
| |||
|
feedback
|
|
You could try invoking a custom command, assuming you have password-less ssh login setup. On the server, edit your ~/.ssh/authorized_keys entry that corresponds to the key from you client:
Look at this link in the section Forced Command for a little more detail. | |||
feedback
|
|
If you can administrate the target host you can configure sshd to allow passing your local environment variables along to the target host. From the sshd_config man page:
sshd configuration typically lives at | |||
|
feedback
|
sshman page, I don't see any way to do that other than setting the variable manually once you've logged in to the server, unless you modify ~/.ssh2/environment. – garyjohn Jul 13 '10 at 17:23