简体   繁体   English

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

[英]GitHub Error Message - Permission denied (publickey)

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.

GitHub isn't able to authenticate you. GitHub 无法对您进行身份验证。 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.所以,要么你没有设置 SSH 密钥,因为你没有在你的机器上设置一个,要么你的密钥没有与你的 GitHub 帐户相关联。

You can also use the HTTPS URL instead of the SSH/git URL to avoid having to deal with SSH keys.您还可以使用 HTTPS URL 而不是 SSH/git URL,以避免处理 SSH 密钥。 This is GitHub's recommended method .这是GitHub 推荐的方法

Further, GitHub has a help page specifically for that error message , and explains in more detail everything you could check.此外,GitHub 有一个专门针对该错误消息的帮助页面,并更详细地解释了您可以检查的所有内容。

I know about this problem.我知道这个问题。 After add ssh key, add you ssh key to ssh agent too (from official docs )添加 ssh 密钥后,也将 ssh 密钥添加到 ssh 代理(来自官方文档

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

After it all work fine, git can view proper key, before couldn't.一切正常后,git可以查看正确的密钥,之前不能。

Did you create a config file in your ~/.ssh directory?您是否在 ~/.ssh 目录中创建了配置文件? It should have contents like these:它应该有如下内容:

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

You need to generate an SSH key (if you don't have one) and associate the public key with your Github account.您需要生成一个 SSH 密钥(如果您没有)并将公钥与您的 Github 帐户相关联。 See Github's own documentation .请参阅Github 自己的文档

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://git@github.com/YourDirectory/YourProject.git; url 需要类似于 ssh://git@github.com/YourDirectory/YourProject.git; if you don't see git@github.com, use如果您没有看到 git@github.com,请使用

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。

Issue solved if you change the ssh access to https access to the remote repository:如果您将 ssh 访问更改为对远程存储库的 https 访问,则问题已解决:

git remote set-url origin https_link_to_repository

git push -u origin master

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 git@github.com:xxx/xxx.github.com.git, then you should do the following.如果您得到的结果具有以下格式 git@github.com:xxx/xxx.github.com.git,那么您应该执行以下操作。

Generate a SSH key(or use existing one).生成一个 SSH 密钥(或使用现有的)。 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-agent(步骤 2)和您的 GitHub 帐户(步骤 3)。

below are for those who don't have SSH key.以下适用于那些没有 SSH 密钥的人。

Step 1 Generating public/private rsa key pair.步骤 1生成公钥/私钥 rsa 密钥对。

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

You'll be asked to confirm where to save the SSH key and what passphrase you want to use.系统将要求您确认 SSH 密钥的保存位置以及要使用的密码。

Step 2 Add your key to the ssh-agent步骤 2将您的密钥添加到 ssh-agent

  • Ensure ssh-agent is enabled确保 ssh-agent 已启用

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

  • Add your SSH key to the ssh-agent:将 SSH 密钥添加到 ssh-agent:

    $ssh-add ~/.ssh/id_rsa

Step 3 Add your SSH key to your account步骤 3将您的 SSH 密钥添加到您的帐户

$sudo apt-get install xclip

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

Then add the copied key to GitHub然后将复制的密钥添加到 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 密钥

Another solution :另一个解决方案:

create the SSH keys, type ssh-keygen -t rsa -C "your_email@example.com" .创建 SSH 密钥,输入ssh-keygen -t rsa -C "your_email@example.com" This will create both id_rsa and id_rsa.pub files.这将创建 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 :生成密钥后,使用以下命令获取 pubkey:

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== user@email.com

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 密钥)下添加您的公钥。

First, we need to check for existing ssh keys on your computer.首先,我们需要检查您计算机上现有的 ssh 密钥。 Open up Terminal and run:打开终端并运行:

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

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 不知何故搞砸了,我不得不“链接”以前生成的公钥)

this worked for me:这对我有用:

1- remove all origins 1-删除所有来源

git remote rm origin  

