提问者:小点点

在Windows Git bash中工作时,gitlab的Git克隆在linux上失败


我是Linux新手,刚刚安装了Lubuntu并面临这个问题-当我试图从公司的git克隆我的远程工作存储库时:

$ sudo git clone https://path/to/repo.git

我不断收到错误:

Cloning into 'repo'...
fatal: unable to access 'https://path/to/repo.git/': server certificate verification failed. CAfile: none CRLfile: none

我知道它提到了证书,但我没有任何证书。以前,我在 Windows 上工作,并且能够在没有任何证书的情况下简单地 git 克隆这个存储库。


共2个答案

匿名用户

该错误意味着git客户端无法验证证书链或根的完整性。解决此问题的正确方法是确保来自远程存储库的证书有效,然后将其添加到客户端系统。

我建议的第一件事是简单地更新系统已知的根CA列表,如下所示。

# update CA certificates
sudo apt-get install apt-transport-https ca-certificates -y
sudo update-ca-certificates

如果您正在处理一个很长时间没有更新的系统,这可能会有所帮助,但当然不会解决私有证书的问题。

如果您将来自远程git服务器的证书添加到本地检查的证书列表中,来自git客户端的错误将会得到解决。这可以通过使用openssl从远程主机获取证书来实现:

openssl s_client -showcerts -servername git.mycompany.com -connect git.mycompany.com:443 </dev/null 2>/dev/null | sed -n -e '/BEGIN\ CERTIFICATE/,/END\ CERTIFICATE/ p'  > git-mycompany-com.pem

这将获取“https://git.mycompany.com”使用的证书,并将内容复制到名为“git-mycompany-com.pem”的本地文件中。

如果该主机只能通过web代理(如squid)访问git服务器,那么如果您使用的是openssl 1.1.0或更高版本,OpenSSL将只能利用Squid代理。但是如果您使用的是旧版本的OpenSSL,那么您将需要通过使用socat之类的东西来本地绑定到端口4443,并通过squid代理流量到最终目的地,从而解决这个限制。

# install socat
sudo apt-get install socat -y

# listen locally on 4443, send traffic through squid "squidhost"
socat TCP4-LISTEN:4443,reuseaddr,fork PROXY:squidhost:git.mycompany.com:443,proxyport=3128

然后在另一个控制台中,告诉OpenSSL从本地主机的端口4443获取证书。

openssl s_client -showcerts -servername git.mycompany.com -connect 127.0.0.1:4443 </dev/null 2>/dev/null | sed -n -e '/BEGIN\ CERTIFICATE/,/END\ CERTIFICATE/ p' > git-mycompany-com.pem

无论是通过代理还是直接连接,您现在在名为“git-mycompany-com.pem”的文件中都有远程证书列表。此文件将包含证书、其中间链和根 CA 证书。下一步是让 git 客户端在连接到 git 服务器时考虑这一点。这可以通过将证书添加到原始错误中提到的文件中来完成,在这种情况下,将为所有用户全局进行更改,或者可以将其添加到此单个用户的 git 配置中。

**全局添加**

cat git-mycompany-com.pem | sudo tee -a /etc/ssl/certs/ca-certificates.crt

**为单个用户添加**

git config --global http."https://git.mycompany.com/".sslCAInfo ~/git-mycompany-com.pem

它会自动将下面几行添加到~/中。gitconfig

[http "https://git.mycompany.com/"]
        sslCAInfo = /home/user/git-mycompany-com.pem

避免跳过 SSL 认证验证的解决方法。仅使用它们来快速测试证书是否是根本问题,然后使用上述部分来解决问题。

git config --global http.sslverify false

export GIT_SSL_NO_VERIFY=true

匿名用户

我知道已经有答案了。只是对于那些使用专用网络的人,比如Zcaler之类的,如果您的root cert需要更新,就会出现此错误。以下是在Windows机器上使用WSL如何实现此更新的解决方案:

#!/usr/bin/bash

# I exported the Zscaler certifcate out of Microsoft Cert Manager.  It was located under 'Trusted Root Certification > Certificates' as zscaler_cert.cer.
# Though the extension is '.cer' it really is a DER formatted file.
# I then copied that file into Ubuntu running in WSL.

# Convert DER encoded file to CRT.
openssl x509 -inform DER -in zscaler_cert.cer -out zscaler_cert.crt

# Move the CRT file to /usr/local/share/ca-certificates
sudo mv zscaler_cert.crt /usr/local/share/ca-certificates

# Inform Ubuntu of new cert.
sudo update-ca-certificates