When I use rsync to copy files from one computer to another, an rsync process is started on both ends. However, on the remote end, shell initialization files are apparently not read, so I cannot set up environment variables for the remote rsync process. Unfortunately, there is one server that I need to rsync to on which rsync requires an environment variable in order to work. I am not an admin on the server, so I can't change the global configuration. What can I do to set environment varaibles for a remote rsync process?
How to set environment variables for a remote rsync process
environment-variablesremote-connectionrsyncssh
Best Answer
~/.profile
is typically not read when you runssh somecommand
, as opposed to an interactive ssh session (or other login method where you start an interactive session).Ssh supports sending environment variables. In OpenSSH, use the
SendEnv
directive in~/.ssh/config
. However the specific environment variable must be enabled with anAcceptEnv
directive in the server configuration, so this may well not work out for you.OpenSSH also allows setting environment variables on the server side. Again, this must be enabled in the server configuration, here with the
PermitUserEnvironment
directive. The variables can be set in the file~/.ssh/environment
. Assuming you use public key authentication, you can also set per-key variables in~/.ssh/authorized_keys
: addenvironment="FOO=bar"
at the beginning of the relevant line.One thing that I think always works (oddly enough) as long as you're using public key authentication is to (ab)use the
command=
option in theauthorized_keys
file. A key with acommand
option is good only for running the specified command; but the command in theauthorized_keys
file runs with the environment variableSSH_ORIGINAL_COMMAND
set to the command the user specified (empty for interactive sessions). So you can use something like this in~/.ssh/authorized_keys
(of course, it won't apply if you don't use this key to authenticate):Note that I put line breaks above for legibility, but this actually needs to be all on one line.
Another possibility is to write a wrapper script
~/bin/rsync-wrapper
on the server, something likeThen pass
--rsync-path='bin/rsync-wrapper'
on thersync
command line. The argument to--rsync-path
is expanded by a shell, so if you prefer you can make the rsync command line self-contained by passing something like--rsync-path='. ~/.profile; rsync'
.There is another avenue which depends on your login shell being bash or zsh. Bash always reads
~/.bashrc
when it's invoked by rshd or sshd, even if it's not interactive (but not if it's called assh
). Zsh always reads~/.zshenv
.