(cf. https://www.kernel.org/pub/software/scm/git/docs/git-remote.html ) (参见https://www.kernel.org/pub/software/scm/git/docs/git-remote.html

*remote : "Manage the set of repositories ("remotes") whose branches you track. *remote :“管理您跟踪其分支的存储库集(“远程”)。

*rm : "Remove the remote named . All remote-tracking branches and configuration settings for the remote are removed." *rm : "删除名为 . 的远程。远程的所有远程跟踪分支和配置设置都被删除。"

2- check all has been removed : 2-检查所有已被删除:

git remote -v  

3- add new origin master 3-添加新的原点主

git remote add origin git@github.com:YOUR-GIT/YOUR-REPO.git

that's all folks!就是这样!

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

And type the passphrase... done!然后输入密码……完成!

I think i have the best answer for you, your git apps read your id_rsa.pub in root user directory我想我有最好的答案给你,你的 git 应用程序在根用户目录中读取你的 id_rsa.pub

/home/root/.ssh/id_rsa.pub

That's why your key in /home/your_username/.ssh/id_rsa.pub can't be read by git.这就是为什么 git 无法读取 /home/your_username/.ssh/id_rsa.pub 中的密钥的原因。 So you need to create the key in /home/root/.ssh/所以你需要在 /home/root/.ssh/ 中创建密钥

$ sudo su
$ ssh-keygen
$ cd ~/.ssh
$ cat id_rsa.pub

Then copy the key in your github account.然后将密钥复制到您的 github 帐户中。 It's worked for me.它对我有用。 You can try it.你可以试试。

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 = git@github.com:YYY/XXX.git

Change the last line to be the HTTPS of the repository you need to pull:将最后一行更改为您需要拉取的存储库的HTTPS

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

Save ".gitmodules" , and run the 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 = git@github.com:YYY/XXX.git

Change the last line to be the HTTPS of 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.因此,在这种情况下,主要问题只是 url。 HTTPS of any repository can be found now on top of the repository page.现在可以在存储库页面顶部找到任何存储库的 HTTPS。

Make sure ssh-add -l shows a fingerprint of an SSH key that's present in the list of SSH keys in your Github account.确保ssh-add -l显示 SSH 密钥的指纹,该指纹出现在您的 Github 帐户的 SSH 密钥列表中。

If the output is empty, but you know you have a private SSH key that works with your github account, run ssh-add on this key (found in ~/.ssh . It's named id_rsa by default, so you'll likely run ssh-add id_rsa ).如果输出为空,但您知道您有一个与您的 github 帐户一起使用的私有 SSH 密钥,请在此密钥上运行ssh-add (在~/.ssh中找到。它默认命名为id_rsa ,因此您可能会运行ssh-add id_rsa - ssh-add id_rsa )。

Else, follow these instructions to generate an SSH key pair .否则,请按照这些说明生成 SSH 密钥对。

I was using github earlier for one of my php project.我之前在我的一个 php 项目中使用了 github。 While using github, I was using ssh instead of https.在使用 github 时,我使用的是 ssh 而不是 https。 I had my machine set up like that and every time I used to commit and push the code, it would ask me my rsa key password.我的机器是这样设置的,每次我提交和推送代码时,它都会询问我的 rsa 密钥密码。

After some days, I stopped working on the php project and forgot my rsa password.几天后,我停止了 php 项目的工作并忘记了我的 rsa 密码。 Recently, I started working on a java project and moved to bitbucket.最近,我开始从事一个 java 项目并转移到 bitbucket。 Since, I had forgotten the password and there is no way to recover it I guess, I decided to use the https(recommended) protocol for the new project and got the same error asked in the question.因为,我忘记了密码并且我想没有办法恢复它,所以我决定对新项目使用 https(推荐)协议,并在问题中遇到了同样的错误。

How I solved it?我是怎么解决的?

  1. Ran this command to tell my git to use https instead of ssh:运行此命令来告诉我的 git 使用 https 而不是 ssh:

     git config --global url."https://".insteadOf git://
  2. Remove any remote if any删除任何遥控器(如果有)

     git remote rm origin
  3. Redo everything from git init to git push and it works!重做从git initgit push的所有操作,它就可以工作了!

PS: I also un-installed ssh from my machine during the debug process thinking that, removing it will fix the problem. PS:我还在调试过程中从我的机器上卸载了 ssh,认为删除它可以解决问题。 Yes I know!!是的,我知道!! :) :)

