解决GitLab中使用SSH的git clone总是提示输入密码且任何密码都不对

  笔者最近在新 Linux 中安装 GitLab 后,发现一个诡异的事情。当配置完管理员账号、SSH 密钥之后、开启防火墙端口号、在 GitLab 新建仓库 test 等等之后,笔者尝试在远程客户端 Windows 上使用 git clone 来 clone 这个在 GitLab 上的仓库,使用的是 SSH 协议。但无论 clone 的 URL 是否正确,终端提示输入密码,且输入任何密码都不对。完整提示信息内容如下:

笔者报错时的运行环境:

  • GitLab:gitlab-ee-14.3.6-ee.0.el8.x86_64

  • GitLab 上的 Linux:CentOS Stream 8 x86_64

  • 远程客户端 Windows:Windows 10 教育版

$ git clone git@xxx.xxx.xxx.xxx:XXX/test.git
Cloning into 'test'...
/c/Users/xxx/.ssh/config line 2: Unsupported option "rsaauthentication"
git@xxx.xxx.xxx.xxx's password:
Permission denied, please try again.
git@xxx.xxx.xxx.xxx's password:
34xxxPermission denied, please try again.
git@xxx.xxx.xxx.xxx's password:
git@xxx.xxx.xxx.xxx: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
fatal: Could not read from remote repository.

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

  但奇怪的是,如果使用 HTTP 协议进行 clone、push 却没有任何问题。笔者在反复地卸载、重装 GitLab,试了各种方法才发现,这个密码其实是 GitLab 所在的 Linux 上的一个账户名为 git 的账户密码。这个账户是怎么来的呢?实际上,在第一次使用命令 gitlab-ctl reconfigure 初始化 GitLab 配置时,GitLab 会自动为 Linux 创建五个账户,分别是:

  • git

  • gitlab-redis

  • gitlab-psql

  • gitlab-prometheus

  • gitlab-www

  因为这些账户在被创建时,GitLab 并没有提示我们输入密码,所以我们并不知道密码,或者它们本来就没有密码。不过,我们在知道账户名的时候就可以强制改密码了。可以使用如下命令来更改账户 git 的密码:

passwd git


  在设置完账户 git 的密码后,笔者已经一切就已经结束了,结果后面还有一个大坑等着。在提示密码后,输入设置的账户 git 的密码后,虽然没有提示密码错误,但 clone 时却报了如下的错误:

git clone does not appear to be a git repository fatal: Could not read from remote repository.

  笔者反复检查过,clone 时使用的 SSH 的 URL 是从 GitLab 项目中复制下来的,URL 没有问题。在反复安装不同的 Linux 与相应的 GitLab 后,这才发现原因。笔者前面安装 GitLab 使用的 Linux 操作系统是 CentOS Stream 8,但截止至笔者编写本博客时,GitLab 并未对 CentOS Stream 提供 GitLab 安装包,于是笔者想当然地以 CentOS 8 安装包来代替,所以在安装后的 GitLab 的 SSH 环境不正确,以至于引发了各种问题。

  当笔者使用 CentOS 8 安装 GitLab 后,在没有主动设置账户 git 的密码时,使用 SSH 协议进行 clone、push 时,不仅连输入密码这个过程都免了,也没有产生任何报错。

成功运行时的环境:

  • GitLab:gitlab-ee-14.3.6-ee.0.el8.x86_64

  • GitLab 上的 Linux:CentOS 8 x86_64

  • 远程客户端 Windows:Windows 10 教育版

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