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

当你在使用 git 命令时,如果出现 "Permission denied" 的错误提示,通常是由于没有正确的身份验证导致的。首先,确保你已经在 GitHub 账户中设置了 SSH 密钥,这样你才能通过 SSH 协议进行身份验证。 另外,如果你在 macOS Mojave 中执行 `brew update` 命令时遇到了 "Permission denied" 的错误提示,可能是由于文件权限问题导致的。你可以尝试使用 `sudo` 命令以管理员身份运行该命令,或者检查你的文件和目录权限是否正确设置。你可以参考这篇文章来解决该问题。 同时,当你在执行 git 命令时出现 "Permission denied" 的错误提示,并且显示 "git@62.234.13.38's password" 时,这通常意味着你需要提供正确的密码才能进行身份验证。请确保你输入的密码是正确的,并且你具有相应的权限来访问该资源。如果问题仍然存在,请检查你的网络连接是否正常,或者联系服务器管理员以获取更多帮助。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [npm安装vue-element-admin时遇到git.EXE ls-remote -h -t ssh://git@github.com错误error code: 128](https://blog.csdn.net/qq_27361945/article/details/123739009)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [自己建仓库并同步部署到宝塔,后期报错:Unable to create /path /.git/index.lock : Permission denied](https://blog.csdn.net/kfgauss/article/details/109639226)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值