Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js unavailable via ssh

Tags:

bash

node.js

ssh

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?

like image 938
TDN169 Avatar asked Apr 30 '15 14:04

TDN169


2 Answers

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
like image 162
TDN169 Avatar answered Sep 19 '22 01:09

TDN169


$ 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:

  1. 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.

  2. 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.

like image 41
Kenster Avatar answered Sep 21 '22 01:09

Kenster