Command is not found using `ssh root@host command`
Assume there are host A and host B and A can ssh B without password.
When I run ssh root@B ls from A's shell, the ls is executed and output is showing.
However, when I run something like ssh root@B yarn from A's shell, bash: yarn: command not found is showing.
I'm sure that yarn is available by manually input yarn at B's shell.
I guess it's about permission issue. ls and yarn may have different execution level.
So what's the exact reason of it and how can I make ssh root@B yarn available from A's shell.
1 Answer
Your yarn command is in some non-standard location that was added to $PATH through the shell's initialization scripts (e.g. ~/.bashrc or ~/.*profile or /etc/profile.d).
The problem is that Bash often completely ignores these scripts when being run in non-interactive mode. If you compare printenv PATH entered interactively with ssh root@B printenv PATH started non-interactively, you'll probably see different values.
Fixing this depends on the remote server's OS:
ssh root@B ". /etc/profile && yarn ..."should always work (assuming /etc/profile is where PATH is getting set; you'll need to replace it with the correct filename otherwise).Linking the
yarncommand to one of the standard $PATH locations (e.g. /usr/bin or often /usr/local/bin) should always work – assuming it doesn't require other environment variables to be present.Creating a shellscript at
/usr/[local/]bin/yarnwhich sets all required environment variables and runs the real thing should always work.Setting $PATH through ~/.bashrc or /etc/bash.bashrc will work on Debian/Ubuntu, but won't work on most other distributions (unfortunately Bash offers this as a compile-time option).
Setting $PATH through ~/.pam_environment or /etc/environment will work on some distributions. Note that these are not shell scripts and use a different syntax.