Yes I too had this question :/ I was going to push my project to Github in HTTP type(not in SSH type).是的,我也有这个问题:/ 我打算以 HTTP 类型(不是 SSH 类型)将我的项目推送到 Github。 I had to enter my username and password in each push.我必须在每次推送中输入我的用户名和密码。 So first I entered code relevant to below type所以首先我输入了与以下类型相关的代码

git remote add origin git@github.com:YOUR-GIT/YOUR-REPO.git

and I got我得到了

git@github.com: Permission denied (publickey). git@github.com:权限被拒绝(公钥)。 fatal: Could not read from remote repository.致命:无法从远程存储库中读取。

So however I solved my problems by doing below methods所以我通过以下方法解决了我的问题

  1. git remote rm origin remove your git remote repository git remote rm origin删除你的 git 远程仓库
  2. git remote now check whether your remote remote repository have been removed git remote现在检查您的远程远程存储库是否已被删除

URL = https://github.com/<username>/<repository>.git URL = https://github.com/<username>/<repository>.git <用户名>/<存储库>.git

  1. git remote add origin URL Again add your URL to make remote repository git remote add origin URL再次添加您的 URL 以创建远程存储库
  2. git push -u origin master
    You can push your content to remote repository.您可以将内容推送到远程存储库。 In here as you use "-u" with the command you make a tracking branch and with the help of tracking branch in the remote repository you won't to tell git what branch you push in the next steps :)在这里,当您使用“-u”命令创建一个跟踪分支并在远程存储库中跟踪分支的帮助下,您不会告诉 git 您在接下来的步骤中推送哪个分支:)
    And here if you use linux git will ask username and password before push the content.在这里,如果你使用 linux git 会在推送内容之前询问用户名和密码。 And give your relevant credentials.并提供您的相关凭据。

For prevent to give username and password in each push you have to change git config为了防止在每次推送中提供用户名和密码,您必须更改 git config

For list your git config - git config --global --list列出你的 git config - git config --global --list
you will see你会看见

user.name=<your_username>
user.email=<your_email>

So you have to add credential.helper attribute to your git config to this因此,您必须将credential.helper属性添加到您的 git 配置中

5. git config --global --add credential.helper store add this to your terminal Now you can add new item to your files then git add. 5. git config --global --add credential.helper store将此添加到您的终端现在您可以将新项目添加到您的文件中,然后git add. , git commit -m "<message>" , git push , git commit -m "<message>" , git push
And now too there git will request your username and password and that will be the last time.现在 git 也会请求你的用户名和密码,这将是最后一次。 And for next git pushes git won't request username and password from you :)对于下一个 git pushes,git 不会向你请求用户名和密码 :)

OK there are few solutions to this one, some of them might already been mentioned but just to keep them together:好的,这个解决方案很少,其中一些可能已经被提及,但只是为了将它们放在一起:

  • make sure you keys are present, by default another ~/.ssh/ folder, ie id.rsa and id.rsa.pub确保您的密钥存在,默认情况下另一个 ~/.ssh/ 文件夹,即 id.rsa 和 id.rsa.pub

  • make sure the keys have correct permissions, you can run chmod:确保密钥具有正确的权限,您可以运行 chmod:

    chmod 600 ~/.ssh/id_rsa chmod 600 ~/.ssh/id_rsa

    chmod 644 ~/.ssh/id_rsa.pub chmod 644 ~/.ssh/id_rsa.pub

  • make sure the content of you public key (id_rsa.pub) matches the one uploaded in the remote repository configuration确保您的公钥 (id_rsa.pub) 的内容与远程存储库配置中上传的内容相匹配

  • Finally fix the problems with ssh agent: ssh-add最后修复 ssh 代理的问题: ssh-add

Some more info: https://itcodehub.blogspot.com/2015/01/ssh-add-problems-with-ssh-agent-and.html更多信息: https ://itcodehub.blogspot.com/2015/01/ssh-add-problems-with-ssh-agent-and.html

Allow write access for the key (identity) and then click Add key允许对密钥(身份)进行写访问,然后单击添加密钥

在此处输入图像描述

If on Windows check for more details in Using Github via SSH .如果在 Windows 上,请在通过 SSH 使用 Github中查看更多详细信息。

Solutin for error like this解决这样的错误

git@github.com: Permission denied (public key). git@github.com:权限被拒绝(公钥)。 fatal: Could not read from remote repository.致命:无法从远程存储库中读取。 Please make sure you have the correct access rights请确保您拥有正确的访问权限

