I want to pass some parameters as part of my ssh connection that I can use to set custom variables for my login to do certain things or run certain scripts. How do you do this running Putty on Windows machine, connecting via SSH to a CentOS machine?
I want to pass some parameters as part of my ssh connection that I can use to set custom variables for my login to do certain things or run certain scripts. How do you do this running Putty on Windows machine, connecting via SSH to a CentOS machine?
On the CentOS machine, create a file in your home directory named
.bashrc
and set your environmental variables in there. For example, the contents of the file can be:Here's some discussion of this: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_01.html
You can also use the
SendEnv
andAcceptEnv
options. This will allow environmental variables on the client to be passed to the server.You will also need to change the server's
sshd_config
file to specify which environmental variables are accepted by the server. I'm not sure what you'll need to do with PuTTY to issue theSendEnv
option, but that should get you started.You can enter environment variables in the PuTTY configuration under
Connection -> Data
.But this works only under certain conditions. Quote from the documentation:
Additionally, this has to be allowed on the server side. For the OpenSSH server the configuration directive is named
AcceptEnv
. On an Ubuntu server it looks like this by default:This allows you to define the variable
LANG
and all variables starting withLC_
in PuTTY, so you can always get the output in your preferred locale.If you want to set additional variables you have to add them to the list on all servers you want to connect to. On older (SSH1 only) hosts it won't work at all.
For those, that cannot modify
sshd
config for various reasons and/or have +2000 servers (and no access to mass-configuration tools or can't/don't want to change settings for other users), here's a solution I came up with:In PuTTY load the desired session, go to Connection > SSH. In the "Data to send to the server" section, in "Remote command" field use:
Example:
I unset the variable first, because it didn't work otherwise.
You could can modify PermitUserEnvironment in sshd.conf to allow processing of ~/.ssh/environment or "environment=" options on keys in ~/.ssh/authorized_keys.
The format of these two files differs. ~/.ssh/environment is lines of VARIABLBE=VALUE where in ~/.ssh/authorized_keys the environment option is environment="VARAIBLE=VALUE"