Git SSH Host Key Verification Failed 问题排查与解决问题描述在 VSCode 或命令行中使用 SSH 方式操作 Git 远程仓库时遇到如下报错 git fetch testgit1 Host key verification failed. fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.原因分析SSH 在首次连接一台主机时会要求确认该主机的身份并将主机密钥host key保存到~/.ssh/known_hosts文件中。如果该文件中没有目标主机的密钥记录且当前环境无法进行交互式确认例如 VSCode 后台执行的 Git 操作SSH 会直接拒绝连接报出Host key verification failed错误。常见触发场景新装的系统或新创建的 SSH 环境从未连接过该 Git 平台首次使用 SSH 协议操作 Gitee/GitHub/GitLab 等平台VSCode 等 IDE 后台自动执行git fetch无法弹出交互式确认提示解决方法方法一ssh-keyscan 自动添加推荐一条命令将目标平台的主机密钥加入信任列表ssh-keyscan-trsa,ecdsa,ed25519 gitee.com~/.ssh/known_hosts对其他平台同理替换域名即可# GitHubssh-keyscan-trsa,ecdsa,ed25519 github.com~/.ssh/known_hosts# GitLabssh-keyscan-trsa,ecdsa,ed25519 gitlab.com~/.ssh/known_hosts方法二手动测试连接触发确认ssh-Tgitgitee.com首次连接时会提示The authenticity of host gitee.com (xxx.xxx.xxx.xxx) cant be established. ECDSA key fingerprint is SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx. Are you sure you want to continue connecting (yes/no/[fingerprint])?输入yes回车即可。成功后会自动将密钥写入known_hosts。之后再执行 Git 操作就不会提示了。验证是否解决ssh-Tgitgitee.com如果输出类似以下内容说明 SSH 连接已正常Hi TDon1! Youve successfully authenticated, but GITEE.COM does not provide shell access.注意GitHub 的输出为Hi username! Youve successfully authenticated...GitLab 为Welcome to GitLab, username!语义相同。扩展known_hosts 文件说明文件路径说明~/.ssh/known_hosts用户级别所有私钥共享Windows 路径C:\Users\用户名\.ssh\known_hosts/etc/ssh/ssh_known_hosts系统级别需管理员权限每行记录一条主机密钥格式为主机名 密钥类型 密钥内容例如gitee.com ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNo...总结Host key verification failed是 SSH 无法验证远程主机身份时的安全保护机制本质上不是 Git 的问题而是 SSH 配置不完整导致的。通过ssh-keyscan或首次手动ssh -T连接即可解决。建议在配置好 SSH 密钥后顺手执行一次ssh -T测试避免后续操作时被阻塞。