Solution (OSX)解决方案 (OSX)

  • Open your terminal and follow below commands打开您的终端并按照以下命令进行操作

  • $ cd ~ $光盘~

  • $ sudo su $须藤苏

  • $ ssh-keygen $ ssh-keygen

    • Enter file in which to save the key (/var/root/.ssh/id_rsa): $ id_rsa输入保存密钥的文件 (/var/root/.ssh/id_rsa): $ id_rsa
    • Enter passphrase (empty for no passphrase): $ hit enter Enter passphrase (empty for no passphrase): $ hit enter
    • Enter same passphrase again: $ hit enter再次输入相同的密码:$回车
  • $ cd ~/.ssh $ cd ~/.ssh

  • $ cat id_rsa.pub $猫id_rsa.pub

  • copy display content.复制显示内容。 在此处输入图像描述

  • Open GitHub click your profile icon settings>SSH and GPC Keys打开 GitHub 单击您的个人资料图标设置>SSH 和 GPC 密钥

  • Click on the new ssh key button单击新的 ssh 密钥按钮在此处输入图像描述

  • enter any title and key that you copied输入您复制的任何标题和密钥在此处输入图像描述

  • check now your issue is resolved现在检查您的问题已解决

If you have already created an SSH key and are still getting the error it is because you need to give the user permissions to read and write to the folder you are cloning into.如果您已经创建了 SSH 密钥并且仍然收到错误,那是因为您需要授予用户权限来读取和写入您要克隆到的文件夹。 To do this, sudo chmod 777 <your_folder_name_here>" . Of course, this is after you have generated an SSH key and you are still getting this error. Hope this helps future users.为此, sudo chmod 777 <your_folder_name_here>" 。当然,这是在您生成 SSH 密钥之后,您仍然会收到此错误。希望这对未来的用户有所帮助。

Edit编辑

To add on to this use admin in Windows if you're using the git bash如果您使用的是 git bash,请在 Windows 中添加到此使用 admin

I would like to add some of my findings:我想补充一些我的发现:

If you are using GitBash , then make sure the SSH key is stored in ~/.ssh/id_rsa .如果您使用的是GitBash ,请确保 SSH 密钥存储在~/.ssh/id_rsa中。

By Default GitBash searches for ~/.ssh/id_rsa as default path for SSH key.默认情况下, GitBash搜索~/.ssh/id_rsa作为 SSH 密钥的默认路径。

Even the file name id_rsa matters.甚至文件名id_rsa也很重要。 If you save your SSH key in another filename or path, it will throw the Permission Denied(publickey) error.如果您将 SSH 密钥保存在另一个文件名或路径中,它将引发Permission Denied(publickey)错误。

I had the same issue recently.我最近有同样的问题。 This might help if you need a fix immediately, but this needs to be done every time you re-start your system如果您需要立即修复,这可能会有所帮助,但每次重新启动系统时都需要这样做

From terminal, run : ssh-add ~/.ssh/id_rsa从终端运行: ssh-add ~/.ssh/id_rsa

Enter your system password and that should work.输入您的系统密码,这应该可以工作。

TLDR: TLDR:

  1. make sure you have write access to the repo (configure it from the repo's settings ).确保您具有对 repo 的write权限(从 repo 的settings配置它)。
  2. make sure the public key is in the SSH and GPG keys of your github account.确保公钥在您的 github 帐户的SSH and GPG keys中。

For me, this error usually occurs when I try to clone some repo from a newly installed machine.对我来说,当我尝试从新安装的机器克隆一些 repo 时,通常会发生此错误。 When receiving a request, github will first check the public key hash. github 收到请求时,首先会检查公钥哈希。 If the public key does not match any user, github will reject this request.如果公钥不匹配任何用户,github 将拒绝此请求。 This case is common if the machine is new and your ssh key is newly generated.如果机器是新机器并且您的 ssh 密钥是新生成的,这种情况很常见。

you can use Https url to login您可以使用 Https url 登录

i guess you are trying to login with ssh url when you say git push if it as asking only password consider you are connecting through ssh.better you use http url.我猜您在说 git push 时尝试使用 ssh url 登录,如果它只询问密码,则认为您是通过 ssh 连接的。最好使用 http url。

Also in ubuntu, even though there was already SSH key entered in settings in BitBucket, I got this problem.同样在 ubuntu 中,即使在 BitBucket 的设置中已经输入了 SSH 密钥,我也遇到了这个问题。 The reason was, I was trying the following:原因是,我正在尝试以下操作:

sudo git push origin master

Not sure why, but it got solved by using不知道为什么,但它通过使用解决了

git push origin master

No sudo used.没有使用 sudo。

For me I tried this -对我来说,我试过这个 -

eval "$(ssh-agent -s)"

then I run然后我跑

ssh-add ~/.ssh/path-to-the-keyfile

and for generating the key you can run并生成您可以运行的密钥

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

this will generate the pair of keys (Public and private).这将生成一对密钥(公共和私人)。

you can store this key to github for more read this Adding a new SSH key to your GitHub account您可以将此密钥存储到 github 以获取更多信息,请阅读将新的 SSH 密钥添加到您的 GitHub 帐户

I hope it will help others :)我希望它会帮助别人:)

