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
?
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.To complete the chain:
Finally, the remote server must have the environment variable listed in
AcceptEnv
in itssshd_config
.You can give a specific value by using
SetEnv
in your~/.ssh/config
, e.g.As per
man ssh_config
:Assuming your server got the following line in
/etc/ssh/sshd_config
:Check also:
man ssh_config
andman sshd_config
.You can set per host config values using .ssh/config file. For example:
Note that server must also support it.
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':
I want to highlight the
SendEnv / AcceptEnv
answer and a different way to trigger it.What's happening here is we're declaring environment variables called
LC_SECRET
andLC_MAGIC
. We've requested to send bothLC_SECRET
andLC_MAGIC
to the remote host usingSendEnv
. The remote host will accept it because it has the following rule in/etc/ssh/sshd_config
:This is, obviously an exploit of the remote system that automatically accepts the
LANG
environment variable or ANY environment variable starting withLC_
.Hence, why I named my variables
LC_SECRET
andLC_MAGIC
.If you want to do it properly, the remote system will require
sudo
access for you to modify/etc/ssh/sshd_config
to append other environment variables.To send a different value of the env variable than the value of the env in the shell: