使用 Git 时如何解决 Permission denied (publickey) 错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2643502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 08:15:00  来源:igfitidea点击:

How to solve Permission denied (publickey) error when using Git?

gitsshssh-keyspublic-key

提问by teepusink

I'm on Mac Snow Leopard and I just installed git.

我在 Mac Snow Leopard 上,我刚刚安装了git.

I just tried

我刚试过

git clone [email protected]:cakebook.git

but that gives me this error:

但这给了我这个错误:

Initialized empty Git repository in `/Users/username/Documents/cakebook/.git/`
Permission denied (publickey).
fatal: The remote end hung up unexpectedly

What am I missing?
I've also tried doing ssh-keygenwith no passphase but still same error.

我错过了什么?
我也试过ssh-keygen没有密码,但仍然是同样的错误。

回答by Rufinus

If the user has not generated a ssh public/private key pair set before

如果用户之前没有生成 ssh 公钥/私钥对

This info is working on theChaw but can be applied to all other git repositories which support SSH pubkey authentications. (See gitolite, gitlab or github for example.)

此信息适用于 theChaw,但可以应用于所有其他支持 SSH 公钥身份验证的 git 存储库。(例如,请参见gitolite、 gitlab 或 github。)

First start by setting up your own public/private key pair set. This can use either DSA or RSA, so basically any key you setup will work. On most systems you can use ssh-keygen.

  • First you'll want to cd into your .ssh directory. Open up the terminal and run:

    cd ~/.ssh && ssh-keygen

  • Next you need to copy this to your clipboard.
    • On OS X run: cat id_rsa.pub | pbcopy
    • On Linux run: cat id_rsa.pub | xclip
    • On Windows (via Cygwin/Git Bash) run: cat id_rsa.pub | clip
  • Add your key to your account via the website.
  • Finally setup your .gitconfig.
    • git config --global user.name "bob"
    • git config --global user.email bob@...(don't forget to restart your command line to make sure the config is reloaded)

That's it you should be good to clone and checkout.

首先设置您自己的公钥/私钥对集。这可以使用 DSA 或 RSA,因此基本上您设置的任何密钥都可以使用。在大多数系统上,您可以使用 ssh-keygen。

  • 首先,您需要 cd 进入您的 .ssh 目录。打开终端并运行:

    cd ~/.ssh && ssh-keygen

  • 接下来,您需要将其复制到剪贴板。
    • 在 OS X 上运行: cat id_rsa.pub | pbcopy
    • 在 Linux 上运行: cat id_rsa.pub | xclip
    • 在 Windows 上(通过 Cygwin/Git Bash)运行: cat id_rsa.pub | clip
  • 通过网站将您的密钥添加到您的帐户。
  • 最后设置你的 .gitconfig。
    • git config --global user.name "bob"
    • git config --global user.email bob@...(不要忘记重新启动命令行以确保重新加载配置)

这就是你应该克隆和结帐的好方法。

Further information can be found at https://help.github.com/articles/generating-ssh-keys(thanks to @Lee Whitney) -

更多信息可以在https://help.github.com/articles/generating-ssh-keys找到(感谢@Lee Whitney) -

If the user has generated a ssh public/private key pair set before

如果用户之前已经生成了 ssh 公钥/私钥对

  • check which key have been authorized on your github or gitlab account settings
  • determine which corresponding private key must be associated from your local computer
  • 在你的 github 或 gitlab 账户设置中检查哪个 key 已经被授权
  • 确定必须从本地计算机关联哪个对应的私钥

eval $(ssh-agent -s)

eval $(ssh-agent -s)

  • define where the keys are located
  • 定义密钥所在的位置

ssh-add ~/.ssh/id_rsa

ssh-add ~/.ssh/id_rsa

回答by stevek

More extensive troubleshooting and even automated fixing can be done with:

可以通过以下方式完成更广泛的故障排除甚至自动修复:

ssh -vT [email protected]

Source: https://help.github.com/articles/error-permission-denied-publickey/

来源:https: //help.github.com/articles/error-permission-denied-publickey/

回答by kavinyao

This error can happen when you are accessing the SSH URL (Read/Write) instead of Git Read-Only URL but you have no write access to that repo.

当您访问 SSH URL(读/写)而不是 Git 只读 URL 但您没有对该存储库的写访问权限时,可能会发生此错误。

Sometimes you just want to clone your own repo, e.g. deploy to a server. In this case you actually only need READ-ONLY access. But since that's your ownrepo, GitHub may display SSH URL if that's your preference. In this situation, if your remote host's public key is not in your GitHub SSH Keys, your access will be denied, which is expected to happen.

有时您只想克隆自己的存储库,例如部署到服务器。在这种情况下,您实际上只需要只读访问权限。但由于这是您自己的存储库,如果您愿意,GitHub 可能会显示 SSH URL。在这种情况下,如果您的远程主机的公钥不在您的 GitHub SSH 密钥中,您的访问将被拒绝,这在预料之中

An equivalent case is when you try cloning someone else's repo to which you have no write access with SSH URL.

一种等效的情况是,当您尝试克隆其他人的存储库时,您对 SSH URL 没有写访问权限。

In a word, if your intent is to clone-only a repo, use HTTPS URL(https://github.com/{user_name}/{project_name}.git) instead of SSH URL ([email protected]:{user_name}/{project_name}.git), which avoids (unnecessary) public key validation.

总之,如果您的意图是仅克隆一个 repo,请使用 HTTPS URL( https://github.com/{user_name}/{project_name}.git) 而不是 SSH URL ( [email protected]:{user_name}/{project_name}.git),这样可以避免(不必要的)公钥验证。



Update: GitHub is displaying HTTPS as the default protocol now and this move can probably reduce possible misuse of SSH URLs.

更新:GitHub 现在将 HTTPS 显示为默认协议,这一举措可能可以减少滥用 SSH URL 的可能性。

回答by jarora

The github help linkhelped me sort out this problem. Looks like the ssh key was not added to the ssh-agent. This is what i ended up doing.

github 帮助链接帮我解决了这个问题。看起来 ssh 密钥没有添加到 ssh-agent 中。这就是我最终要做的。

Command 1:

命令 1:

Ensure ssh-agent is enabled. The command starts the ssh-agent in the background:

确保 ssh-agent 已启用。该命令在后台启动 ssh-agent:

eval "$(ssh-agent -s)"

Command 2:

命令 2:

Add your SSH key to the ssh-agent:

将您的 SSH 密钥添加到 ssh-agent:

ssh-add ~/.ssh/id_rsa

回答by Robina Li

Got same error report.

得到同样的错误报告。

Fixed with using HTTP instead. Since I don't want set "SSH keys" for a test PC.

使用 HTTP 来修复。因为我不想为测试 PC 设置“SSH 密钥”。

Change URL to HTTP when clone:

克隆时将 URL 更改为 HTTP:

git clone https://github.com/USERNAME/REPOSITORY.git

My problem is a little bit different: I have URL set when adding a existing local repo to remote, by using:

我的问题有点不同:我在将现有的本地存储库添加到远程时设置了 URL,使用:

git remote add origin ssh://github.com/USERNAME/REPOSITORY.git

To fix it, reset URL to HTTP:

要修复它,请将 URL 重置为 HTTP:

git remote set-url origin https://github.com/USERNAME/REPOSITORY.git

BTW, you may check your URL using command:

顺便说一句,您可以使用以下命令检查您的 URL:

git remote -v
origin  https://github.com/USERNAME/REPOSITORY.git (fetch)
origin  https://github.com/USERNAME/REPOSITORY.git (push)

Hope this will help some one like me. :D

希望这会帮助像我这样的人。:D

回答by Wouter Schoofs

This works for me:

这对我有用:

ssh-add ~/.ssh/id_rsa

回答by Mason Bryant

Note that (at least for some projects) you must have a github account with an ssh key.

请注意(至少对于某些项目而言)您必须拥有一个带有 ssh 密钥的 github 帐户

Look at the keys listed in your authentication agent (ssh-add -l)
(if you don't see any, add one of your existing keys with ssh-add /path/to/your/key (eg: ssh-add ~/.ssh/id_rsa))
(if you don't have any keys, first create one. See: http://rcsg-gsir.imsb-dsgi.nrc-cnrc.gc.ca/documents/internet/node31.htmlor just google ssh-keygen)

查看您的身份验证代理 ( ssh-add -l) 中列出的密钥
(如果您没有看到任何密钥,请使用 ssh-add /path/to/your/key 添加现有密钥之一(例如:ssh-add ~ /.ssh/id_rsa))
(如果你没有任何密钥,先创建一个。见:http: //rcsg-gsir.imsb-dsgi.nrc-cnrc.gc.ca/documents/internet/node31.html或者只是谷歌 ssh-keygen)

To verify that you have a key associated with your github account:

要验证您是否拥有与 github 帐户关联的密钥

Go to: https://github.com/settings/ssh

转到:https: //github.com/settings/ssh

You should see at least one key with a hash key matching one of the hashes you saw when you typed ssh-add -ljust a minute ago.

您应该至少看到一个键,其哈希键与您在一分钟前键入ssh-add -l时看到的一个哈希值相匹配。

If you don't, add one, then try again.

如果没有,请添加一个,然后再试一次。

回答by Zeeshan Shabbir

I was struggling with same problem that's what i did and i was able clone the repo. I followed these procedure for iMac.

我正在努力解决同样的问题,这就是我所做的,我能够克隆回购。我为iMac遵循了这些程序。

First Step : Checking if we already have the public SSH key.

第一步:检查我们是否已经拥有公共 SSH 密钥。

  1. Open Terminal.
  2. Enter ls -al ~/.sshto see if existing SSH keys are present:
  1. 打开终端。
  2. 输入ls -al ~/.ssh以查看是否存在现有的 SSH 密钥:

Check the directory listing to see if you already have a public SSH key.Default public are one of the following d_dsa.pub,id_ecdsa.pub,id_ed25519.pub,id_rsa.pub

检查目录列表以查看您是否已经拥有公共 SSH 密钥。默认公共是以下 d_dsa.pub、id_ecdsa.pub、id_ed25519.pub、id_rsa.pub 之一

If you don't find then go to step 2 otherwise follow step 3

如果没有找到,请转到第 2 步,否则请执行第 3 步

Step 2 : Generating public SSH key

第 2 步:生成公共 SSH 密钥

  1. Open Terminal.
  2. Enter followong command with you valid email address that you use for github ssh-keygen -t rsa -b 4096 -C "[email protected]"
  3. You will see following in terminal Generating public/private rsa key pair. When it prompts to"Enter a file in which to save the key,"press Enter. This accepts the default file location. When it prompts to Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]Just press enter again. At the prompt, type a secure passphrase.
  4. Enter passphrase (empty for no passphrase): [Type a passphrase]press enter if you don't want to Enter same passphrase again: [Type passphrase again]press enter again
  1. 打开终端。
  2. 使用您用于 github 的有效电子邮件地址输入 followong 命令 ssh-keygen -t rsa -b 4096 -C "[email protected]"
  3. 您将在终端中看到以下内容Generating public/private rsa key pair。当它提示"Enter a file in which to save the key,"按 Enter 时。这接受默认文件位置。当它提示Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]再次按回车键时。在提示下,键入安全密码。
  4. Enter passphrase (empty for no passphrase): [Type a passphrase]如果您不想Enter same passphrase again: [Type passphrase again]再次按 Enter ,请按 Enter

This will generate id_rsa.pub

这将产生 id_rsa.pub

Step 3: Adding your SSH key to the ssh-agent

第 3 步:将您的 SSH 密钥添加到 ssh-agent

  1. Interminal type eval "$(ssh-agent -s)"
  2. Add your SSH key to the ssh-agent. If you are using an existing SSH key rather than generating a new SSH key, you'll need to replace id_rsa in the command with the name of your existing private key file. Enter this command $ ssh-add -K ~/.ssh/id_rsa
  1. 终端类型 eval "$(ssh-agent -s)"
  2. 将您的 SSH 密钥添加到 ssh-agent。如果您使用现有的 SSH 密钥而不是生成新的 SSH 密钥,则需要将命令中的 id_rsa 替换为现有私钥文件的名称。输入这个命令$ ssh-add -K ~/.ssh/id_rsa

Now copy the SSH key and also add it to you github account

现在复制 SSH 密钥并将其添加到您的 github 帐户

  1. In terminal enter this command with your ssh file name pbcopy < ~/.ssh/id_rsa.pubThis will copy the file to your clipboard Now open you github account Go to Settings > SSH and GPG keys > New SSH keyEnter title and paste the key from clipboard and save it. Voila you're done.
  1. 在终端中输入带有您的 ssh 文件名的命令pbcopy < ~/.ssh/id_rsa.pub这会将文件复制到您的剪贴板 现在打开您的 github 帐户转到设置 > SSH 和 GPG 密钥 > 新 SSH 密钥输入标题并从剪贴板粘贴密钥并保存它。瞧,你完成了。

回答by William Hu

I met the same issue because of I was thought the difference between SSHand HTTPSis

我遇到了同样的问题,因为我认为SSHHTTPS之间的区别是

https://github.com/USERNAME/REPOSITORY.git

https://github.com/USERNAME/REPOSITORY.git

ssh://github.com/USERNAME/REPOSITORY.git

ssh://github.com/USERNAME/REPOSITORY.git

So I changed from HTTPSto SSHjust by changing https://to ssh://nothing on the end of the url was changed.

所以,我从改为HTTPSSSH只是通过改变https://ssh://没事就网址的结尾被改变。

But the truth is:

但事实是:

https://github.com/USERNAME/REPOSITORY.git

[email protected]:USERNAME/REPOSITORY.git

Which means I changed ssh://github.com/USERNAME/REPOSITORY.gitto [email protected]:USERNAME/REPOSITORY.gitit works.

这意味着我改变ssh://github.com/USERNAME/REPOSITORY.git[email protected]:USERNAME/REPOSITORY.git它的工作原理。

Stupid error but hope helps someone!

愚蠢的错误,但希望对某人有所帮助!

回答by Richipal

I had a slight different situation, I was logged on to a remote server and was using git on the server, when I ran any git command I got the same message

我的情况略有不同,我登录到远程服务器并在服务器上使用 git,当我运行任何 git 命令时,我得到了相同的消息

   Permission denied (publickey).
   fatal: The remote end hung up unexpectedly

The way I fixed it was by changing the file /etc/ssh_config on my Mac. from

我修复它的方法是更改​​我的 Mac 上的文件 /etc/ssh_config。从

ForwardAgent no 

to

ForwardAgent yes