git GitHub 错误消息 - 权限被拒绝(公钥)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12940626/
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 14:47:30  来源:igfitidea点击:

GitHub Error Message - Permission denied (publickey)

gitgithub

提问by webwrks

Anybody seen this error and know what to do?

有没有人看到这个错误并知道该怎么做?

I'm using the terminal, I'm in the root, the GitHub repository exists and I don't know what to do now.

我正在使用终端,我在根目录中,GitHub 存储库存在,但我现在不知道该怎么做。

> git push -u origin master
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

采纳答案by bdukes

GitHub isn't able to authenticate you. So, either you aren't setup with an SSH key, because you haven't set one up on your machine, or your key isn't associated with your GitHub account.

GitHub 无法对您进行身份验证。所以,要么你没有设置 SSH 密钥,因为你没有在你的机器上设置一个,或者你的密钥没有与你的 GitHub 帐户相关联。

You can also use the HTTPS URL instead of the SSH/git URL to avoid having to deal with SSH keys. This is GitHub's recommended method.

您还可以使用 HTTPS URL 而不是 SSH/git URL 以避免必须处理 SSH 密钥。这是GitHub 推荐的方法

Further, GitHub has a help page specifically for that error message, and explains in more detail everything you could check.

此外,GitHub 有一个专门针对该错误消息帮助页面,并更详细地解释了您可以检查的所有内容。

回答by IgorGanapolsky

Did you create a configfile in your ~/.ssh directory? It should have contents like these:

您是否在 ~/.ssh 目录中创建了配置文件?它应该有这样的内容:

Host github.com 
 IdentityFile ~/.ssh/github_rsa

Assuming that you created an ssh key named github_rsa

假设您创建了一个名为github_rsa的 ssh 密钥

and uploaded it to GitHub...

并将其上传到 GitHub ...

NOTE: You must follow this way of explicit configuration if you have more than 1 key (2 and more) in your ~/.ssh/ directory. If you don't specify key this way, then first key in order is taken and used for github authentication, so it depends on the key file name then.

注意:如果您的 ~/.ssh/ 目录中有超过 1 个密钥(2 个或更多),您必须遵循这种显式配置方式。如果您不以这种方式指定密钥,则按顺序获取第一个密钥并用于 github 身份验证,因此它取决于密钥文件名。

回答by VelikiiNehochuha

