权限被拒绝(公钥)在 Windows 7 上使用 git 时出错

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

permission denied (publickey) Error using git on windows 7

git

提问by tutuDajuju

When I want to push to github with this command

当我想用这个命令推送到 github 时

git push origin master

I got this

我懂了

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

So, what's wrong?

那么,怎么了?

回答by tutuDajuju

EUREKA!

尤里卡!

Apparently, you can use plinkas the main ssh client and just load your keys in pageant(if you're like me, you already do):

显然,您可以plink用作主要的 ssh 客户端,只需加载您的密钥pageant(如果您像我一样,您已经这样做了):

You can do that by setting the GIT_SSHenv variable to plink.exepath like so:

您可以通过将GIT_SSHenv 变量设置为plink.exepath来做到这一点,如下所示:

set GIT_SSH=C:\Program Files\PuTTY\plink.exe

设置 GIT_SSH=C:\Program Files\PuTTY\plink.exe

or, you can use plink from TortoiseGit:

或者,您可以使用 TortoiseGit 中的 plink:

set GIT_SSH=c:\Program Files\TortoiseGit\bin\TortoisePLink.exe

设置 GIT_SSH=c:\Program Files\TortoiseGit\bin\TortoisePLink.exe

Credit: Original solution taken from this blog post

信用:从这篇博客文章中获取的原始解决方案

回答by G Shah

I was able to resolve this issue as follows:

我能够解决这个问题如下:

When you do:

当你这样做时:

ssh-keygen -t rsa

it prompts you to (optionally) enter a filename for saving the generated keys. Specifying a filename wasted my whole day! Next day I let it use the default filename and the problem(s) disappeared! Imagine!!

它会提示您(可选)输入文件名以保存生成的密钥。指定文件名浪费了我一整天!第二天我让它使用默认文件名,问题就消失了!想象!!

Platform was Win7 and msysgit.

平台是Win7和msysgit。

回答by Johnny Oshika

Here is a step-by-step guide that I used to get this to work.

这是我用来使其工作的分步指南。

Platform: Windows 7

平台:Windows 7

Install msysgit from http://msysgit.github.io/

http://msysgit.github.io/安装 msysgit

During installation, accept all of the default options, except when the 'Select Components' option appears. When this appears, select 'Git Bash Here' option. Although this isn't necessary, it adds a nice context menu when working in Windows Explorer that I found to be very helpful.

在安装过程中,接受所有默认选项,除非出现“选择组件”选项。当出现这种情况时,选择“Git Bash Here”选项。虽然这不是必需的,但它在 Windows 资源管理器中工作时添加了一个很好的上下文菜单,我发现它非常有用。

enter image description here

在此处输入图片说明

Once msysgit is installed Git Bash will also be installed. Open Git Bash in one of 2 ways:

一旦安装了 msysgit,Git Bash 也将被安装。以两种方式之一打开 Git Bash:

  • Click the Windows Start key and start typing Git Bash
  • Or, right click somewhere (e.g. your Desktop) and select Git Bash Here. This option is only available if 'Git Bash Here' context menu was installed.
  • 单击 Windows 开始键并开始键入 Git Bash
  • 或者,右键单击某处(例如您的桌面)并选择 Git Bash Here。此选项仅在安装了“Git Bash Here”上下文菜单时可用。

In Git Bash's command window, enter this:

在 Git Bash 的命令窗口中,输入:

$ ssh-keygen -t rsa

When asked to enter a file name, just accept the default. Choose a strong passphrase when prompted, and your public key should now be saved. Your screen should look like this:

当要求输入文件名时,只需接受默认值。出现提示时选择一个强密码,现在应该保存您的公钥。您的屏幕应如下所示:

enter image description here

在此处输入图片说明

Go open the public key file in Notepad. The file should reside here:

在记事本中打开公钥文件。该文件应位于此处:

C:\Users\{username}\.ssh\id_rsa.pub

Copy all of the content in the file to your clipboard, then go to GitHub's SSH settings page:

将文件中的所有内容复制到剪贴板,然后转到 GitHub 的 SSH 设置页面:

https://github.com/settings/ssh

https://github.com/settings/ssh

Choose 'Add SSH key', enter a useful 'Title' and paste the content into the 'Key' textarea.

选择“添加 SSH 密钥”,输入有用的“标题”并将内容粘贴到“密钥”文本区域。

To simplify your life, you can use the SSH agent to save your passphrase so that you don't need to remember it. To do so, type this into Git Bash:

为了简化您的生活,您可以使用 SSH 代理来保存您的密码,这样您就不需要记住它。为此,请在 Git Bash 中输入:

$ eval `ssh-agent -s`
$ ssh-add ~/.ssh/id_rsa

You'll be prompted to enter your passsphrase. If everything succeeds, your identity will have been added. Note: this passphrase will be forgotten as soon as you close your shell. I'm not sure how to make this persist across sessions, but maybe someone can help?

系统会提示您输入密码。如果一切顺利,您的身份将被添加。注意:一旦关闭 shell,这个密码就会被忘记。我不确定如何在会话中保持这种状态,但也许有人可以提供帮助?

To test that everything works, enter this into Git Bash:

要测试一切正常,请在 Git Bash 中输入:

$ ssh -T [email protected]

You should see a 'success' meesage.

您应该会看到“成功”消息。

Sources:

资料来源:

https://help.github.com/articles/generating-ssh-keys/

https://help.github.com/articles/generate-ssh-keys/

https://help.github.com/articles/working-with-ssh-key-passphrases/

https://help.github.com/articles/working-with-ssh-key-passphrases/

explanation on why eval `ssh-agent -s` should be used instead of just ssh-agent -s

https://stackoverflow.com/a/17848593/188740

https://stackoverflow.com/a/17848593/188740

回答by Veeti

Have you generated an SSH key for yourself and added it to your Github account? They have a guide for this here.

您是否为自己生成了 SSH 密钥并将其添加到您的 Github 帐户中?他们在这里有一个指南。

回答by Tasha

Using Windows 8 to setup your ssh and Github

使用 Windows 8 设置 ssh 和 Github

  1. If it says "Permission denied (publickey)" you will have to put in a passphrase for your key. Do not be tempted to just press enter...this was what worked for me...it took me five hours to realize that pressing enter made OpenSSH feel that your key was too public so that is why it is denying you from going to the next step.

  2. If it says "The authenticity of host 'github.com (203.232.175.90)' can't be established." Then you will have to go through the process shown here: [Google] (https://help.github.com/articles/error-permission-denied-publickey.)

  1. 如果它显示“权限被拒绝(公钥)”,您将必须为您的密钥输入密码。不要只想按回车键...这对我有用...我花了五个小时才意识到按回车键让 OpenSSH 觉得您的密钥太公开了,这就是为什么它拒绝您去下一步。

  2. 如果显示“无法建立主机'github.com (203.232.175.90)'的真实性。” 然后,您必须完成此处显示的流程:[Google] ( https://help.github.com/articles/error-permission-denied-publickey。)

Go to the part that says "Verify the public key is attached to your GitHub account". You want to start with the part that says account. (I am assuming you already have an account.

转到“验证公钥是否附加到您的 GitHub 帐户”部分。您想从说帐户的部分开始。(我假设您已经有一个帐户。

Also, you want to be in C:\RailsInstaller\Git.ssh and when I did 'dir' I noticed that I had '.', '..', 'known_hosts'. I had to get a new key by doing the following (I found this on the Github site):

另外,您想在 C:\RailsInstaller\Git.ssh 中,当我执行 'dir' 时,我注意到我有 '.'、'..'、'known_hosts'。我必须通过执行以下操作来获得一个新密钥(我在 Github 站点上找到了这个):

"ssh-keygen -t rsa -C "[email protected]" # Creates a new ssh key, using the provided email as a label # Generating public/private rsa key pair. < # Enter file in which to save the key(/c/Users/you/.ssh/id_rsa): [Press enter]"

"ssh-keygen -t rsa -C "[email protected]" # 创建一个新的 ssh 密钥,使用提供的电子邮件作为标签 # 生成公共/私有 rsa 密钥对。< # 输入保存密钥的文件( /c/Users/you/.ssh/id_rsa): [按回车]”

And pay attention to the file where the key will be saved. Do not just press enter.. my one showed "(//.ssh/id_rsa)" I had to type "/.ssh/id_rsa" to make sure it was in the right directory.

并注意保存密钥的文件。不要只是按回车键..我的一个显示“(//.ssh/id_rsa)”我必须输入“/.ssh/id_rsa”以确保它在正确的目录中。

And you should be able to type ssh in the command line. If you cannot do that you will have to add it to your path.

您应该能够在命令行中键入 ssh。如果您不能这样做,则必须将其添加到您的路径中。

回答by mynkow

I solved a similar problem by adding a System Environment Variable. The key was that I am pointing to the git.exeinside the cmdfolder

我通过添加System Environment Variable解决了类似的问题。关键是,我指着git.exe里面CMD文件夹

name: GIT
value: C:\Program Files (x86)\Git\cmd\git.exe

名称:GIT
值:C:\Program Files (x86)\Git\cmd\git.exe

回答by Vladimir Beletskiy

If you have already generated the key, and it has a default name, the problem might be in the absent environment variable.

如果您已经生成了密钥,并且它有一个默认名称,那么问题可能出在缺少环境变量中。

On Windows 7 right click "My computer" and go to properties. There click on advanced properties and click the button Environment Variables. There add a user environment variable

在 Windows 7 上,右键单击“我的电脑”并转到属性。单击高级属性,然后单击按钮环境变量。添加一个用户环境变量

Name: HOME 
Value: %USERPROFILE%

回答by Alexander Taylor

there could be something wrong with your heroku keys. try:

您的 heroku 密钥可能有问题。尝试:

heroku keys:add

similar question here: Heroku Git - fatal: The remote end hung up unexpectedly

类似的问题在这里:Heroku Git - 致命:远程端意外挂断

回答by Leo Moore

The problem may be related to the order in which the ssh.exe are in the PATH environmental variable.

该问题可能与 ssh.exe 在 PATH 环境变量中的顺序有关。

In my case there was a ssh.exe in both the C:\Program Files(x86)\git and also in my C:\cwgwin directory. When I was in my Git Bash everything worked fine (it was using the ssh in the git directory) but when I was in my Command Prompt it was using the ssh in my cygwin directory.

就我而言,C:\Program Files(x86)\git 和我的 C:\cwgwin 目录中都有一个 ssh.exe。当我在 Git Bash 中时一切正常(它在 git 目录中使用 ssh)但是当我在命令提示符中时它在我的 cygwin 目录中使用 ssh。

When I tried to access github it was not able to find the .ssh folder with my private key because the key was not attached to the ssh the command prompt was using (ie the C:\cygwin one) and that is why I was getting the Permission Denied error.

当我尝试访问 github 时,它无法找到带有我的私钥的 .ssh 文件夹,因为密钥没有附加到命令提示符正在使用的 ssh(即 C:\cygwin 一个),这就是为什么我得到权限被拒绝错误。

What I did was rename (or delete) the git.exe and ssh.exe in my C:\cygwin directory and make sure that C:\Program Files(x86)\git appears before the C:\cwgwin in the PATH order.

我所做的是重命名(或删除)我的 C:\cygwin 目录中的 git.exe 和 ssh.exe,并确保 C:\Program Files(x86)\git 在 PATH 顺序中出现在 C:\cwgwin 之前。

回答by Alan Berezin

Here is a solution to a very specific problem that has the same error signature. This was the mistake I made and it is very easy to make. Basically, instead of doing this

这是具有相同错误签名的非常具体问题的解决方案。这是我犯的错误,而且很容易犯。基本上,而不是这样做

  git remote add origin [email protected]:myusername/myrepo.git

I did this (note typo)

我这样做了(注意错字)

  git remote add origin [email protected]:myusername/myrepo.git

http://www.celticwolf.com/blog/2011/02/08/git-permission-denied-publickey/

http://www.celticwolf.com/blog/2011/02/08/git-permission-denied-publickey/