I was having a similar problem to @Batman.我遇到了与@Batman 类似的问题。 However, because I was running this under /usr/local/src/projectname, running without sudo was not an option.但是,因为我在 /usr/local/src/projectname 下运行它,所以不使用 sudo 运行不是一个选项。

Just add the -E flag to preseve the environment (your ~/.ssh/ path).只需添加-E 标志来保存环境(您的 ~/.ssh/ 路径)。

$ sudo -E git clone git@your_repo

From man sudo:从人须藤:

-E, --preserve-env Indicates to the security policy that the user wishes to pre‐ serve their existing environment variables. -E, --preserve-env 向安全策略表明用户希望保留他们现有的环境变量。 The security policy may return an error if the user does not have permis‐ sion to preserve the environment.如果用户没有保护环境的权限,安全策略可能会返回错误。

I found this page while searching for a solution to a similar error message using git pull on a remote host:我在远程主机上使用git pull搜索类似错误消息的解决方案时找到了此页面:

$ git pull
Permission denied (publickey).
fatal: Could not read from remote repository.

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

I was connected from my local machine to the remote host via ssh -AY remote_hostname .我通过ssh -AY remote_hostname从本地机器连接到远程主机。 This is not a solution to OP's question, but useful for others who come across this page, so posting it here.这不是 OP 问题的解决方案,但对遇到此页面的其他人很有用,因此在此处发布。

Note that in my case, git pull works fine on my local machine (that is, ssh key had been set up, and added to the GitHub account, etc).请注意,在我的情况下, git pull在我的本地机器上运行良好(也就是说,已设置 ssh 密钥并添加到 GitHub 帐户等)。 I solved my issue by adding this to ~/.ssh/config on my laptop:我通过将其添加到笔记本电脑上的~/.ssh/config解决了我的问题:

Host *
     ForwardAgent yes

I then re-connected to the remote host with ssh -AY remote_hostname , and git pull now worked.然后我用ssh -AY remote_hostname重新连接到远程主机,现在git pull可以工作了。 The change in the config enables to forward my ssh keypair from my local machine to any host.配置中的更改可以将我的 ssh 密钥对从本地机器转发到任何主机。 The -A option to ssh actually forwards it in that ssh session. ssh-A选项实际上在该 ssh 会话中转发它。 See more details here. 在此处查看更多详细信息。

在此处输入图像描述 If you are using Widows-10, follow this instruction.如果您使用的是 Widows-10,请按照此说明进行操作。 It works for me.这个对我有用。

  1. Open terminal as administrator以管理员身份打开终端

  2. run this command: "ssh-keygen".运行这个命令:“ssh-keygen”。 It generate a ssh key and will show the folder where it has been created.它会生成一个 ssh 密钥,并将显示创建它的文件夹。 Check my image检查我的图像在此处输入图像描述

  3. copy the generated "ssh key"复制生成的“ssh key”

  4. go to your github profile ---> settings --> Click SSH and GPH --> Click ""New SSH Key Button" and paste the "ssh key" and finally "clickthe add Button"转到您的 github 配置文件 ---> 设置 --> 单击 SSH 和 GPH --> 单击““新建 SSH 密钥按钮”并粘贴“ssh 密钥”,最后“单击添加按钮”

