15

In ssh_config, one can choose to export some environment variables to the host using SendEnv.

Is there also a way to force a given value for this variable, per host? For example, would it be possible to export variable $FOO with value bar only when connecting to host example.com?

14

You can't give a specific value for an environment variable in ssh_config, but you can certainly send the existing environment variable only to specific hosts.

Host example.com
    SendEnv FOO

To complete the chain:

FOO=bar ssh user@example.com

Finally, the remote server must have the environment variable listed in AcceptEnv in its sshd_config.

AcceptEnv FOO
  • WHAT is precisely what you'd like to avoid? – Michael Hampton Feb 27 '13 at 7:32
  • 1
    Having to write an alias in order to send a variable with a given value. – ℝaphink Feb 27 '13 at 7:35
  • Sorry, not much else you can do... unless you can get the computer to read your mind. :) – Michael Hampton Feb 27 '13 at 7:38
  • 6
    Thanks for confirming my suspicions though ;-) What I'd have liked would been something like SetVar FOO='bar' in ssh_config ;) – ℝaphink Feb 27 '13 at 7:38
  • 2
    The recurring case for me is that old systems don't support TERM=tmux-256color so I'd like a fallback per remote host (with wildcards). – Jed Jun 11 at 15:01
3

You can give a specific value by using SetEnv in your ~/.ssh/config, e.g.

Host *
  SetEnv FOO=bar

Assuming your server got the following line in /etc/ssh/sshd_config:

AcceptEnv LANG LC_* FOO

Check also: man ssh_config and man sshd_config.

  • 1
    Note for the puzzled reader (like myself, when I first read this): This is new since OpenSSH 7.8, released in August of 2018, and it will take a while for it to become available to the average user through OS updates. (But once it does become common, it will be by far the best solution to the problem) – Irfy Jul 16 at 9:37
1

Another idea is to set the environment variables in the ssh command and run an interactive shell, eg. i'm trying to invoke an interactive shell with env-var 'MANWIDTH':

ssh example.com 'MANWIDTH=120 bash -li'
0

You can set per host config values using .ssh/config file. For example:

Host example.com
SendEnv FOO

Note that server must also support it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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