新手问题 gitlab 报错:找不到 ruby 命令。看了 Issue 还是没懂这么解决。。

wcp1231 · December 20, 2013 · Last by wcp1231 replied at December 20, 2013 · 13014 hits

前段时间在服务器上搭建的 Gitlab 不知道为啥昨晚突然出现问题。push 代码的时候报错:

/usr/bin/env: ruby: No such file or directory

登陆一开始发现 rvm 有一个警告: 我按着提示解决后。还是报原来的错误:

$ ssh -T git@localhost
/usr/bin/env: ruby: No such file or directory

Gitlab 在配置的时候新建了一个 git 账户,我在这个账户下试了一下:

$ ssh git@localhost echo $PATH
/home/chunpeng_wen/.rvm/gems/ruby-2.0.0-p247/bin:/home/chunpeng_wen/.rvm/gems/ruby-2.0.0-p247@global/bin:/home/chunpeng_wen/.rvm/rubies/ruby-2.0.0-p247/bin:/home/chunpeng_wen/.rvm/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

$ ssh git@localhost env
MAIL=/var/mail/git
USER=git
SSH_CLIENT=127.0.0.1 54254 22
HOME=/home/git
LOGNAME=git
SSH_ORIGINAL_COMMAND=env
PATH=/usr/bin:/bin:/usr/sbin:/sbin
SHELL=/bin/sh
PWD=/home/git
SSH_CONNECTION=127.0.0.1 54254 127.0.0.1 22

我尝试这样:

sudo ln -s /home/chunpeng_wen/.rvm/rubies/ruby-2.0.0-p247/bin/ruby /usr/bin/ruby

这样之后 ssh 连接好了,但是 http 连接会报错,提示找不到找不到一些 gem。 这应该是虽然 /usr/bin/ruby 文件存在了,但需要的 gem 还是在我自己的 rvm 里。

我在 GitHub 上查到了这个 Issue:gitlab-shell + rvm,我感觉是说 ssh 连接后并没有执行环境变量脚本,所以 bash 没有将 rvm 添加到环境变量里?看完了也不知道怎么解决这个问题。。感觉 Linux 知识欠缺太多了。。 Gitlab 站点运行了一个多月都没问题,就在昨晚突然不行了。。好奇怪。求大神指导如何解决。。

Update

看了一下 gitlab-shell 的源码。在用 ssh 方式进行 push、fetch 等操作的时候。根据/home/git/.ssh/authorized_keys文件的配置会执行/home/git/gitlab-shell/bin/gitlab-shell脚本。这个脚本的第一行就是

#!/usr/bin/env ruby

因为是 ssh 过来的,环境变量里没有 ruby 的路径,因此就报错了。 所以我非常丑陋的把这段改成了我 rvm 里的路径。

结论就是,生产环境中不要使用 rvm....

顶一下。我的问题有两个:

  1. 为啥echo $PATHenv的结果里PATH的值不一样?
  2. 报错的原因是因为ssh到一个disabled-login账户之后,环境变量里没有需要的 rvm 路径吗?

安装完 rvm,这个 source /etc/profile 执行了吗?

env,应该是所有的环境变量。path 不是。

这行,目测对应的脚本一开始的地方有 BOM

/usr/bin/env: ruby: No such file or directory

你看看/home/chunpeng_wen/.rvm/gems/ruby-2.0.0-p247/bin:/home/chunpeng_wen/.rvm/gems/ruby-2.0.0-p247@global/bin:/home/chunpeng_wen/.rvm/rubies/ruby-2.0.0-p247/bin:/home/chunpeng_wen/.rvm/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin:/usr/local/bin:/usr/games:/usr/local/games,是不是在.bash_profile 或.bashrc 里

#2 楼 @tankerwng 用 git 这个用户执行 source /etc/profile?

/etc/profile,是针对所有的用户,存放的环境变量,而.bash_profile或.bashrc,是针对某个用户自己的。希望对你有帮助。

看你的做法,好像是用 chunpeng_wen 执行的安装的吧?

不行的话,手动将/home/chunpeng_wen/.rvm/gems/ruby-2.0.0-p247/bin:/home/chunpeng_wen/.rvm/gems/ruby-2.0.0-p247@global/bin:/home/chunpeng_wen/.rvm/rubies/ruby-2.0.0-p247/bin:/home/chunpeng_wen/.rvm/bin,加到/etc/profile。

https://rvm.io/rvm/install中写道:

  1. Load RVM into your shell sessions as a function

Single-User:

The rvm function will be automatically configured for every user on the system if you install as single user. Read the output of installer to check which files were modified.

Multi-User:

The rvm function will be automatically configured for every user on the system if you install with sudo. This is accomplished by loading /etc/profile.d/rvm.sh on login. Most Linux distributions default to parsing /etc/profile which contains the logic to load all files residing in the /etc/profile.d/ directory. Once you have added the users you want to be able to use RVM to the rvm grou p, those users MUST log out and back in to gain rvm group membership because group memberships are only evaluated by the operating system at initial login time. Zsh not always sources /etc/profile so you might need to add this in /etc/**/zprofile:

source /etc/profile

就是我上边中文解释的意思。

#4 楼 @bhuztez BOM 指的是这个?

#!/usr/bin/env ruby

#9 楼 @tankerwng 在我自己的 .profile 里有:

PATH=$PATH:$HOME/.rvm/bin

在 .bash_profile 里有:

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"

我改了 gitlab-shell 弄好了。待会我试试把配置放到 /etc/profile 里。谢谢~

#4 楼 @bhuztez 大大问一下,echo $PATHenv 的结果不同,何解?或者在哪可以看到资料?

#12 楼 @wcp1231 就是这个文件最开始会有几个字节。你 hexdump -C filename | head 应该能看出来的

#14 楼 @bhuztez gitlab-shell/bin/gitlab-shell 这个脚本看了应该没问题,.ssh/authorized_keys文件也没问题。。

You need to Sign in before reply, if you don't have an account, please Sign up first.