# exporting a variable locally on ssh connection. [solved]

## DaggyStyle

Hello,

I have a ssh server in which I want to export a variable (lets say FOO) right after the connection (before any other scripts) so when running "export | grep FOO" I'll see the variable.

I don't want to place it in .bashrc and I need it for every connection.

I've tried using both /etc/ssh/sshrc and /etc/ssh/environment but it didn't worked.

any ideas?

Thanks.

----------

## rgawenda

You need the server to set the variable for incoming ssh shells only. Right?

----------

## DaggyStyle

 *rgawenda wrote:*   

> You need the server to set the variable for incoming ssh shells only. Right?

 

yes, it is a var which a script that I run upon login should act in accordance to it's value.

----------

## rgawenda

What's the reason you don't want it in .bashrc or /etc/bash/bashrc?

Would /etc/profile / .profile serve your purposes?

I've put there the following code to test, and it worked flawlessly

```
[[ -n "$SSH_CONNECTION" ]] && export TEST=1
```

----------

## DaggyStyle

 *rgawenda wrote:*   

> What's the reason you don't want it in .bashrc or /etc/bash/bashrc?
> 
> Would /etc/profile / .profile serve your purposes?
> 
> I've put there the following code to test, and it worked flawlessly
> ...

 

I'd rather do that only with ssh connection but it seem that your solution may help.

thanks.

----------

## rgawenda

 *DaggyStyle wrote:*   

>  *rgawenda wrote:*   What's the reason you don't want it in .bashrc or /etc/bash/bashrc?
> 
> Would /etc/profile / .profile serve your purposes?
> 
> I've put there the following code to test, and it worked flawlessly
> ...

 

The test [[ -n "$SSH_CONNECTION" ]] does exactly that. No value or exported TEST var if the session is not from an incoming SSH

----------

## DaggyStyle

 *rgawenda wrote:*   

>  *DaggyStyle wrote:*    *rgawenda wrote:*   What's the reason you don't want it in .bashrc or /etc/bash/bashrc?
> 
> Would /etc/profile / .profile serve your purposes?
> 
> I've put there the following code to test, and it worked flawlessly
> ...

 

ok, now I just need to test it in ssh connection.

----------

## DaggyStyle

well, not good, when I run a ssh with command, it doesn't works as the is no bash session open.

even running with bash -x doesn't work.

----------

## rgawenda

Just tried putting the above code in ~./.bashrc and it worked:

```
ais@aisl ~ $ ssh rex env

A=1

SHELL=/bin/bash

SSH_CLIENT=10.68.32.35 34270 7922

[...]

SSH_CONNECTION=10.68.32.35 34270 10.68.30.32 7922

_=/usr/bin/env

ais@aisl ~ $
```

```
ais@aisl ~ $ ssh rex cat .bashrc

#!/bin/bash

[[ -n "$SSH_CONNECTION" ]] && export A=1

ais@aisl ~ $
```

----------

## wcg

I think the OP is saying that he is not necessarily running a login shell like bash,

tcsh, ksh, etc when the ssh session logs in on the remote machine, hence standard

login environments and per user login shell configuration files are not sourced.

man sshd_config, look up PermitUserEnvironment. I do not see that there is any

other mechanism for configuring a pre-login-shell ssh session environment.

(If you are an experienced C hacker, you could look at the openssh source,

of course, follow the process of a login from sshd's point of view, and see what

all happens, with and without PermitUserEnvironment.)

----------

## DaggyStyle

 *wcg wrote:*   

> I think the OP is saying that he is not necessarily running a login shell like bash,
> 
> tcsh, ksh, etc when the ssh session logs in on the remote machine, hence standard
> 
> login environments and per user login shell configuration files are not sourced.
> ...

 

I think I remember that using /etc/ssh/sshrc did worked for me when it comes to a echo to file but not when it comes to export vars.

I think tat I've read that /etc/ssh/environment should work too but it didn't worked too.

I think that I'll use the sshrc to write that data into the actual script.

----------

## DaggyStyle

/etc/ssh/environment didn't worked, so I've took your advice partially and I'm setting the var within the script depending on the content of SS_CONNECTION

----------

