tags:

views:

2960

answers:

5

I can't seem to set a new $PATH such that it is used when executing commands via ssh user@host command. I have tried adding export PATH=$PATH:$HOME/new_path to ~/.bashrc and ~/.profile on the remote machine, but executing ssh user@host "echo \$PATH" shows that the change has not been picked up (it shows /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games). The remote machine is running Ubuntu 8.04.

I'm sure I could hack it into /etc/profile, but that's not a clean solution and it only works when one has root access.

A: 

At a guess, you need to add it to ~/.bash_profile, not ~/.profile. Certainly one out of all those should do it, and it's the one that's left.

chaos
A: 

You can always say:

ssh remotemachine 'export PATH=wedontneedastinkingpath; echo $PATH'
Chas. Owens
+3  A: 

Do you have an ~/.bash_login or ~/.bash_profile?

Bash in interactive mode checks for these files, and uses the first existing one, in this order:

  1. ~/.bash_profile
  2. ~/.bash_login
  3. ~/.profile

So if you have an ~/.bash_profile, then whatever changes you do to ~/.profile will be left unseen.

Bash in non-interactive mode reads the file ~/.bashrc (which is also often source'd from the interactive scripts.)

ssh seems to be using the non-interactive mode, so ~/.bashrc should be enough. When having problems like this, I usually add a few echo's to see what files are being run.

grawity
A: 

ssh documentation says:

If command is specified, it is executed on the remote host instead of a login shell.

which is why adding to the bashrc files doesn't work. you do however have the following options:

  1. If the PermitUserEnvironment option is set in the sshd config, you can add your PATH setting to ~/.ssh/environment

  2. ssh remotemachine 'bash -l -c "somecommand"'

Hasturkun
1. It's not set in my sshd config and `man sshd_config` says it's off by default so it's unlikely this solution would work for most people.2. This would work, but I can't easily modify the command sent to ssh (see the second comment on my question).
Denver Gingerich
+7  A: 

As grawity said, ~/.bashrc is what you want, since it is sourced by non-interactive non-login shells.

I expect the problem you're having has to do with the default Ubuntu ~/.bashrc file. It usually starts with something like this:

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

You want to put anything for non-interactive shells before this line.

singpolyma
Yep, I moved the `export PATH=$PATH:$HOME/new_path` above that line and it worked. Thanks!
Denver Gingerich