使用Git,是否有一种方法告诉它接受自签名证书?
我使用https服务器托管git服务器,但目前证书是自签名的。
当我第一次尝试在那里创建回购时:
git push origin master -f
我得到了错误:
error: Cannot access URL
https://the server/git.aspx/PocketReferences/, return code 22
fatal: git-http-push failed
使用Git,是否有一种方法告诉它接受自签名证书?
我使用https服务器托管git服务器,但目前证书是自签名的。
当我第一次尝试在那里创建回购时:
git push origin master -f
我得到了错误:
error: Cannot access URL
https://the server/git.aspx/PocketReferences/, return code 22
fatal: git-http-push failed
当前回答
你可以将GIT_SSL_NO_VERIFY设置为true:
GIT_SSL_NO_VERIFY=true git clone https://example.com/path/to/git
或者配置Git不验证命令行上的连接:
git -c http.sslVerify=false clone https://example.com/path/to/git
请注意,如果您不验证SSL/TLS证书,那么您很容易受到MitM攻击。
其他回答
在Windows上,这对我来说很管用:
将自签名证书的内容添加到ca-bundle文件的末尾。包括-----BEGIN CERTIFICATE-----和-----END CERTIFICATE-----行
ca-bundle文件的位置通常是C:\Program Files\Git\mingw64\ssl\certs
然后,将ca-bundle文件的路径添加到全局git配置中。下面的命令可以做到这一点:git config——global http。sslCAInfo "C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt"
注意:路径依赖于ca-bundle文件的本地路径!
在.gitconfig文件中,您可以添加以下给定值,以使自签名证书可接受
sslCAInfo = /home/XXXX/abc.crt
你可以将GIT_SSL_NO_VERIFY设置为true:
GIT_SSL_NO_VERIFY=true git clone https://example.com/path/to/git
或者配置Git不验证命令行上的连接:
git -c http.sslVerify=false clone https://example.com/path/to/git
请注意,如果您不验证SSL/TLS证书,那么您很容易受到MitM攻击。
I'm not a huge fan of the [EDIT: original versions of the] existing answers, because disabling security checks should be a last resort, not the first solution offered. Even though you cannot trust self-signed certificates on first receipt without some additional method of verification, using the certificate for subsequent git operations at least makes life a lot harder for attacks which only occur after you have downloaded the certificate. In other words, if the certificate you downloaded is genuine, then you're good from that point onwards. In contrast, if you simply disable verification then you are wide open to any kind of man-in-the-middle attack at any point.
举一个具体的例子:著名的repo.or.cz存储库提供了一个自签名证书。我可以下载这个文件,把它放在/etc/ssl/certs这样的地方,然后做:
# Initial clone
GIT_SSL_CAINFO=/etc/ssl/certs/rorcz_root_cert.pem \
git clone https://repo.or.cz/org-mode.git
# Ensure all future interactions with origin remote also work
cd org-mode
git config http.sslCAInfo /etc/ssl/certs/rorcz_root_cert.pem
注意,在这里使用本地git配置(即不使用——global)意味着这个自签名证书只对这个特定的存储库受信任,这很好。它也比使用GIT_SSL_CAPATH更好,因为它消除了git通过不同的证书颁发机构进行验证的风险,这种风险可能会受到损害。
以下答案摘自迈克尔·考夫曼(Michael Kauffman)撰写的文章。
使用带有公司SSL证书的Git for Windows
问题:
如果你有一个公司SSL证书,想要从控制台或VSCode克隆你的repo,你会得到以下错误:
致命:无法访问“https://myserver/tfs/DefaultCollection/_git/Proj/”:SSL证书问题:无法获得本地颁发者证书
解决方案:
将根自签名证书导出到文件中。您可以在浏览器中完成此操作。 定位“ca-bundle”。crt”文件(当前版本C:\Program Files\Git\usr\ssl\certs,但在过去已更改)。将文件复制到您的用户配置文件。用文本编辑器(如VSCode)打开它,并将导出的证书的内容添加到文件的末尾。
现在我们必须配置git来使用新文件:
Git配置——global http。sslCAInfo C: /用户/ < yourname > / ca-bundle.crt
这将把以下条目添加到用户配置文件根目录下的.gitconfig文件中。
(http) sslCAInfo = C:/Users/<yourname>/ bat .crt