报错解决:git clone git@github.com: Permission denied (publickey)权限拒绝问题
解析 git clone SSH 报 Permission denied (publickey) 的原因,提供 HTTPS 临时克隆与 ed25519 SSH 密钥生成、添加 GitHub、ssh-agent 验证的完整根治步骤(以 detectron2 为例)。
一、前言
最近在部署detectron2(Facebook开源的目标检测框架)时,执行克隆命令:
git clone git@github.com:facebookresearch/detectron2.git
终端直接抛出如下错误:
Cloning into 'detectron2'...
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.

起初以为是仓库权限问题,但detectron2是公开仓库,任何人都可克隆,排查后发现核心是SSH身份验证环节出了问题。本文将完整还原问题分析和解决过程,帮助遇到同类报错的开发者少走弯路。
二、问题分析
要理解这个报错,首先要明白Git通过SSH协议访问GitHub的底层逻辑:
- SSH验证原理:GitHub通过SSH密钥对(公钥+私钥)验证用户身份——本地生成密钥对后,将公钥配置到GitHub账户,克隆/推送代码时,GitHub服务器会校验本地私钥与账户公钥是否匹配,匹配成功才允许操作。
- 报错本质:
Permission denied (publickey)意味着GitHub服务器未在你的账户中找到匹配的SSH公钥,无法确认你的身份,因此拒绝了SSH连接请求。 - 常见诱因:
- 本地未生成SSH密钥对;
- 生成了密钥对但未将公钥添加到GitHub账户;
- SSH agent未启动,或未将私钥添加到agent中;
- 密钥文件路径/权限配置错误(如私钥权限过宽)。
三、解决方案
针对该问题,提供两种解决方案:「快速临时方案」(改用HTTPS克隆)和「长期根治方案」(配置SSH密钥),可根据实际需求选择。
方案1:快速解决——改用HTTPS协议克隆(无需配置SSH)
这是最便捷的临时方案,跳过SSH验证,直接使用HTTPS协议克隆仓库,适合临时克隆公开仓库的场景。
执行如下命令即可:
git clone https://github.com/facebookresearch/detectron2.git

注意事项:
- 如果克隆时提示输入GitHub账号密码:
- 账号填写你的GitHub用户名;
- 若开启了GitHub两步验证(2FA),密码不能填登录密码,需使用「个人访问令牌(PAT)」代替。
- 生成PAT的步骤:GitHub首页 → 右上角头像 → Settings → Developer settings → Personal access tokens → Generate new token → 勾选
repo权限 → 生成后复制令牌(仅显示一次,需妥善保存)。
方案2:长期根治——配置SSH密钥(推荐)
如果需要长期使用SSH协议操作GitHub(如频繁推送代码、克隆私有仓库),建议配置SSH密钥,一劳永逸解决身份验证问题,步骤如下:
步骤1:检查本地是否已有SSH密钥
首先查看本地.ssh目录下是否有现成的密钥文件(如id_rsa.pub/id_ed25519.pub,.pub后缀为公钥,无后缀为私钥):
ls -al ~/.ssh
如果输出中有id_ed25519.pub或id_rsa.pub,说明已有密钥对,可跳过步骤2;若为空,需生成新密钥。

步骤2:生成新的SSH密钥(无密钥时执行)
使用ssh-keygen生成密钥对(推荐使用更安全的ed25519算法),替换为你的GitHub注册邮箱:
ssh-keygen -t ed25519 -C "your_email@example.com"
执行后按提示操作:
-
按回车默认保存路径(
~/.ssh/id_ed25519),无需修改; -
可选设置密钥密码(按回车跳过即可,设置后每次使用SSH需输入密码,更安全)。

步骤3:启动SSH Agent并添加私钥
SSH Agent用于管理私钥,避免每次操作都输入密钥密码,执行如下命令:
# 启动ssh-agent
eval "$(ssh-agent -s)"
# 添加私钥(若为rsa密钥,替换为id_rsa)
ssh-add ~/.ssh/id_ed25519

步骤4:复制SSH公钥内容
执行命令复制公钥文本(后续需粘贴到GitHub):
cat ~/.ssh/id_ed25519.pub
复制输出的全部内容(以ssh-ed25519开头,你的邮箱结尾的完整字符串)。
步骤5:将公钥添加到GitHub账户
-
打开GitHub官网,登录后点击右上角头像 →
Settings(设置);
-
在左侧菜单找到
SSH and GPG keys→ 点击New SSH key;
-
Title栏填写标识(如“工作机-2026”),Key栏粘贴复制的公钥内容;
-
点击
Add SSH key完成添加(若开启2FA,需验证身份)。
步骤6:验证SSH连接
执行如下命令验证配置是否成功:
ssh -T git@github.com
若终端输出Hi [你的GitHub用户名]! You've successfully authenticated, but GitHub does not provide shell access.,说明SSH配置成功。

