git连接到https服务器时出现“gnutls_handshake() failed”

git连接到https服务器时出现“错误: gnutls_handshake()失败”

问题描述

当我尝试使用git连接到任何HTTPS服务器时(例如git clone),它会出现以下错误:

$ git clone https://github.com/xx/x.git
Cloning into 'xx'...
fatal: unable to access 'https://github.com/xx/xx.git/': gnutls_handshake() failed: The TLS connection was non-properly terminated.

之前使用搜索到一些文章,说是代理问题,取消即可:

# 如果没有配置代理,可使用以下命令 取消代理 :
git config --global --unset http.proxy
git config --global --unset https.proxy

可是问题仍然存在。

error: gnutls_handshake() failed: A TLS packet with unexpected length was received. while accessing ...
fatal: HTTP request failed

我认为可能有些与gnutls_handshake相关的软件包已被破坏;开始怀疑SSL有关的部分。

参考资料:

傻瓜化解决方法

关闭SSL自签名,不需要额外的改动:

git config --global http.sslVerify false

附录:通过重装git解决

得到问题的原因,gnutls包出现问题可能会导致连接异常,它在代理背后很奇怪。

但是openssl即使在弱网络中也能正常工作。所以解决方法是我们应该用openssl编译git

为此,请运行以下命令:

$ sudo apt-get update
$ sudo apt-get install build-essential fakeroot dpkg-dev
$ sudo apt-get build-dep git
$ mkdir ~/git-openssl
$ cd ~/git-openssl
$ apt-get source git
$ dpkg-source -x git_1.7.9.5-1.dsc
$ cd git-1.7.9.5

请记住将1.7.9.5替换为系统中实际的git版本。

然后,编辑debian/control文件(运行命令:gksu gedit debian/control)并将所有libcurl4-gnutls-dev实例替换为libcurl4-openssl-dev

然后构建软件包(如果测试失败,可以从文件debian/rules中删除TEST=test行):

sudo apt-get install libcurl4-openssl-dev
sudo dpkg-buildpackage -rfakeroot -b

安装新包:

  • i386:sudo dpkg -i ../git_1.7.9.5-1_i386.deb
  • x86_64:sudo dpkg -i ../git_1.7.9.5-1_amd64.deb

灵感来自:https://github.com/xmendez/wfuzz/wiki/PyCurlSSLBug

posted @ 2021-06-04 09:39  schips  阅读(7208)  评论(0编辑  收藏  举报