如果您使用的是 GitHub for Mac UI,请检查首选项以确保您已登录。

I was getting same error during 'git push'.在“git push”期间我遇到了同样的错误。 In client side I had two origin and master.在客户端,我有两个起源和主人。 I removed one, then it worked fine.我删除了一个,然后它工作正常。

Once scenario where this will happen is when you follow GitHub instructions after you create your repository.发生这种情况的一种情况是在创建存储库后按照 GitHub 说明进行操作。 Git will instruct you to add your remote with something like this. Git 将指示您使用类似的东西添加您的遥控器。

git remote add origin git@github.com:<user>/<project>.git

Replace what's in <> with values related to your account.将 <> 中的内容替换为与您的帐户相关的值。

The solution is to remove the .git suffix.解决方法是去掉.git后缀。 Add the remote as follows:添加遥控器如下:

git remote add origin git@github.com:<user>/<project>

tl;dr tl;博士

in ~/.ssh/config put~/.ssh/config

PubkeyAcceptedKeyTypes=+ssh-dss

Scenario If you are using a version of openSSH > 7, like say on a touchbar MacBook Pro it is ssh -V
OpenSSH_7.4p1, LibreSSL 2.5.0
场景如果您使用的是 openSSH > 7 的版本,例如在触控栏 MacBook Pro 上,它是ssh -V
OpenSSH_7.4p1, LibreSSL 2.5.0
ssh -V
OpenSSH_7.4p1, LibreSSL 2.5.0

You also had an older Mac which originally had your key you put onto Github, it's possible that is using an id_dsa key.您还拥有一台旧 Mac,它最初将您的密钥放在 Github 上,这可能是使用 id_dsa 密钥。 OpenSSH v7 doesn't put in by default the use of these DSA keys (which include this ssh-dss ) , but you can still add it back by putting the following code into your ~/.ssh/config OpenSSH v7 默认不使用这些 DSA 密钥(包括这个ssh-dss ),但您仍然可以通过将以下代码放入您的~/.ssh/config来添加它

PubkeyAcceptedKeyTypes=+ssh-dss

Source that worked for me is this Gentoo newsletter对我有用的来源是这个Gentoo 时事通讯

Now you can at least use GitHub and then fix your keys to RSA.现在您至少可以使用 GitHub,然后将您的密钥修复为 RSA。

This Worked for me这对我有用

There are 2 options in github - HTTPS/SSH github 中有2 options - HTTPS/SSH

I had selected SSH by mistake due to which the error was occuring -_- I had selected SSH ,因此发生了错误-_-

Switch it to HTTPS and then copy the url again and try :)将其切换到 HTTPS ,然后再次复制 url 并尝试 :)

I had the same issue but none of the answers worked but a simple solution worked.我有同样的问题,但没有一个答案有效,但一个简单的解决方案有效。 But please note that I did add the ssh key as suggested by the answers first.但请注意,我确实按照答案的建议添加了ssh key Not sure if that really helped.不确定这是否真的有帮助。 Anyway this is what finally worked:无论如何,这最终奏效了:

Change your remote's URL from HTTPS to SSH with the git remote set-url command.使用 git remote set-url 命令将远程的 URL 从 HTTPS 更改为 SSH。 $ git remote set-url origin git@github.com:USERNAME/REPOSITORY.git . $ git remote set-url origin git@github.com:USERNAME/REPOSITORY.git Verify that the remote URL has changed.验证远程 URL 是否已更改。

I also got this problem since I use several GIT accounts and also different ones for work and university, this worked for me:我也遇到了这个问题,因为我使用了几个 GIT 帐户以及不同的工作和大学帐户,这对我有用:

-> go to your ssh config -> 转到您的 ssh 配置

cd ~/.ssh/
vim config

-> copy the following block and fill in the host and user you want to push to/with -> 复制以下块并填写您要推送到/使用的主机和用户

Host [Hostname]
User [User]
PubkeyAcceptedAlgorithms +ssh-rsa
HostkeyAlgorithms +ssh-rsa

I got this problem for a few hostnames so now i have several of those configurations in my ssh config file.我遇到了几个主机名的问题,所以现在我的 ssh 配置文件中有几个这样的配置。

