本文基于一次真实的 WSL 环境调试过程整理,涵盖 SSL 证书验证失败、SSH 端口超时、凭据缓存残留等典型问题,提供从诊断到解决的完整路径。
一、问题背景
在 WSL(Windows Subsystem for Linux)环境下从 GitHub 克隆仓库时,常常会遇到以下几类报错:
| 现象 | 报错关键词 |
|---|---|
| HTTPS 证书验证失败 | server certificate verification failed. CAfile: ... |
| SSH 连接超时 | ssh: connect to host github.com port 22: Connection timed out |
| 缺少 SSH 密钥 | no such identity: .../id_ed25519: No such file or directory |
| 公钥认证被拒 | Permission denied (publickey) |
| Gitee 认证失败 | Incorrect username or password (access token) |
这些问题的根源通常来自三个方面:WSL 与 Windows 证书链不同步、网络防火墙拦截 22 端口、Git 凭据缓存残留。
二、问题诊断:先搞清楚"卡在哪一层"
在动手修复前,先做两步快速诊断:
1. 确认 Git 的凭据助手配置
git config --get-all credential.helper
- 输出
cache→ 凭据存在内存中,用git credential-cache exit清除 - 输出
store→ 凭据存在~/.git-credentials,删除该文件即可 - 无输出 → 未配置凭据助手,问题不在这一层
2. 确认 SSH 目录状态
ls -la ~/.ssh/
如果只有 config 和 known_hosts,说明尚未生成任何密钥,需要先创建。
三、HTTPS 方式的修复方案
方案 A:更新 CA 证书(首选,最安全)
WSL 中的 CA 证书库可能过时或不完整,导致无法验证 GitHub 证书链:
sudo apt update
sudo apt upgrade -y
sudo apt-get install --reinstall ca-certificates
sudo update-ca-certificates
方案 B:让 Git 使用 Windows 证书存储(WSL 专治)
WSL 内的 Git 与 Windows 主机的证书环境不同步时,可直接指定 Git 使用 Windows 的 schannel 后端:
git config --global http.sslBackend schannel
这是微软文档中针对 WSL 证书问题的官方建议。
方案 C:临时跳过验证(应急,不推荐长期使用)
git -c http.sslVerify=false clone https://github.com/xxx/yyy.git
用 -c 参数将配置限制在单次命令,比全局 http.sslVerify=false 安全得多。
方案 D:重置错误的全局 SSL 配置
如果之前手动设置过 http.sslCAInfo 导致路径失效:
git config --global --unset http.sslCAInfo
git config --global --unset http.sslVerify
四、SSH 方式的完整配置(推荐方案)
HTTPS 证书问题反复出现时,改用 SSH + 443 端口是最一劳永逸的方案。
第一步:配置 SSH 走 443 端口(绕过 22 端口封锁)
编辑 ~/.ssh/config(不存在则创建):
nano ~/.ssh/config
添加以下内容:
Host github.com
Hostname ssh.github.com
Port 443
User git
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
原理:GitHub 官方支持通过
ssh.github.com:443提供 SSH 服务,443 是 HTTPS 标准端口,几乎不会被防火墙拦截。
第二步:验证 GitHub 主机指纹
首次连接时会提示确认指纹。GitHub 官方公布的 ED25519 指纹为:
SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU
确认一致后输入 yes 继续。
第三步:生成 SSH 密钥
ssh-keygen -t ed25519 -C "your_email@example.com"
三次提示的处理方式:
- 保存路径:直接回车,使用默认
~/.ssh/id_ed25519 - passphrase:可留空(免密使用),也可设置(更安全)
- 确认 passphrase:与上一步一致
第四步:设置正确的文件权限(关键!)
SSH 对权限非常敏感,权限过宽会导致密钥被拒绝加载:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
第五步:将公钥添加到 GitHub
-
查看并复制公钥:
cat ~/.ssh/id_ed25519.pub复制整行内容(以
ssh-ed25519开头)。 -
登录 GitHub → Settings → SSH and GPG keys → New SSH key
- Title 随意填(如
WSL-DESKTOP) - Key type 选
Authentication Key - 粘贴公钥内容,点击 Add SSH key
- Title 随意填(如
第六步:验证连接
ssh -T git@github.com
成功时会返回:
Hi <你的用户名>! You've successfully authenticated, but GitHub does not provide shell access.
第七步:克隆仓库
git clone git@github.com:DeepRoboticsLab/deep_robotics_model.git
五、Gitee 认证失败的额外处理
Gitee 已停用密码进行 HTTPS 克隆,必须使用个人访问令牌(PAT):
-
Gitee 网页端 → 设置 → 安全设置 → 私人令牌 → 生成新令牌
-
勾选
projects等所需权限,生成后立即复制保存(仅显示一次) -
清除本地凭据缓存:
git config --global --unset credential.helper rm -f ~/.git-credentials git credential-cache exit -
重新
git clone,密码处粘贴 PAT 而非登录密码
注意:
git config --system --unset credential.helper需要 root 权限,普通用户会报Permission denied,改用--global即可。
六、WSL 环境的额外提示
1. 跨系统凭据管理器干扰
WSL 中的 Git 可能调用 Windows 的凭据管理器。若 Linux 侧清理无效,可在 Windows 凭据管理器(控制面板 → 用户账户 → 凭据管理器 → Windows 凭据)中删除 git:https://github.com 或 git:https://gitee.com 开头的条目。
2. 代理环境
如果处于公司网络或使用代理,需为 Git 配置代理,或将公司根证书导入系统信任库:
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890
3. 善用 -c 参数
临时覆盖任何 Git 配置而不污染全局环境:
git -c credential.helper= clone <url>
git -c http.sslVerify=false clone <url>
七、推荐排查顺序(速查表)
┌─────────────────────────────────────┐
│ 1. git clone 报错? │
└──────────────┬──────────────────────┘
│
┌───────┴────────┐
│ │
HTTPS 报错 SSH 报错
│ │
▼ ▼
┌─────────┐ ┌──────────────┐
│ 证书问题 │ │ port 22 超时 │
└────┬────┘ └──────┬───────┘
│ │
▼ ▼
① 重装 CA 证书 ① 配置 443 端口
② sslBackend=schannel ② 生成 SSH 密钥
③ 临时 sslVerify=false ③ 添加公钥到 GitHub
④ chmod 600 权限
⑤ ssh -T 验证
八、核心经验总结
| 问题 | 最佳解法 |
|---|---|
| WSL 证书验证失败 | git config --global http.sslBackend schannel 或重装 ca-certificates |
| 22 端口超时 | SSH 走 ssh.github.com:443 |
| 缺少密钥 | ssh-keygen -t ed25519 + 添加到 GitHub |
| 权限过宽被拒 | chmod 600 ~/.ssh/id_ed25519 |
| Gitee 认证失败 | 使用 PAT 替代密码 + 清除凭据缓存 |
--system 权限拒绝 | 改用 --global 或 --local 作用域 |
一条黄金建议:在 WSL 中访问 GitHub,优先使用 SSH over 443。一次配置,永久解决证书、端口、认证三重问题,比 HTTPS 方式稳定得多。
305



被折叠的 条评论
为什么被折叠?



