MacOS下git clone git@github.com: Permission denied (publickey)錯誤及解決方式

前言

這個錯誤是筆者在使用以下指令時發生的:

git clone git@github.com:dcos/dcos-cli.git

錯誤訊息

Cloning into ‘dcos-cli’…
Warning: Permanently added the RSA host key for IP address ‘192.30.253.113’ to the list of known hosts.
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.

解決辦法

GitHub Help - Error: Permission denied (publickey)中有給出完整的問題排除方法。
以下記錄筆者實測的結果。

改用git當使用者名稱

如果原先使用的是:

ssh -T GITHUB-USERNAME@github.com

請將使用者名稱改為git:

ssh -T git@github.com

但在筆者的機器上還是輸出:

git@github.com: Permission denied (publickey).

所以問題不在這裡。

檢查是否有使用key

在背景啟動ssh-agent

eval "$(ssh-agent -s)"

Agent pid 67142

檢查是否有私鑰且己載入ssh:

ssh-add -l -E md5

The agent has no identities.

如果輸出不是像:

2048 MD5:a0:dd:42:3c:5a:9d:e4:2a:21:52:4e:78:07:6e:c8:4d /Users/you/.ssh/id_rsa (RSA)

則需要生成新的ssh key並將它與GitHub關聯。
下面這個部份來自:GitHub Help - Generating a new SSH key and adding it to the ssh-agent

生成新的ssh key

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

如果過程中出現Enter file in which to save the key,可以直接按Enter略過,使用預設的檔案路徑。
如果出現Enter passphrase,可以輸入自己的密碼或直接按Enter略過。
輸出:

Generating public/private rsa key pair.
Enter file in which to save the key (/Users/yourname/.ssh/id_rsa): 
/Users/yourname/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/yourname/.ssh/id_rsa.
Your public key has been saved in /Users/yourname/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:CXtXJ/s8V00b5oz474OUm2lHsBrtP7fgU3eWYy4eCYs mimifasosofamire1123@gmail.com
The key's randomart image is:
+---[RSA 4096]----+
|                 |
|                 |
|      .     o .o.|
|       o . ..==.+|
|      . S .oo.+++|
|       . ...+B.*=|
|         E .==&.=|
|           ..X+B.|
|            oo*==|
+----[SHA256]-----+

經過這一步以後,新的key己經生成,被存在~/.ssh/config這個路徑下。
我們可以用cat來查看它的內容:

cat ~/.ssh/config

# --- Sourcetree Generated ---
Host yourgithubname-GitHub
	HostName github.com
	User yourgithubname
	PreferredAuthentications publickey
	IdentityFile /Users/yourname/.ssh/yourgithubname-GitHub
	UseKeychain yes
	AddKeysToAgent yes
# ----------------------------

ForwardX11 yes

因為這時key己經生成,這時候可以回去用ssh-add -l -E md5來查看key是否己載入ssh:

2048 MD5:03:57:c1:67:f1:04:20:7d:73:4b:91:d4:39:7d:90:47 /Users/yourname/.ssh/id_rsa (RSA)

可以發現己經有id了。

這時用ssh -vT git@github.com試著連接到git@github.com,來檢查剛剛生成的key是否己被使用:

OpenSSH_7.6p1, LibreSSL 2.6.2
debug1: Reading configuration data /Users/yourname/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 48: Applying options for *
debug1: /etc/ssh/ssh_config line 52: Applying options for *
debug1: Connecting to github.com port 22.
debug1: Connection established.
debug1: identity file /Users/yourname/.ssh/id_rsa type 0
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yourname/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yourname/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yourname/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yourname/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yourname/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yourname/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yourname/.ssh/id_ed25519-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_7.6
debug1: Remote protocol version 2.0, remote software version babeld-f43b814b
debug1: no match: babeld-f43b814b
debug1: Authenticating to github.com:22 as ‘git’
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256@libssh.org
debug1: kex: host key algorithm: ssh-rsa
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ssh-rsa SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8
debug1: Host ‘github.com’ is known and matches the RSA host key.
debug1: Found key in /Users/yourname/.ssh/known_hosts:4
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: RSA SHA256:FbFoShO5TA4eD6fXyLjyJJbPGvO8CGO00ZXMmR/7H8I /Users/yourname/.ssh/id_rsa
debug1: Authentications that can continue: publickey
debug1: Offering public key: RSA SHA256:CXtXJ/s8V00b5oz474OUm2lHsBrtP7fgU3eWYy4eCYs /Users/yourname/.ssh/id_rsa
debug1: Authentications that can continue: publickey
debug1: Trying private key: /Users/yourname/.ssh/id_dsa
debug1: Trying private key: /Users/yourname/.ssh/id_ecdsa
debug1: Trying private key: /Users/yourname/.ssh/id_ed25519
debug1: No more authentication methods to try.
git@github.com: Permission denied (publickey).

