3

I'm running this command from my local machine:

ssh -tt -i "pem.pem" ec2-user@ec2-IPADDRESS.compute-1.amazonaws.com "sudo su -c 'cd /dir/;npm install pm2'"

It connects, operates as a super users, cds to dir and attempts to run the command but returns that npm is not a command recognized by the system.

However, when I connect "manually" i.e.

ssh -i "pem.pem" ec2-user@ec2-IPADDRESS.compute-1.amazonaws.com
sudo su
cd /dir
npm install pm2

it works.

npm is installed under root and the system can see it.

ssh -tt -i "pem.pem" ec2-user@ec2-IPADDRESS.compute-1.amazonaws.com "sudo su -c 'cd /dir/;whoami'"

and

ssh -i "pem.pem" ec2-user@ec2-IPADDRESS.compute-1.amazonaws.com
sudo su
cd /dir
whoami

both return "root"

Why can't the npm command be found when running on top of an ssh?

Paul Sender
  • 376
  • 2
  • 19

1 Answers1

2

When you login, you create an interactive shell, which typically will read a couple of files, including /etc/profile, $HOME/.profile, and $HOME/.bashrc in the case of .

Any of these files can add extra elements (paths) to the PATH variable, which affects which commands can be found.

When you run the command line directly, no such initialisation takes place, and the value of $PATH may be limited to just /bin:/usr/bin.

Next there is sudo, which may or may not import the value of PATH when looking for commands.

Solution

Best you can do is find out where npm is installed, and use its full PATH.

Henk Langeveld
  • 8,088
  • 1
  • 43
  • 57