提醒

普通的ssh不能访问,请先检查gitlab-docker的端口开放和主机映射是否正确,以及git url是否正确(包括端口号)

前言

之前在阿里云学生机上,搞了个gitlab服务器,因为主机上已经有git了,而且环境已经弄的比较乱了,不好再直接装gitlab,只好整到docker容器里面运行。

gitlab部署docker容器很方便,切换http和ssh的端口映射就可以使用,但是我弄完很长一段时间里,ssh访问gitlab里的仓库都是不行,基本原因输出就是下面的内容,找不到仓库:

fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

之前因为不怎么用gitlab,所以也没管它。但是最近在用gitlab,想起来我自己还有一个gitlab服务器,自己测试的时候,每次都没有ssh就感觉很不爽,于是上网各种搜,类似问题很多但资料都不管用,从gitlab配置到ssh配置到docker配置甚至github上提的一些相关bug-issue,都没用。

本来都准备放弃的。

一、原因

燃鹅,天无绝人之路,在我最后一次检查配置准备退出放弃的时候,余光扫到了gitlab的logs目录… …愣住,然后果断打开sshd日志文件(logs/sshd/current文件,看自己的docker目录挂载点,没挂载的话要访问docker容器内文件):

> @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
> @         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
> @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
> Permissions 0777 for '/etc/gitlab/ssh_host_xxxx_key' are too open.
> It is required that your private key files are NOT accessible by others.
> This private key will be ignored.
> key_load_private: bad permissions
> Could not load host key: /etc/gitlab/ssh_host_xxxx_key

然后再愣住,然后恍然大悟:搞了半天原来是ssh每次加载服务器秘钥时,发现私钥文件过于不安全而拒绝使用,直接忽略了,导致每次ssh访问都会出错==

二、解决

  1. 先检查gitlab的配置目录(看自己的docker目录挂载点,没有的话要进docker容器操作):

    > -rwxrwxrwx 1 root root 111987 May 17 11:57 gitlab.rb
    > -rw------- 1 root root  18888 May 17 13:48 gitlab-secrets.json
    > -rwxrwxrwx 1 root root    227 Sep 30  2020 ssh_host_ecdsa_key
    > -rwxrwxrwx 1 root root    179 Sep 30  2020 ssh_host_ecdsa_key.pub
    > -rwxrwxrwx 1 root root    411 Sep 30  2020 ssh_host_ed25519_key
    > -rwxrwxrwx 1 root root     99 Sep 30  2020 ssh_host_ed25519_key.pub
    > -rwxrwxrwx 1 root root   1679 Sep 30  2020 ssh_host_rsa_key
    > -rwxrwxrwx 1 root root    399 Sep 30  2020 ssh_host_rsa_key.pub
    > drwxr-xr-x 2 root root   4096 Sep 30  2020 trusted-certs
    
    
  2. ssh秘钥有3种加密算法,分别对应私钥文件ssh_host_ecdsa_keyssh_host_ed25519_keyssh_host_rsa_key。我们ssh一般常用的是rsa算法,如果发现上面私钥文件的权限是有问题的,那么说明你的情况跟我的是一样的。

  3. 发现上述情况,果断修改权限:

    chmod 700 ssh_host_ecdsa_key # 权限设置为只有拥有者能访问,其他组员和其他用户不能访问
    chmod 700 ssh_host_ed25519_key
    chmod 700 ssh_host_rsa_key
    

三、皆大欢喜

修改完权限,直接再重新git访问ssh:ssh://git@xxxipxxx:port/profile/repo.git,接受远端服务器验证,一气呵成、一路畅通,再也不用clone、pull、push输入密码了。
回头再查看一下ssh日志:

2021-05-17_06:23:21.86719 Accepted publickey for git from xxipxx port 19304 ssh2: RSA SHA256:xxxhashxx
2021-05-17_06:23:22.89831 Received disconnect from xxipxx port 19304:11: disconnected by user
2021-05-17_06:23:22.89834 Disconnected from xxipxx port 19304

可以看到,刚才的一次ssh访问流程一切正常,Over

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