42

How can I pass a value into an ssh command, such that the environment that is started on the host machine starts with a certain environment variable set to my choosing?

EDIT: The goal is to pass the current kde desktop ( from dcop kwin KWinInterface currentDesktop ) to the new shell created so that I can pass back an nfs locations to my JEdit instance on the original server which is unique for each KDE desktop. ( Using a mechanism like emacsserver/emacsclient)

The reason multiples ssh instances can be in flight at one time is because when I'm setting up my environment, I'm opening a bunch of different ssh instances to different machines.

marked as duplicate by jtbandes, DavidPostill, fixer1234, Ben N, Andrew Lambert Apr 3 '16 at 4:45

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

17

The ~/.ssh/environment file can be used to set variables you want available for remote commands. You will have to enable PermitUserEnvironment in the sshd configuration.

Variables set this way are exported to child processes, so you can:

echo "Foo=Bar" > sshenv
echo "Joe=37" >> sshenv
scp sshenv user@server:~/.ssh/environment
ssh user@server myscript

and myscript will know that Foo is Bar and Joe is 37.

  • 3
    the variable needs to change potentially every ssh call – Ross Rogers Sep 29 '09 at 23:03
  • 1
    Might be better to describe what you're trying to do and why. There could be other solutions. The environment file would have to be dynamically generated on each ssh call, which isn't impossible. – EmmEff Sep 29 '09 at 23:31
  • What is going to change? The values of those variables or even their names? – innaM Sep 30 '09 at 6:19
  • darn. I tried this solution, but I don't have access the sshd config file and putting vars in ~/.ssh/environment or ~/.ssh2/environment doesn't work. I guess I'm going to use a kludge where I leave this variable on an nfs disk and then snarf it up with my ~/.tcsh setup file. – Ross Rogers Sep 30 '09 at 18:46
  • 2
    This answer doesn't really seem to answer the question. – intuited Jun 17 '10 at 19:54
54

The SendEnv option is your guy.

~/.ssh/config: (locally)

SendEnv MYVAR

/etc/ssh/sshd_config: (on the remote end)

AcceptEnv MYVAR

Now, whatever the value of $MYVAR locally is, it becomes available in the remote session too.
If you login multiple times, each session will have its own copy of $MYVAR, with possibly different values.

~/.ssh/environment is meant for other purposes. It kind of acts as $ENV file when executing non-shell commands remotely.

  • 6
    can also be passed (more usefully) through the command line as ssh myserver -o SendEnv="MYVAR", so you can make it dynamic in scripts. – Mike Campbell Feb 3 '17 at 16:27
30

You can pass values with a command similar to the following:

ssh username@machine VAR=value cmd cmdargs

You can test with:

ssh machine VAR=hello env

On tcsh the following seems to work:

ssh machine "setenv VAR <value>; printenv"
  • Looks like that works well for bash environments. Too bad I'm in a corporate tcsh environment. – Ross Rogers May 21 '12 at 21:45
  • 2
    How can i use the session interactively? – luckydonald Nov 4 '16 at 19:01
  • 1
    Note that the first example only works for the first command if you are chaining commands together (with &&). Using bash, export VAR=value; instead of setenv in the third form works for this case. – contrebis Feb 28 '17 at 14:27
  • 2
    This is what I ended up doing! Wherever you go, take your environment with you. Your can do it like that: ssh user@host "$(<env_to_source.sh) command ..." . In env to source, I have export var=value ; on separate lines (remember the semicolon). – Tomasz Gandor Jan 11 at 14:16
  • @TomaszGandor: years later, still perfect - allows me to even pass complex stuff like PROMPT_COMMAND up there w/o needing to worry about escaping :-) 1000 thanks. – Red Pill Jun 5 at 13:38
29

There's also a horrible, horrible hack.

If your script is consuming the variable on the remote end (i.e. you can name it whatever you want), you can abuse the locale variables. Any variable of the form LC_* will be passed on verbatim, with no requirement for configuration whatsoever.

For example, we have a series of bastion servers at one of my clients. I hate having to connect to it, just to connect to another server... and another server... every time. I have a script that behaves just like SSH, except that it's clever.

Basically, if LC_BOUNCE_HOSTS is set, it splits it on spaces and peels off the first host. Then it bounces through and runs the same script. On the destination node, this list is eventually empty, so it runs the command. I also have a debug mode (which is great during network troubles), which is set by LC_BOUNCE_DEBUG. Since ssh passes all of these along for me magically, I don't have to do anything other than recognize the end of the host list (which I do with a -- option).

I feel dirty every time I use this, but it works everywhere I've tried it.

  • 2
    Why would you use something like that instead of OpenSSH's built-in ProxyCommand option? Edit your ~/.ssh/config and add a block like Host *.example.com: ProxyCommand -ssh -W %h:%p bastionhost and let it tunnel your connections for you. – Kirk Strauser Sep 26 '12 at 21:32
  • 1
    For tunnels, it's not so bad. For env vars, two reasons: One, PermitUserEnvironment requires admin access to configure on the server to pass them directly. Passing them through the command line is also really hard to get the escaping right. Two, multiple bastions must be bounced through, making this a bit more complex -- especially when it's not clear from the source host which path to take to a certain destination host. It's easier to say: "bounce-ssh bast1 bast2 nodeX -- rm -rf /" than to maintain the routes for an evolving population of hosts in a series of ssh-config files. – Jayson Sep 26 '12 at 22:59
  • Good catch !! So wonderful and a little dirty !! – zw963 Dec 14 '16 at 18:21
  • 2
    This is horrible. Bravo. 👏 – Pi Delport Mar 6 '18 at 20:10
  • 1
    This is horribly great, thanks! I've used it for one more terribly nice hack! :) – lumbric May 12 '18 at 11:14
1
bla="MyEnvSelection=dcop"
ssh user@host "export $bla && ./runProg"

On bash I tested with:

$ echo '#!/bin/sh' > readEnv.sh
$ echo 'echo "MyEnv: "$MyEnvFromSSH' >> readEnv.sh

$ scp readEnv.sh user@host:~/
$ bla="MyEnvFromSSH=qwert"
$ ssh user@host "export $bla && ./readEnv.sh"

Not the answer you're looking for? Browse other questions tagged or ask your own question.