Git-重装系统后ssh认证问题处理
🧾 Git 报错问题总结:Host key verification failed
🧠 问题背景
- 操作系统:你刚刚重装了 Windows
- 报错命令:
git pull --tags origin main
- 错误信息:
Host key verification failed.
fatal: Could not read from remote repository.
🧨 问题原因分析
- 系统重装后
.ssh
目录被清空- SSH 配置文件(包括私钥、公钥、
known_hosts
)都被重置或删除
- SSH 配置文件(包括私钥、公钥、
- 首次 SSH 连接 GitHub,系统未记录 Host Key
- SSH 安全机制要求你手动确认远程主机的身份(host key),否则连接失败
- 你未输入 “yes” 确认信任 GitHub 的 host key
- 导致连接被中断,显示
Host key verification failed
- 导致连接被中断,显示
✅ 解决方案
① 手动确认 GitHub 主机身份
执行以下命令:
ssh -T git@github.com
看到提示:
Are you sure you want to continue connecting (yes/no/[fingerprint])?
正确输入:
yes
系统将输出:
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
📌 这会在 ~/.ssh/known_hosts
文件中记录 GitHub 的 host key,未来不再提示。
② 如尚未生成 SSH 密钥,则需新建
ssh-keygen -t ed25519 -C "你的邮箱@example.com"
一路回车使用默认路径(建议使用 ed25519 算法)
然后将生成的 id_ed25519.pub
添加到 GitHub:
👉 https://github.com/settings/keys
③ 验证 SSH 连接是否正常
ssh -T git@github.com
看到:
Hi your-username! You've successfully authenticated...
说明连接成功 ✅
④ 再次执行 Git 命令
git pull --tags origin main
📌 附加命令备忘
命令 | 作用 |
---|---|
ssh-keygen -t ed25519 -C "你邮箱" | 生成 SSH 密钥对 |
ssh -T git@github.com | 测试 SSH 连接 |
ssh-keygen -R github.com | 删除已知的 GitHub host key(用于重新验证) |
notepad $env:USERPROFILE\.ssh\id_ed25519.pub | 打开 SSH 公钥文件 |