 failed 的排查与修复)
在项目中执行git fetch时报错fatal: unable to access https://git.example.com/rxx/demo_project.git/: gnutls_handshake() failed: Error in protocol version2. 表现git fetch/git pull均失败无法访问远程仓库。报错信息固定为gnutls_handshake() failed: Error in protocol version。系统其他工具如curl访问同一域名却正常。3. 根因分析3.1 服务器只支持 TLS 1.3用openssl s_client实测远端服务器echo|openssl s_client-connectgit.example.com:443-tls1_2# 失败# error:1409442E:SSL routines:ssl3_read_bytes:tlsv1 alert protocol versionecho|openssl s_client-connectgit.example.com:443-tls1_3# 成功服务器对 TLS 1.2 握手直接拒绝只放行 TLS 1.3。3.2 git 客户端使用的 TLS 库不支持 TLS 1.3旧版系统 gitUbuntu 18.04 自带 2.17.1使用系统 gnutls 3.5.18不支持 TLS 1.3。升级到 PPA 版 git 2.50.1 后依旧失败因为 PPA 把gnutls 版 libcurl 静态链接进了 git 二进制ldd /usr/bin/git看不到 libcurl 动态依赖被静态打入。git -c http.sslBackendopenssl报Could not set SSL backend to openssl: already set说明编译时已固定为 gnutls。系统curl之所以正常是因为它链接的是 openssl 版 libcurllibcurl.so.4。3.3 关键点组件TLS 后端TLS 1.3 支持结果系统 curlOpenSSL 1.1.1支持正常系统 git 2.17.1gnutls 3.5.18不支持失败PPA git 2.50.1静态 gnutls libcurl不支持失败4. 解决办法从源码编译 git链接 openssl 版 libcurl核心思路让 git 使用系统已有的 openssl 版libcurl.so.4而不是默认的-lcurl它会解析到 gnutls 版的libcurl.so - libcurl-gnutls.so符号链接。4.1 下载源码cd/tmp/opencodecurl-sL-ogit.tar.gz https://github.com/git/git/archive/refs/tags/v2.50.1.tar.gztarxzf git.tar.gzcdgit-2.50.14.2 生成 configure 并配置makeconfigure ./configure--prefix$HOME/git-local --without-tcltkCURL_LDFLAGS-Wl,-l:libcurl.so.4注意configure 生成的config.mak.autogen会覆盖为CURL_LDFLAGS-lcurl需要手动改回sed-is|^CURL_LDFLAGS-lcurl$|CURL_LDFLAGS-Wl,-l:libcurl.so.4|config.mak.autogen4.3 编译并安装make-j$(nproc)makeinstall4.4 验证# 确认 git-remote-http 链接 openssl 版 libcurlldd$HOME/git-local/libexec/git-core/git-remote-http# libcurl.so.4 /usr/lib/x86_64-linux-gnu/libcurl.so.4# libssl.so.1.1 /usr/lib/x86_64-linux-gnu/libssl.so.1.1# 测试 fetch$HOME/git-local/bin/git fetch origin# 成功exit04.5 设为默认echoexport PATH$HOME/git-local/bin:$PATH~/.zshrc# 新开终端生效5. 总结服务器只接受 TLS 1.3客户端 TLS 库不支持则握手失败。PPA 版 git 静态绑定了 gnutls 版 libcurl无法通过配置切换后端。从源码编译 git 并强制链接 openssl 版libcurl.so.4即可解决。