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

GitHub SSH密钥无法识别,推送时提示Permission denied(publickey)

解决Git连接GitHub时Permission denied (publickey)问题

问题重现

执行git push git@github.com时触发错误:

PS F:\Xampp\htdocs\website.com> git push git@github.com: Permission denied (publickey). fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.

尝试过重启SSH服务、重新生成密钥并添加至GitHub,但问题持续。操作中的关键报错:

  • PowerShell生成密钥失败:Saving key "~/.ssh/github" failed: No such file or directory
  • 指定密钥连接仍被拒绝:ssh -i ~/.ssh/id_rsa -T git@github.com返回Permission denied (publickey)

针对性解决方案

1. 修复PowerShell密钥生成路径问题

PowerShell中~的路径解析可能异常,改用绝对路径生成密钥:

# 替换Eoin为你的系统用户名
ssh-keygen -t rsa -b 4096 -C "eoin@squareballoon.co.uk" -f C:\Users\Eoin\.ssh\github

生成后将github.pub的内容完整复制到GitHub的SSH密钥列表中。

2. 强制指定GitHub连接用密钥

在~/.ssh/config(Windows路径:C:\Users\Eoin\.ssh\config)中添加配置,避免SSH自动选择错误密钥:

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa  # 替换为你实际使用的私钥路径,比如~/.ssh/github
  IdentitiesOnly yes

3. 修正密钥文件权限

  • Windows环境:右键.ssh文件夹→属性→安全→高级,确保只有当前用户拥有完全控制权限,删除其他用户(如Everyone、Administrators)的权限条目。
  • Bash/WSL环境:执行权限修正命令:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub

4. 排查连接细节

执行详细调试命令,查看SSH连接时的密钥加载情况:

ssh -vT git@github.com

重点关注输出中Offering public key:的条目,确认是否有你添加到GitHub的公钥;若出现server refused our key,说明该密钥未在GitHub注册或权限不匹配。

5. 验证GitHub公钥匹配

重新复制公钥内容到GitHub:

  • 执行Get-Content ~/.ssh/id_rsa.pub | clip(PowerShell)或cat ~/.ssh/id_rsa.pub(Bash)
  • 打开GitHub→Settings→SSH and GPG keys→New SSH key,粘贴内容并保存,确保无多余空格或换行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 09:58:22