I have a user that has made no modifications to the $PATH in any dot-files: it is exactly the system default setting. From a login shell:

$ ssh example.com
user@example.com:~$ cat /tmp/hello.hs
#!/bin/bash

echo "$SHELL"
echo "$PATH"

user@example.com:~$ /tmp/hello.hs
/bin/bash
/usr/local/bin:/usr/bin:/bin

Exactly as specified in /etc/profile. This I find rather unexpected:

$ ssh example.com '/tmp/hello.sh'
/bin/bash       
/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games

Like I said, there's no modification of $PATH in ~/.bashrc, nor in /etc/bash.bashrc. No ~/.ssh/environment either. The ssh(1) declares that the environment variable PATH is

Set to the default PATH, as specified when compiling ssh.

but this thread from StackOverflow and this mailing list article suggest that I should be able to influence the $PATH for a given command simply by modifying /etc/profile, one of the shell startup files, etc.

What's going on here?

link|improve this question

feedback

2 Answers

up vote 2 down vote accepted

From ssh(1) manual page: "If command is specified, it is executed on the remote host instead of a login shell."

So in short when you actually login to the machine bash is started as a login shell and loads the apropriate files, when you connect remotely and issue a command it is run in the place of bash, which means that these files do NOT load. You can work around it with using su -l -c or similar in the command part of ssh.

In some cases I've seen -t argument to ssh work (allocate tty) too.

Edit 1:
I think the PATH information you found, that the default path (unless we override it) is the one compiled into sshd. I made sure that my /etc/profile, /etc/bash*, local dotfiles, etc. didn't have any PATH information in it, then I logged on and still had a PATH. I searched for this one in sshd and found it there. So its how the manpage says:

ahnberg@remote$ strings /usr/sbin/sshd | grep -i x11 | grep bin
/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games

Then I add PATH=$PATH:/my/test to the very top of my .bashrc file on remote, and check it again:

ahnberg@local$ ssh ahnberg@remote "env | grep PATH"
PATH=/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/usr/games:/my/test

So I can absolutely influence it, and default PATH is the one compiled into sshd. :)

link|improve this answer
Hmm, that phrase "executed on the remote host" means a lot more than it says, I think. The more interesting bit, which I missed earlier, comes in the 'ENVIRONMENT' section of the same manpage: "PATH Set to the default PATH, as specified when compiling ssh." Except this suggests that I should be able to influence the PATH of a command. – troutwine Jan 20 at 2:02
Well the point is that its not a login shell so it doesn't run/source/include the startup files the same way as for a login shell, hence my suggestions to try out. Putting things in .bashrc might work too, but overall I'd work around it if PATH is important. Or why not just specify full pathnames if you have a need for the 'command' way of running ssh? :) – Mattias Ahnberg Jan 20 at 2:12
I've edited my post slightly. Now, there's a login shell, a non-login shell and interactive/non-interactive variants thereof. SSH commands are invoked in the user's shell in non-interactive non-login form. The bash(1) INVOCATION suggests that no startup files are read in this fashion, but I can't find documentation on how ssh is invoking the shell. This seems counter to the linked sources above, unless others have /etc/ssh/sshrc startup file sourcing that I don't have. (There are workarounds, of course, but the point is understanding exactly how Debian SSHD handles paths by default.) – troutwine Jan 20 at 2:30
If I modify PATH in /etc/profile on my remote box path updates for me, so ssh user@remotebox 'env' shows me the updated PATH. The same thing goes if I add export PATH=$PATH:/my/testpath to .bashrc (but in my case in top of the file before checks for interactive shells (-z "$PS1"). – Mattias Ahnberg Jan 20 at 2:50
Updated with my tests/findings. – Mattias Ahnberg Jan 20 at 2:58
show 1 more comment
feedback

I was able to get ssh to run commands using the remote path by running:

ssh dist@d6 "bash --login -c 'env'"

Here env can be replaced with what ever command you'd like.

I have authorized keys so didn't need a password to run the command or ssh.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.