Friday, September 16, 2011

Setting up the environment in a non-interactive SSH session (Ubuntu)


You are using SSH to run commands on a remote Ubuntu host, like
$ ssh remotehost command args

and require that the remote host set up some environment (set PATH, source a script, etc.)

The remote host ignores .bashrc, .bash_profile, .ssh/environment, /etc/profile, etc.


At the top of the default .bashrc in Ubuntu, you will find the lines
# If not running interactively, don't do anything
[ -z "$PS1" ] && return

SSH does not execute an interactive shell when called with a command argument, so .bashrc bails immediately. Comment out the second line:
# If not running interactively, don't do anything
# [ -z "$PS1" ] && return

and set up your environment in .bashrc as usual.


bash does not expand $s inside quotes, but for some reason SSH does. running
ssh remotehost "echo $PATH"

will fill in the path on the current machine and tell the remote machine to echo that, so it appears that your current PATH has been forwarded to the remote host. Use
ssh remotehost echo \$PATH


No comments:

Post a Comment