步骤7:重新克隆仓库
此时再执行原SSH克隆命令,即可正常克隆:
git clone git@github.com:facebookresearch/detectron2.git

四、总结
- 报错核心原因:本地SSH公钥未配置到GitHub账户,导致GitHub无法验证身份,拒绝SSH连接。
- 方案选择建议:临时克隆公开仓库选HTTPS方式(快速),长期开发/操作私有仓库选SSH配置(便捷、安全)。
- 额外注意点:配置SSH后若仍报错,可检查密钥文件权限(私钥需设为600)、SSH Agent是否正常运行,或更换RSA算法重新生成密钥。
希望本文能帮助大家快速解决git@github.com: Permission denied (publickey)问题,如果你有其他解决思路,欢迎在评论区交流~
報錯解決:git clone git@github.com: Permission denied (publickey)許可權拒絕問題
解析 git clone SSH 報 Permission denied (publickey) 的原因,提供 HTTPS 臨時克隆與 ed25519 SSH 密鑰生成、添加 GitHub、ssh-agent 驗證的完整根治步驟(以 detectron2 為例)。
來源:https://blog.csdn.net/2403_87969572/article/details/156694422
抓取時間(ISO本地):2026-05-18 05:17:24
一、前言
最近在部署detectron2(Facebook開源的目標檢測框架)時,執行克隆命令:
git clone git@github.com:facebookresearch/detectron2.git
終端直接丟擲如下錯誤:
Cloning into 'detectron2'...
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.

起初以為是倉庫許可權問題,但detectron2是公開倉庫,任何人都可克隆,排查後發現核心是SSH身份驗證環節出了問題。本文將完整還原問題分析和解決過程,幫助遇到同類報錯的開發者少走彎路。
二、問題分析
要理解這個報錯,首先要明白Git透過SSH協議訪問GitHub的底層邏輯:
- SSH驗證原理:GitHub透過SSH金鑰對(公鑰+私鑰)驗證使用者身份——本地生成金鑰對後,將公鑰配置到GitHub賬戶,克隆/推送程式碼時,GitHub伺服器會校驗本地私鑰與賬戶公鑰是否匹配,匹配成功才允許操作。
- 報錯本質:
Permission denied (publickey)意味著GitHub伺服器未在你的賬戶中找到匹配的SSH公鑰,無法確認你的身份,因此拒絕了SSH連線請求。 - 常見誘因:
- 本地未生成SSH金鑰對;
- 生成了金鑰對但未將公鑰新增到GitHub賬戶;
- SSH agent未啟動,或未將私鑰新增到agent中;
- 金鑰檔案路徑/許可權配置錯誤(如私鑰許可權過寬)。
三、解決方案
針對該問題,提供兩種解決方案:「快速臨時方案」(改用HTTPS克隆)和「長期根治方案」(配置SSH金鑰),可根據實際需求選擇。
方案1:快速解決——改用HTTPS協議克隆(無需配置SSH)
這是最便捷的臨時方案,跳過SSH驗證,直接使用HTTPS協議克隆倉庫,適合臨時克隆公開倉庫的場景。
執行如下命令即可:
git clone https://github.com/facebookresearch/detectron2.git

注意事項:
- 如果克隆時提示輸入GitHub賬號密碼:
- 賬號填寫你的GitHub使用者名稱;
- 若開啟了GitHub兩步驗證(2FA),密碼不能填登入密碼,需使用「個人訪問令牌(PAT)」代替。
- 生成PAT的步驟:GitHub首頁 → 右上角頭像 → Settings → Developer settings → Personal access tokens → Generate new token → 勾選
repo許可權 → 生成後複製令牌(僅顯示一次,需妥善儲存)。
方案2:長期根治——配置SSH金鑰(推薦)
如果需要長期使用SSH協議操作GitHub(如頻繁推送程式碼、克隆私有倉庫),建議配置SSH金鑰,一勞永逸解決身份驗證問題,步驟如下:
步驟1:檢查本地是否已有SSH金鑰
首先檢視本地.ssh目錄下是否有現成的金鑰檔案(如id_rsa.pub/id_ed25519.pub,.pub字尾為公鑰,無字尾為私鑰):
ls -al ~/.ssh
如果輸出中有id_ed25519.pub或id_rsa.pub,說明已有金鑰對,可跳過步驟2;若為空,需生成新金鑰。

步驟2:生成新的SSH金鑰(無金鑰時執行)
使用ssh-keygen生成金鑰對(推薦使用更安全的ed25519演算法),替換為你的GitHub註冊郵箱:
ssh-keygen -t ed25519 -C "your_email@example.com"
執行後按提示操作:
-
按回車預設儲存路徑(
~/.ssh/id_ed25519),無需修改; -
可選設定金鑰密碼(按回車跳過即可,設定後每次使用SSH需輸入密碼,更安全)。