結果仍然是Permission denied。
這是因為我們尚未將ssh key添加到ssh-agent及GitHub帳戶所致。

將ssh key添加到ssh-agent

在背景啟動ssh-agent:

eval "$(ssh-agent -s)"

將ssh私鑰加入ssh-agent:

ssh-add -K ~/.ssh/id_rsa

這一步完成後,還需將ssh key加入GitHub帳戶。

將SSH key加入GitHub帳戶

此處內容來自:GitHub Help - Adding a new SSH key to your GitHub account

~/.ssh/id_rsa.pub檔案裡的內容複製到剪貼簿:

pbcopy < ~/.ssh/id_rsa.pub

前往GitHub頁面,找到右上角的+號→SSH and GPG keysNew SSH keyAdd SSH key

這時會出現兩個欄位:
Title欄位輸入有意義的名稱,如iMacPersonal MacBook Air
Key欄位貼上剛剛複製的內容。
最後點擊Add SSH key來完成ssh key的添加。

檢查ssh key是否有被使用

可以再次使用以下指令,來檢查剛添加的key有沒有被使用:

ssh -vT git@github.com

OpenSSH_7.6p1, LibreSSL 2.6.2
debug1: Reading configuration data /Users/yourname/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 48: Applying options for *
debug1: /etc/ssh/ssh_config line 52: Applying options for *
debug1: Connecting to github.com port 22.
debug1: Connection established.
debug1: identity file /Users/yourname/.ssh/id_rsa type 0
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yourname/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yourname/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yourname/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yourname/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yourname/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yourname/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/yourname/.ssh/id_ed25519-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_7.6
debug1: Remote protocol version 2.0, remote software version babeld-f43b814b
debug1: no match: babeld-f43b814b
debug1: Authenticating to github.com:22 as ‘git’
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256@libssh.org
debug1: kex: host key algorithm: ssh-rsa
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ssh-rsa SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8
debug1: Host ‘github.com’ is known and matches the RSA host key.
debug1: Found key in /Users/yourname/.ssh/known_hosts:4
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: RSA SHA256:FbFoShO5TA4eD6fXyLjyJJbPGvO8CGO00ZXMmR/7H8I /Users/yourname/.ssh/id_rsa
debug1: Authentications that can continue: publickey
debug1: Offering public key: RSA SHA256:CXtXJ/s8V00b5oz474OUm2lHsBrtP7fgU3eWYy4eCYs /Users/yourname/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 535
debug1: Authentication succeeded (publickey).
Authenticated to github.com ([192.30.253.112]:22).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: pledge: network
debug1: Sending environment.
debug1: Sending env LANG = de_DE.UTF-8
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
Hi yourgithubname! You’ve successfully authenticated, but GitHub does not provide shell access.
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 3760, received 2060 bytes, in 0.8 seconds
Bytes per second: sent 4446.1, received 2435.9
debug1: Exit status 1

這時再次使用:

git clone git@github.com:dcos/dcos-cli.git

發現它己經能正常運作了!

參考連結

GitHub Help - Error: Permission denied (publickey)
GitHub Help - Generating a new SSH key and adding it to the ssh-agent
GitHub Help - Adding a new SSH key to your GitHub account

