How can I set environment variables when I ssh login to my Unix box by passing custom arguments?

38,685

Solution 1

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:

export VARIABLE=foo

Here's some discussion of this: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_03_01.html


You can also use the SendEnv and AcceptEnv 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 the SendEnv option, but that should get you started.

Solution 2

You can enter environment variables in the PuTTY configuration under Connection -> Data.

enter image description here

But this works only under certain conditions. Quote from the documentation:

The Telnet protocol provides a means for the client to pass environment variables to the server. [...]

Version 2 of the SSH protocol also provides a similar mechanism, which is easier to implement without security flaws. Newer SSH-2 servers are more likely to support it than older ones.

This configuration data is not used in the SSH-1, rlogin or raw protocols.

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:

AcceptEnv LANG LC_*

This allows you to define the variable LANG and all variables starting with LC_ 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.

Solution 3

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:

enter image description here

In PuTTY load the desired session, go to Connection > SSH. In the "Data to send to the server" section, in "Remote command" field use:

env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...] bash

Example:

env -u PS1 PS1="[\u@\h]\\$ " bash

I unset the variable first, because it didn't work otherwise.

Solution 4

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"

Share:
38,685

Related videos on Youtube

qodeninja
Author by

qodeninja

I write qode mostly for myself... out of curiosity for solving problems, understanding how things work or making (sometimes unnecessarily) complex systems to only simplify them later (once I discover alternative strategies). For whatever reason, I like torturing myself with Regular Expressions, SED, Bash and JavaScript (Node), but have found a growing (painful) love with Python. Having said that, I enjoy scripting languages a lot more than compiled languages, and I've coded in almost all of the major modern ones except Ruby. I'm a secret Turing Machine/Computer Grammars/Regular Expressions nerd, and have written my own mini compilers and toy languages. I'm constantly writing command dispatchers that I later write scripting languages for; it's an addiction. There's plenty room for me to grow and learn still; and I appreciate the wisdom of grey beards and lady wizards even if I don't always follow their sage advice. FOSS is hella cool; cool projects are cool. Find me online if you have ideas. I'm a really bad programmer but I'll write a line or two for the betterization of the peoples. Edit: I recently discoved that VI is really just SED with wings. Still not using VI. Nano or bust.

Updated on September 18, 2022

Comments

  • qodeninja
    qodeninja almost 2 years

    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?

  • qodeninja
    qodeninja almost 13 years
    Yeah I know how to do that, but how can I pass variables over as part of my connection?
  • cjc
    cjc almost 13 years
    Edited to include the SendEnv and AcceptEnv suggestion.
  • Phill Healey
    Phill Healey almost 6 years
    You can add variables via the Data tab as @Gerald Schneider shows in his answer.
  • yahol
    yahol almost 6 years
    Not in my scenario. I've tried all obvious options in PuTTY first, before coming up with this hack. Please read carefully first 7 words of my answer. Gerald's answer also mentions this: this has to be allowed on the server side.
  • SebMa
    SebMa about 5 years
    @yahol Your solution somehow deprived me from having a terminal, vim now says : Vim: Warning: Output is not to a terminal
  • yahol
    yahol about 5 years
    @SebMa what's the string you're using in "Remote command" in putty?
  • SebMa
    SebMa about 5 years
    @yahol My command was env debug=1 bash and tty shows nothing
  • papo
    papo about 4 years
    this works, @SebMa try sh or /bin/bash instead of bash. Or whatever your shell is there.
  • Mahn
    Mahn over 3 years
    AcceptEnv is located by default in: /etc/ssh/sshd_config Don't forget to restart the ssh service (service ssh restart) to apply the changes afterwards.