I am trying to call an installation of node.js on a remote server running Ubuntu via SSH. Node has been installed via nvm.
SSHing in and calling node works just fine:
user@localmachine:~$ ssh user@remoteserver
(Server welcome text)
user@remoteserver:~$ which node
/home/user/.nvm/v0.10.00/bin/node
However if I combine it into one line:
user@localmachine:~$ ssh user@remoteserver "which ls"
/bin/ls
user@localmachine:~$ ssh user@remoteserver "which node"
No sign of node, so I tried sourcing .bashrc and waiting 10 seconds:
user@localmachine:~$ ssh user@remoteserver "source ~/.bashrc; sleep 10; which node"
Only node seems affected by this. One thing I did notice was that if I ssh in and then check which shell I'm in it says -bash
whilst if I ssh direct it gives me /bin/bash
. I tried running the commands inside a bash login shell:
user@localmachine:~$ ssh user@remoteserver 'bash --login -c "which node"'
Still nothing.
Basically my question is: Why isn't bash finding my node.js installation when I call it non-interactively from SSH?
Another approach is to run bash in interactive mode with the -i
flag:
user@localmachine:~$ ssh user@remoteserver "bash -i -c 'which node'"
/home/user/.nvm/v0.10.00/bin/node
$ ssh user@remoteserver "which node"
When you run ssh
and specify a command to be run on the remote system, ssh by default doesn't allocate a PTY (pseudo-TTY) for the session. Not having a TTY causes your remote shell process (ie, bash) to initialize as a non-interactive session instead of an interactive session. This can alter how it interprets your initialization files--.bashrc, .bash_profile, and so on.
The actual problem is probably that the line which adds /home/user/.nvm/v0.10.00/bin
to your command PATH isn't executing for non-interactive sessions. There are two ways to resolve this:
Find the command in your initialization file(s) which adds /home/user/.nvm/v0.10.00/bin
to your command path, figure out why it's not running for non-interactive sessions, and correct it.
Run ssh
with the -t
option. This tells it to allocate a PTY for the remote session. Or add the line RequestTTY yes
to your .ssh/config
file on the local host.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With