步驟3:啟動SSH Agent並新增私鑰
SSH Agent用於管理私鑰,避免每次操作都輸入金鑰密碼,執行如下命令:
# 啟動ssh-agent
eval "$(ssh-agent -s)"
# 新增私鑰(若為rsa金鑰,替換為id_rsa)
ssh-add ~/.ssh/id_ed25519

步驟4:複製SSH公鑰內容
執行命令複製公鑰文字(後續需貼上到GitHub):
cat ~/.ssh/id_ed25519.pub
複製輸出的全部內容(以ssh-ed25519開頭,你的郵箱結尾的完整字串)。
步驟5:將公鑰新增到GitHub賬戶
-
開啟GitHub官網,登入後點選右上角頭像 →
Settings(設定);
-
在左側選單找到
SSH and GPG keys→ 點選New SSH key;
-
Title欄填寫標識(如“工作機-2026”),Key欄貼上複製的公鑰內容;
-
點選
Add SSH key完成新增(若開啟2FA,需驗證身份)。
步驟6:驗證SSH連線
執行如下命令驗證配置是否成功:
ssh -T git@github.com
若終端輸出Hi [你的GitHub使用者名稱]! You've successfully authenticated, but GitHub does not provide shell access.,說明SSH配置成功。

步驟7:重新克隆倉庫
此時再執行原SSH克隆命令,即可正常克隆:
git clone git@github.com:facebookresearch/detectron2.git

四、總結
- 報錯核心原因:本地SSH公鑰未配置到GitHub賬戶,導致GitHub無法驗證身份,拒絕SSH連線。
- 方案選擇建議:臨時克隆公開倉庫選HTTPS方式(快速),長期開發/操作私有倉庫選SSH配置(便捷、安全)。
- 額外注意點:配置SSH後若仍報錯,可檢查金鑰檔案許可權(私鑰需設為600)、SSH Agent是否正常執行,或更換RSA演算法重新生成金鑰。
希望本文能幫助大家快速解決git@github.com: Permission denied (publickey)問題,如果你有其他解決思路,歡迎在評論區交流~
Fix: git clone git@github.com Permission denied (publickey)
While deploying detectron2, I ran: text git clone git@github.com:facebookresearch/detectron2.git Error: text Cloning into 'detectron2'... 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. The repo is public, so access rights were not the issue — SSH authentication failed. This post documents the fix.
Captured at (local ISO): 2026-05-18 05:17:24
I. Preface
While deploying detectron2, I ran:
git clone git@github.com:facebookresearch/detectron2.git
Error:
Cloning into 'detectron2'...
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.

The repo is public, so access rights were not the issue — SSH authentication failed. This post documents the fix.
II. Analysis
How GitHub SSH works:
- You generate a key pair; add the public key to GitHub; Git matches your local private key on clone/push.
Permission denied (publickey)means GitHub found no matching public key for your account.- Common causes:
- No SSH key pair;
- Public key not added to GitHub;
- ssh-agent not running or private key not loaded;
- Wrong path or permissions on key files.
III. Solutions
Option 1: Quick fix — HTTPS clone (no SSH setup)
git clone https://github.com/facebookresearch/detectron2.git

Notes:
- If prompted for password with 2FA enabled, use a Personal Access Token (PAT), not your login password.
- PAT: GitHub → Settings → Developer settings → Personal access tokens → Generate → enable
repo→ copy once.
Option 2: Long-term fix — SSH keys (recommended)
Step 1: Check for existing keys
ls -al ~/.ssh
If you see id_ed25519.pub or id_rsa.pub, skip generation.

Step 2: Generate a new key (if needed)
ssh-keygen -t ed25519 -C "your_email@example.com"
- Default path
~/.ssh/id_ed25519is fine (Enter). - Optional passphrase (Enter to skip).

Step 3: Start ssh-agent and add private key
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
(Use id_rsa for RSA keys.)

Step 4: Copy public key
cat ~/.ssh/id_ed25519.pub
Copy the full ssh-ed25519 ... email line.
Step 5: Add to GitHub
- Avatar → Settings.

- SSH and GPG keys → New SSH key.

- Title (e.g. “Work-PC-2026”), paste key in Key.

- Add SSH key (2FA verification if enabled).

Step 6: Test connection
ssh -T git@github.com
Success message: Hi [username]! You've successfully authenticated...

Step 7: Clone again
git clone git@github.com:facebookresearch/detectron2.git

IV. Summary
- Cause: GitHub has no matching SSH public key for your machine.
- Choose: HTTPS for one-off public clones; SSH for daily dev and private repos.
- Still failing? Check private key permissions (600), ssh-agent, or regenerate with RSA/ed25519.
Share other fixes in the comments.