I'd like to access and modify shell environment variables before
executing a command on a remote host via ssh. Unfortenatly, I can't
figure out the proper way to do this...
It seems, standard shell configuration files (e.g. ~/.bash_profile) are
not sourced before ssh executes a command. Of course I could explicitly
source them, e.g.
ssh user@host 'source ~/.bash_profile; command'
But I don't like that because it delegates a server side configuration
task to the clients.
Is there a way to achieve this in a more general fashion on a per user
basis? I tried both ~/.ssh/environment and ~/.ssh/rc, as documented in
the man page. But ~/.ssh/environment only works for static variables and
I didn't manage to export variables defined in ~/.ssh/rc.
For example, if I have these files in my home directory on server
# ~/.ssh/environment
VAR1=$(date)
and
# ~/.ssh/rc
VAR2=$(date)
export VAR2
then executing env from the client via
ssh malte@server env | grep VAR
will result in
VAR1=$(date)
i.e., VAR1 is passed literally while VAR2 is not available.
And where are the variables defined that I see when exexuting
ssh user@server env
Among them are
SHELL=/bin/bash
PWD=/home/malte
Thanks in advance,
Malte
_______________________________________________
openssh-unix-dev mailing list
openssh-...@mindrot.org
https://lists.mindrot.org/mailman/listinfo/openssh-unix-dev
Thanks for the suggestions I received!
Karlan T. Mitchell schrieb:
> ~/.bashrc I believe. Profile is for local terminals.....pretty sure at
> least. Hope this helps
Jim Rees schrieb:
> The bash man page suggests you should be able to do this:
> ~/.ssh/environment
> BASH_ENV=.bash_profile
I extended my test scenario to four configuration files on the server:
# .ssh/environment
VAR1=$(date)
BASH_ENV=~/.ssh/ssh_profile
# .ssh/rc
echo ".ssh/rc"
VAR2=$(date)
export VAR2
# .bashrc
echo ".bashrc"
VAR3=$(date)
export VAR3
# ~/.ssh/ssh_profile
echo ".ssh/ssh_profile"
VAR4=$(date)
export VAR4
Now, when I execute
$ ssh server env | grep -E -e '^\.' -e VAR
I get
.ssh/ssh_profile
.ssh/rc
.bashrc
VAR1=$(date)
VAR3=Mi 2. Dez 18:16:33 CET 2009
So all four files are read or sourced, respectively.
I hadn't noticed that ~/.bashrc is sourced because it starts with
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
which terminates it in the ssh context. But above that test I could
insert a test for some flag set in .ssh/environment and setup the shell
variables I need for ssh. Still, that seems a little awkward as .bashrc
is sourced every time any shell is created with the default
configuration files.
I'm still looking for a more ssh-specific solution. Unfortenately, the
variables exported from the two ssh-specific files .ssh/rc and
.ssh/ssh_profile don't make it into the environment. Why is that?
A secondary problem with using BASH_ENV could be that its value is
evaluated by every non-interactive shell. So, if I use ssh to login to
the server and then run a script with bash, the file specified by
BASH_ENV is sourced. For example, with the above setup:
$ echo "echo foo" > say_foo.sh
$ bash say_foo.sh
.ssh/ssh_profile
foo
Any more suggestions on how to modify environment variables before
executing a command via ssh?
Thanks, Malte
You can use SendEnv in the client configuration. But the server must
also AcceptEnv those same variables.
//Peter
Malte Forkel wrote:
>
> Any more suggestions on how to modify environment variables before
> executing a command via ssh?
>
SSH will set the SSH_CLIENT, SSH_TTY and SSH_CONNECTION
So you can tell if this is an ssh session.
~./.bashrc is always run, and $- on my Ubuntu is hBc
the first time. If its interactive, ~/.bash_profile is then run and
$- is himBH.
Some systems might run /etc/profile as well, so tests are needed
try echo $-
So your .bashrc could test for SSH_CLIENT,and $- for c
and set what it wants. It could also set MY_ENV_HAS_BEEN_SET=1
so you only run you code once at the start of a session.
> Thanks, Malte
>
>
>
>
> _______________________________________________
> openssh-unix-dev mailing list
> openssh-...@mindrot.org
> https://lists.mindrot.org/mailman/listinfo/openssh-unix-dev
>
>
--
Douglas E. Engert <DEEn...@anl.gov>
Argonne National Laboratory
9700 South Cass Avenue
Argonne, Illinois 60439
(630) 252-5444
The command I execute via ssh installs packages into a repository on the
server. The environment variable I'd like to be able to modify is used
to access an instance of gpg-agent on the server. So unfortenately,
SendEnv does not help me in this case because the value of the
environment variable is known/created on the server.
You're right. That way I wouldn't have to set a flag in
.ssh/environment. But I'm still somewhat reluctant to put ssh-specific
code into .bashrc which does not seem to be meant for non-interactive
shells while I don't unterstand why the ssh-specific configuration files
can't export variables.
But I'm still somewhat reluctant to put ssh-specific
code into .bashrc which does not seem to be meant for non-interactive
shells while I don't unterstand why the ssh-specific configuration files
can't export variables.
Ssh has no problem exporting env vars, as you have discovered. Just put
them in .ssh/environment. You are trying to do something bash-specific in
your ssh config file:
VAR1=$(date)
That's not going to work. If you want to run bash code, you have to put it
in a bash config file, not in an ssh config file.