<think>好的,用户现在遇到了Git推送时的权限问题,提示“Permission denied (publickey)”,这通常和SSH配置有关。我需要根据之前提供的引用内容来帮助他解决这个问题。 首先,我得回忆一下引用里的信息。引用[1]提到,出现这个错误通常有两个原因:没有生成SSH key或者生成的key不匹配。解决办法是重新生成SSH key并添加到GitHub账户。步骤包括生成密钥、复制公钥内容到GitHub的设置里。引用[2]补充了具体操作,比如打开~/.ssh/id_rsa.pub文件,复制内容到GitHub的SSH设置中。 用户现在的问题和之前的情况类似,所以步骤应该差不多。但可能需要更详细的指导,特别是针对不同操作系统的路径问题。比如,Windows用户可能需要在PowerShell或者Git Bash里操作,而Linux/macOS用户则直接使用终端。 接下来,我需要检查用户是否已经生成了SSH key。通常,默认的私钥文件是id_rsa,公钥是id_rsa.pub。如果用户之前生成过,可能需要检查文件名是否正确,或者是否存在多个密钥导致冲突。这时候可能需要用ssh-add命令来添加密钥到代理。 另外,权限问题也很重要。.ssh目录的权限应该是700,私钥文件是600,公钥是644。如果权限不对,SSH会拒绝使用这些文件,导致错误。 测试连接是否成功是必要的,使用ssh -T git@github.com可以验证配置是否正确。如果出现认证成功的消息,说明配置正确;否则需要进一步排查。 可能用户还遇到其他情况,比如使用HTTPS协议而不是SSH,这时候可能需要更改远程仓库的URL。或者存在多个Git账户,需要配置SSH config文件来管理不同的密钥。 最后,生成相关问题可以帮助用户深入理解问题,比如如何管理多个SSH密钥,或者HTTPS和SSH的区别。这些问题可能用户后续会遇到,提前准备可以提升帮助的全面性。 总结一下,解决步骤应该是:生成SSH密钥,添加到GitHub,测试连接,处理可能的权限或配置问题。同时,考虑不同操作系统的情况,给出对应的命令和路径。确保用户能一步步跟着操作,避免遗漏细节。</think>### Git推送权限问题解决方案 #### 一、核心原因诊断 出现`Permission denied (publickey)`错误时,通常由以下三种情况导致: 1. SSH密钥未生成或路径错误 2. 公钥未正确绑定GitHub账户 3. 密钥文件权限配置异常 #### 二、逐步解决方案 **步骤1:检查现有SSH密钥** ```bash ls -al ~/.ssh ``` 预期应看到`id_rsa`(私钥)和`id_rsa.pub`(公钥)文件。如果不存在则需要新建密钥对[^1]。 **步骤2:生成新密钥对** ```bash ssh-keygen -t ed25519 -C "your_email@example.com" ``` (按3次回车保持默认设置) ![密钥生成示意图](https://docs.github.com/assets/cb-36544/images/help/ssh/ssh-keygen-command.png) **步骤3:添加密钥到SSH代理** ```bash eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_ed25519 ``` **步骤4:绑定公钥到GitHub** ```bash cat ~/.ssh/id_ed25519.pub ``` 复制输出内容,登录GitHub: 1. 右上角头像 > Settings > SSH and GPG keys 2. 点击"New SSH key" 3. 粘贴公钥内容并命名 **步骤5:验证连接** ```bash ssh -T git@github.com ``` 成功时会显示: ```text Hi username! You've successfully authenticated... ``` **步骤6:修正仓库远程地址** ```bash git remote set-url origin git@github.com:username/repo.git ``` (替换username和repo为实际值) #### 三、高级排查技巧 1. **权限修复**: ```bash chmod 700 ~/.ssh chmod 600 ~/.ssh/id_* ``` 2. **多密钥管理**: 创建`~/.ssh/config`文件: ```text Host github.com HostName github.com User git IdentityFile ~/.ssh/id_ed25519 ``` 3. **调试模式**: ```bash ssh -vT git@github.com ``` #### 四、常见错误对照表 | 错误现象 | 解决方案 | |---------|----------| | `No such file or directory` | 确认密钥路径正确 | | `Bad permissions` | 执行权限修复命令 | | `Connection closed` | 检查防火墙/代理设置 |
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值