You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决known_hosts文件更新失败问题?git fetch及SSH连接报错求助

解决SSH无法写入known_hosts文件的问题

问题症状

执行git fetch或ssh连接远程主机时,出现以下报错:

The authenticity of host 'github.com (198.18.0.74)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (ED25519) to the list of known hosts.
hostfile_replace_entries: mkstemp: No such file or directory
update_known_hosts: hostfile_replace_entries failed for /c/Users/aecra/.ssh/known_hosts: No such file or directory

解决方案

1. 检查并修复.ssh目录的存在性与权限

  • 打开终端(如Git Bash),先确认.ssh目录是否存在:
    ls /c/Users/aecra/.ssh/
    
  • 如果目录不存在,创建它:
    mkdir /c/Users/aecra/.ssh
    
  • 设置目录的正确权限(必须为700,否则SSH会拒绝使用):
    chmod 700 /c/Users/aecra/.ssh
    

2. 创建并配置known_hosts文件

  • 手动创建空的known_hosts文件:
    touch /c/Users/aecra/.ssh/known_hosts
    
  • 设置文件权限为600(仅当前用户可读写):
    chmod 600 /c/Users/aecra/.ssh/known_hosts
    

3. 手动添加主机公钥到known_hosts

  • 用ssh-keyscan命令自动获取并添加GitHub的公钥:
    ssh-keyscan github.com >> /c/Users/aecra/.ssh/known_hosts
    
  • 也可以直接手动写入GitHub的ED25519公钥内容到known_hosts文件:
    github.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl
    

4. 测试连接

执行以下命令验证是否能正常连接GitHub:

ssh -T git@github.com

如果看到Hi [你的用户名]! You've successfully authenticated, but GitHub does not provide shell access.的提示,说明问题已解决。

问题原因

报错核心是SSH无法创建或写入known_hosts文件,可能由以下原因导致:

  • .ssh目录不存在,或目录权限不符合SSH要求
  • known_hosts文件缺失,或文件权限过宽(SSH要求该文件必须是当前用户独占读写)
  • 系统无法创建临时文件(mkstemp失败),本质还是目录/文件的权限或存在性问题

内容的提问来源于stack exchange,提问作者Tian Jiale

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 09:01:29