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

Git推送到GitHub时提示Permission denied(publickey)问题求助

Git推送失败(Permission denied (publickey))排查方案

错误信息

git push origin master
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设置中
  • 删除$HOME/.ssh目录并重新生成SSH密钥(ssh-keygen -t rsa -b 4096),随后将新公钥复制到GitHub

远程仓库配置

git remote -v
origin  git@github.com:myusername/myrepo.git (fetch)
origin  git@github.com:myusername/myrepo.git (push)

可能原因及解决步骤

1. SSH密钥未被ssh-agent加载

即使生成了密钥,若ssh-agent未运行或未添加私钥,GitHub无法完成身份验证。

# 启动ssh-agent
eval "$(ssh-agent -s)"
# 添加私钥(若生成时修改了密钥名称,替换为实际路径)
ssh-add ~/.ssh/id_rsa

2. 多SSH密钥冲突

若存在多个GitHub账号或其他服务的SSH密钥,默认可能使用了错误密钥连接GitHub。
在~/.ssh/config文件中添加配置(无则新建):

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa  # 替换为你的私钥路径

3. .ssh目录或密钥文件权限错误

权限设置不正确会导致ssh无法读取私钥:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub

4. GitHub端公钥关联异常

复制公钥时可能存在格式错误(如多余空格、换行或内容遗漏),可通过以下命令验证SSH连接:

ssh -T git@github.com

成功时会显示Hi myusername! You've successfully authenticated, but GitHub does not provide shell access.,失败则会输出具体错误提示。

5. 网络或防火墙限制

本地防火墙、代理可能阻止了SSH连接GitHub的22端口:

  • 用telnet测试端口连通性:
    telnet github.com 22
    
  • 临时解决方案:改用HTTPS协议推送(修改远程仓库地址):
    git remote set-url origin https://github.com/myusername/myrepo.git
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 11:50:20