Maybe your ssh-agent is not enable You can try it也许你的 ssh-agent 没有启用你可以试试

  1. Download git下载 git

http://git-scm.com/ http://git-scm.com/

  1. Install it安装它

  2. Enable ssh-agent启用 ssh 代理

C:\Program Files\Git\cmd C:\Program Files\Git\cmd

start-ssh-agent启动 ssh 代理

Go to your GitHub account dashboard, find your project repository, click Settings tab - under Deploy keys you'll have to add your SSH key.转到您的 GitHub 帐户仪表板,找到您的项目存储库,单击设置选项卡 - 在部署密钥下,您必须添加您的 SSH 密钥。 Open Terminal and type:打开终端并输入:

cat ~/.ssh/id_rsa.pub | pbcopy 

This will copy the key from your id_rsa.pub file.这将从您的 id_rsa.pub 文件中复制密钥。 So just go back to GitHub dashboard, paste it, click Add Key and that's it.所以只需返回 GitHub 仪表板,粘贴它,单击Add Key就可以了。

The same solution applies to Bitbucket accounts.相同的解决方案适用于 Bitbucket 帐户。

I had 2 Github identity files loaded in the ssh agent.我在 ssh 代理中加​​载了 2 个 Github 身份文件。 My personal key and another for a client project.我的个人密钥和另一个用于客户项目的密钥。

I removed the key that was causing confusion:我删除了导致混乱的密钥:

ssh-add -d id_rsa_github_somekey

Using Https is fine, run git config --global credential.helper wincred to create a Github credential helper that stores your credentials for you.使用 Https 很好,运行git config --global credential.helper wincred创建一个 Github 凭据助手,为您存储您的凭据。 If this doesn't work, then you need to edit your config file in your .git directory and update the origin to the https url.如果这不起作用,那么您需要编辑.git目录中的config文件并将源更新为 https url。

See this link for the github docs.有关 github 文档,请参阅此链接

I had the same issue with AWS CodeCommit.我对 AWS CodeCommit 也有同样的问题。 All was working on one laptop but it wasn't on the other.所有人都在一台笔记本电脑上工作,但在另一台笔记本电脑上却不行。 The solution was to remove 'PreferredAuthentications publickey' line from .ssh/config in the section with the AWS config.解决方案是从 AWS 配置部分的 .ssh/config 中删除“PreferredAuthentications publickey”行。

I tried to fix similiar problem with hours.我试图用几个小时来解决类似的问题。 In the end I deleted all keys and made new keys without passphrases and finally get rid of the problem.最后,我删除了所有密钥并制作了没有密码的新密钥,最终解决了这个问题。

The problem was with the SSH passphrase, which need to be removed!问题出在 SSH 密码短语上,需要删除!

In case somebody else needs this.以防其他人需要这个。 I created an ssh key with a different name like ar-2022-ssh in the user's .ssh folder.我在用户的 .ssh 文件夹中创建了一个名称不同的 ssh 密钥,例如ar-2022-ssh The problem with this is that Checking for existing SSH keys specifies that supported public keys for GitHub are one of the following.这样做的问题是检查现有的 SSH 密钥指定 GitHub 支持的公共密钥是以下之一。

id_rsa.pub
id_ecdsa.pub
id_ed25519.pub

Once I changed my ssh key name to one of those when generating a key, it worked connecting to GitHub一旦我在生成密钥时将我的 ssh 密钥名称更改为其中之一,它就可以连接到 GitHub

Complete steps to setting SSH access to GitHub was already posted on ServerFault.设置 SSH 访问 GitHub 的完整步骤已发布在 ServerFault 上。

Check the answer from @Alex: https://serverfault.com/a/1088209/416091检查@Alex的答案: https://serverfault.com/a/1088209/416091

At 24 Jul 2022, I should set the server and port to ssh.github.com:443 in SSH user config file ( ~/.ssh/config ). At 24 Jul 2022, I should set the server and port to ssh.github.com:443 in SSH user config file ( ~/.ssh/config ).

you can try this way...你可以试试这个方法...

在此处输入图像描述

在此处输入图像描述

choose another links选择另一个链接

Use the URL for this page when adding GitHub as a remote.将 GitHub 添加为远程时使用此页面的 URL。 it wont cause any such error and push the code它不会导致任何此类错误并推送代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM