以前就遇到过这个问题,没深究,今天又碰到了,决定彻底解决掉.

症状:

服务器上的 Ruby 是通过 rvm 安装的,登陆到服务器上执行正常,但跑部署脚本( Ruby ,并非是 Shell )就提示找不到命令. 翻 SSH 的手册看到一句话:

If command is specified, it is executed on the remote host instead of a login shell.

这说明 SSH 获得的 Shell 和 login 获得的 Shell 是有区别的,也就是根本不会载入 bashrc .而 rvm 是依赖 bashrc 的,因此也不会启动.

查看 env | grep rvm 远程和本地返回了截然不同的结果:远程缺少和 rvm 相关的路径,这解释了为什么找不到命令.

解决:

env | grep rvm > ~/.ssh/environment
PermitUserEnvironment yes >> /etc/ssh/sshd_conf
service ssh restart