85

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.

1
  • 1
    Apparently not for your specific question, but for passing environment variables to an interactive SSH session without depending on SSH server configuration (e.g. AcceptEnv) you can do ssh -t user@host FOO=var bash -l. Jul 10, 2020 at 18:08

5 Answers 5

45

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.

7
  • 3
    the variable needs to change potentially every ssh call Sep 29, 2009 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, 2009 at 23:31
  • What is going to change? The values of those variables or even their names?
    – innaM
    Sep 30, 2009 at 6:19
  • 3
    This answer doesn't really seem to answer the question.
    – intuited
    Jun 17, 2010 at 19:54
  • 2
    This will break if two processes are trying to do this with different sets of values concurrently
    – nafg
    Mar 28, 2019 at 3:47
116

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.

2
  • 17
    can also be passed (more usefully) through the command line as ssh myserver -o SendEnv="MYVAR", so you can make it dynamic in scripts. Feb 3, 2017 at 16:27
  • 1
    Note that this only works for variables allowed through the AcceptEnv server option. This is often set to just LANG and LC_* so the suggested solution may not work without also changing the server config. Nov 5, 2023 at 21:06
61

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"
5
  • 1
    Looks like that works well for bash environments. Too bad I'm in a corporate tcsh environment. May 21, 2012 at 21:45
  • 4
    How can i use the session interactively? Nov 4, 2016 at 19:01
  • 2
    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, 2017 at 14:27
  • 3
    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). Jan 11, 2019 at 14:16
  • 1
    @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, 2019 at 13:38
39

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.

7
  • 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. Sep 26, 2012 at 21:32
  • 3
    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, 2012 at 22:59
  • 4
    This is horrible. Bravo. 👏
    – Pi Delport
    Mar 6, 2018 at 20:10
  • 1
    This is horribly great, thanks! I've used it for one more terribly nice hack! :)
    – lumbric
    May 12, 2018 at 11:14
  • 5
    This should no longer work at least since OpenSSH version 5.3p1, because new server config only accept specific LC_xxx variables (but not AcceptEnv LC_*) Mar 19, 2019 at 10:47
3
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 .