I know this problem. After add ssh key, add you ssh key to ssh agent too (from oficial docs https://help.github.com/articles/generating-ssh-keys/)

我知道这个问题。添加 ssh 密钥后,也将 ssh 密钥添加到 ssh 代理(来自官方文档https://help.github.com/articles/generating-ssh-keys/

ssh-agent -s
ssh-add ~/.ssh/id_rsa

After it, all work fine, git can view proper key, before coudn't.

之后,一切正常,git可以查看正确的密钥,之前不能。

回答by cdhowie

You need to generate an SSH key (if you don't have one) and associate the public key with your Github account. See Github's own documentation.

您需要生成一个 SSH 密钥(如果您没有)并将公钥与您的 Github 帐户相关联。请参阅Github 自己的文档

回答by rmundo

This happened to me. For some reason my origin got messed up without my realizing it:

这发生在我身上。出于某种原因,我的起源在我没有意识到的情况下搞砸了:

Check if your settings are still correct

检查您的设置是否仍然正确

git remote -v

the url needs to be something like ssh://[email protected]/YourDirectory/YourProject.git; if you don't see [email protected], use

url 需要类似于 ssh://[email protected]/YourDirectory/YourProject.git; 如果您没有看到 [email protected],请使用

git remote set-url origin git://github.com/YourDirectory/YourProject.git

to set it right. Or you could use the github app to check and set the Primary Remote Repository url in the settings panel of your particular repository.

设置正确。或者,您可以使用 github 应用程序在特定存储库的设置面板中检查和设置主远程存储库 URL。

回答by d1jhoni1b

First, we need to check for existing ssh keys on your computer. Open up Terminal and run:

首先,我们需要检查您计算机上现有的 ssh 密钥。打开终端并运行:

ls -al ~/.ssh

#or

cd ~/.ssh
ls

and that will lists the files in your .ssh directory

这将列出 .ssh 目录中的文件

And finally depending on what you see (in my case was):

最后取决于你所看到的(在我的情况下是):

 github_rsa  github_rsa.pub known_hosts

Just try setting up your RSA and hopefully that will solve your "git push origin" issues

只需尝试设置您的 RSA,希望这将解决您的“git push origin”问题

$ ssh-keygen -lf ~/.ssh/github_rsa.pub

$ ssh-keygen -lf ~/.ssh/github_rsa.pub

NOTE:RSA certificates are keys-paired so you will have a private and a public certificate, private will not be accessible for you since it belongs to github (in this case) but the public is the one you might be missing when this error happens (at least that was my case, my github account or repo got messed up somehow and i had to "link" the public key, previously generated)

注意:RSA 证书是密钥配对的,因此您将拥有一个私有证书和一个公共证书,您将无法访问私有证书,因为它属于 github(在这种情况下),但是当发生此错误时,您可能会丢失公共证书(至少这是我的情况,我的 github 帐户或 repo 不知何故搞砸了,我不得不“链接”以前生成的公钥)

回答by xinerd

Assuming you are connecting GitHub over SSH, you can run below command to confirm this.

假设您通过 SSH 连接 GitHub,您可以运行以下命令来确认这一点。

$git config --get remote.origin.url

If you get a result has following format [email protected]:xxx/xxx.github.com.git, then you should do the following.

如果你得到的结果格式为 [email protected]:xxx/xxx.github.com.git,那么你应该执行以下操作。

Generate a SSH key(or use existing one). if you had one, you just need to add your key to the ssh-agent (step 2)and to your GitHub account(step 3).

生成 SSH 密钥(或使用现有密钥)。如果你有一个,你只需要将你的密钥添加到 ssh-agent(第 2 步)和你的 GitHub 帐户(第 3 步)。

below are for those who don't have SSH key.

下面是那些没有 SSH 密钥的人。

Step 1Generating public/private rsa key pair.

步骤 1生成公/私 rsa 密钥对。

$ssh-keygen -t rsa -b 4096 -C "[email protected]"

You'll be asked to confirm where to save the SSH key and what passphrase you want to use.

系统会要求您确认保存 SSH 密钥的位置以及要使用的密码。

Step 2Add your key to the ssh-agent

步骤 2将您的密钥添加到 ssh-agent

  • Ensure ssh-agent is enabled

    $eval "$(ssh-agent -s)"

  • Add your SSH key to the ssh-agent:

    $ssh-add ~/.ssh/id_rsa

  • 确保 ssh-agent 已启用

    $eval "$(ssh-agent -s)"

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

    $ssh-add ~/.ssh/id_rsa

Step 3Add your SSH key to your account

第 3 步将您的 SSH 密钥添加到您的帐户

$sudo apt-get install xclip

$sudo apt-get install xclip

$xclip -sel clip < ~/.ssh/id_rsa.pub

$xclip -sel clip < ~/.ssh/id_rsa.pub

Then add the copied key to GitHub

然后将复制的key添加到GitHub

Go to Settings->SSH keys(Personal settings side bar)->Add SSH key->fill out form(key is on your clipboard, just use ctrl+v)->Add key

转到设置-> SSH 密钥(个人设置侧栏)->添加 SSH 密钥-> 填写表单(密钥在剪贴板上,只需使用 ctrl+v)->添加密钥

After going through above steps, you should solve the permission problem.

完成上述步骤后,您应该解决权限问题。

Reference Link: Generating SSH keys.

参考链接: 生成 SSH 密钥

回答by jfunk

I was getting this error. Turns out I had just upgraded OSX to Sierra and my old key was no longer registered.

我收到这个错误。结果我刚刚将 OSX 升级到 Sierra 并且我的旧密钥不再注册。

At first I thought it was "Upgrading to macOS Sierra will break your SSH keys and lock you out of your own servers"

起初我以为是“升级到 macOS Sierra 会破坏您的 SSH 密钥并将您锁定在您自己的服务器之外”

But I had sidestepped that one. Turns out I just had to re-register my existing key:

但我避开了那个。结果我只需要重新注册我现有的密钥:

ssh-add -K

ssh-add -K

And type the passphrase... done!

然后输入密码...完成!

回答by Badr Bellaj

Another solution :

另一种解决方案:

create the SSH keys, type ssh-keygen -t rsa -C "[email protected]". This will create both id_rsa and id_rsa.pub files.

创建 SSH 密钥,键入ssh-keygen -t rsa -C "[email protected]". 这将创建 id_rsa 和 id_rsa.pub 文件。

Add the id_rsa to ssh list on local computer: ssh-add ~/.ssh/id_rsa.

将 id_rsa 添加到本地计算机上的 ssh 列表: ssh-add ~/.ssh/id_rsa.

After generating the keys get the pubkey using :

生成密钥后,使用以下方法获取公钥:

cat ~/.ssh/id_rsa.pub 

you will get something like :

你会得到类似的东西:

cat ~/.ssh/id_rsa.pub 

ssh-rsa AAAB3NzaC1yc2EAAAADAQABAAACAQCvMzmFEUPvaA1AFEBH6zGIF3N6pVE2SJv9V1MHgEwk4C7xovdk7Lr4LDoqEcqxgeJftwWQWWVrWWf7q9qCdHTAanH2Q5vx5nZjLB+B7saksehVOPWDR/MOSpVcr5bwIjf8dc8u5S8h24uBlguGkX+4lFJ+zwhiuwJlhykMvs5py1gD2hy+hvOs1Y17JPWhVVesGV3tlmtbfVolEiv9KShgkk3Hq56fyl+QmPzX1jya4TIC3k55FTzwRWBd+IpblbrGlrIBS6hvpHQpgUs47nSHLEHTn0Xmn6Q== [email protected]

copy this key (value) and go to github.com and under the setting (ssh and pgp key) add your public key.

复制此密钥(值)并转到 github.com 并在设置(ssh 和 pgp 密钥)下添加您的公钥。

回答by N. Osil

In case you are not accessing your own repository, or cloning inside a cloned repository (using some "git submodule... " commands):

如果您没有访问自己的存储库,或在克隆存储库中进行克隆(使用一些“git submodule...”命令):

In the home directory of your repository:

在存储库的主目录中:

$ ls -a

1. Open ".gitmodules",and you will find something like this:

1. 打开“.gitmodules”,你会发现是这样的:

[submodule "XXX"]
    path = XXX
    url = [email protected]:YYY/XXX.git

Change the last line to be the HTTPSof the repository you need to pull:

将最后一行更改为您需要拉取的存储库的HTTPS

[submodule "XXX"]
    path = XXX
    https://github.com/YYY/XXX.git

Save ".gitmodules", and runthe command for submodules, and ".git" will be updated.

保存 ".gitmodules",并为子模块运行命令,".git" 将被更新。

2. Open ".git",go to "config" file, and you will find something like this:

2. 打开“.git”,进入“config”文件,你会发现如下内容:

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = true
[remote "origin"]
    url = https://github.com/YYY/XXX.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master
[submodule "XXX"]
    url = [email protected]:YYY/XXX.git

Change the last line to be the HTTPSof the repository you need to pull:

将最后一行更改为您需要拉取的存储库的HTTPS

    url = https://github.com/YYY/XXX.git

So, in this case, the main problem is simply with the url. HTTPS of any repository can be found now on top of the repository page.

因此,在这种情况下,主要问题仅在于 url。现在可以在存储库页面的顶部找到任何存储库的 HTTPS。