Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

marked as duplicate by jtbandes, DavidPostill, fixer1234, Ben N, Andrew Lambert Apr 3 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.

up vote 9 down vote accepted

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.

share|improve this answer
the variable needs to change potentially every ssh call – Ross Rogers Sep 29 '09 at 23:03
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
This answer doesn't really seem to answer the question. – intuited Jun 17 '10 at 19:54

The SendEnv option is your guy.

~/.ssh/config: (locally)


/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.

share|improve this answer
Perfect! I've started using this. – offby1 Aug 4 '15 at 21:05

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"
share|improve this answer
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

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.

share|improve this answer
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 * ProxyCommand -ssh -W %h:%p bastionhost and let it tunnel your connections for you. – Kirk Strauser Sep 26 '12 at 21:32
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
ssh user@host "export $bla && ./runProg"

On bash I tested with:

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

$ scp user@host:~/
$ bla="MyEnvFromSSH=qwert"
$ ssh user@host "export $bla && ./"
share|improve this answer

Not the answer you're looking for? Browse other questions tagged .