git 权限错误 github(无法识别 ssh 密钥)

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

Permissions error github (ssh key not recognized)

gitpermissionssshgithub

提问by mike

I seem to have lost my permissions to a github account after pushing to it from another (local) repository. I am now receiving the following error:

从另一个(本地)存储库推送到 github 帐户后,我似乎失去了对 github 帐户的权限。我现在收到以下错误:

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

I then took the following steps to regenerate a key:

然后我采取了以下步骤来重新生成密钥:

ssh-keygen
Set up an ssh on my account for this laptop, using id_rsa.pub

However, this was unsuccessful. When I try the following code suggested, I receive the following error:

然而,这并不成功。当我尝试以下建议的代码时,我收到以下错误:

ssh-add -l
Could not open a connection to your authentication agent.

Any thoughts?

有什么想法吗?

回答by Idealmind

I solved this problem following this step-by-step instructions:

我按照以下分步说明解决了这个问题:

Step 1:Check for SSH keys

步骤 1:检查 SSH 密钥

$ cd ~/.ssh
# Checks to see if there is a directory named ".ssh" in your user directory
# If it says "No such file or directory" skip to step 3. Otherwise continue to step 2.

Step 2:Backup and remove existing SSH keys

第 2 步:备份并删除现有的 SSH 密钥

$ ls
# Lists all the subdirectories in the current directory
# config  id_rsa  id_rsa.pub  known_hosts

$ mkdir key_backup
# Makes a subdirectory called "key_backup" in the current directory

$ cp id_rsa* key_backup
# Copies the id_rsa keypair into key_backup

$ rm id_rsa*
# Deletes the id_rsa keypair

Step 3: Generate a new SSH key

第 3 步:生成新的 SSH 密钥

$ ssh-keygen -t rsa -C "[email protected]"
# Creates a new ssh key using the provided email

# Generating public/private rsa key pair.
# Enter file in which to save the key (/home/you/.ssh/id_rsa):    
# Enter passphrase (empty for no passphrase): [Type a passphrase]
# Enter same passphrase again: [Type passphrase again]    
# Your identification has been saved in /home/you/.ssh/id_rsa.
# Your public key has been saved in /home/you/.ssh/id_rsa.pub.
# The key fingerprint is:
# 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db [email protected]

Step 4: Add your SSH key to GitHub

第 4 步:将您的 SSH 密钥添加到 GitHub

$ sudo apt-get install xclip
# Downloads and installs xclip

$ xclip -sel clip < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard

Then, go to hithub, and do:

然后,去hithub,做:

  1. Go to your Account Settings
  2. Click "SSH Keys" in the left sidebar
  3. Click "Add SSH key"
  4. Paste your key into the "Key" field
  5. Click "Add key"
  6. Confirm the action by entering your GitHub password
  1. 转到您的帐户设置
  2. 单击左侧栏中的“SSH 密钥”
  3. 点击“添加SSH密钥”
  4. 将您的密钥粘贴到“密钥”字段中
  5. 点击“添加密钥”
  6. 输入您的 GitHub 密码以确认操作

Step 5: Test everything out

第 5 步:测试一切

$ ssh -T [email protected]
# Attempts to ssh to github

If ok, you'll see

如果没问题,你会看到

Hi username! You've successfully authenticated, but GitHub does not
# provide shell access.

Otherwise (it happened with me), you will see

否则(它发生在我身上),你会看到

Agent admitted failure to sign using the key.
# debug1: No more authentication methods to try.
# Permission denied (publickey).

To solve this

为了解决这个

$ ssh-add
# Enter passphrase for /home/you/.ssh/id_rsa: [tippy tap]
# Identity added: /home/you/.ssh/id_rsa (/home/you/.ssh/id_rsa)

For original info

对于原始信息

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

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

https://help.github.com/articles/error-agent-admitted-failure-to-sign

https://help.github.com/articles/error-agent-admitted-failure-to-sign

回答by criscom

If you already have a public key in ~/.ssh (and have already added that key to your github account), you might only have to load your key into the SSH agent again.

如果您已经在 ~/.ssh 中有一个公钥(并且已经将该密钥添加到您的 github 帐户),您可能只需要再次将您的密钥加载到 SSH 代理中。

To test if the SSH agent has the key, type ssh-add -lIf the result is:

要测试 SSH 代理是否具有密钥,请键入ssh-add -lIf 结果为:

The agent has no identities.

Then simply load your key into the SSH agent like this:

然后只需像这样将您的密钥加载到 SSH 代理中:

ssh-add ~/.ssh/github_rsa

(github_rsa is the name on my machine for the stored SSH key. This file can, among others, also be named: id_rsa)

(github_rsa 是我机器上存储的 SSH 密钥的名称。这个文件也可以命名为:id_rsa)

After that you have to enter your passphrase for the key (this is likely your password to log into github). If you get a message like this:

之后,您必须输入密钥的密码(这可能是您登录 github 的密码)。如果您收到这样的消息:

Identity added: /Users/name/.ssh/github_rsa (/Users/cpotzinger/.ssh/github_rsa)

回答by Joviano Dias

Doing an $ ssh-addThis worked for me to resolve the following as well for gitlab

做一个$ ssh-add这对我来说也解决了gitlab的以下问题

jovimac-2:work joviano$ git clone [email protected]:bjetfweb.git
Cloning into 'bjetfweb'...
Access denied.
fatal: Could not read from remote repository.

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

回答by Gilles Quenot

You have to export your key on the server with the command

您必须使用命令在服务器上导出您的密钥

ssh-copy-id user@host

and ssh-agentshould run before your ssh-add. You can put this line in /etc/rc.localif you are under Linux :

并且ssh-agent应该在您的 ssh-add 之前运行。/etc/rc.local如果你在 Linux 下,你可以输入这一行:

eval $(ssh-agent)

Edit: now I know you use windows, so see this thread : Getting ssh-agent to work with git run from windows command shell

编辑:现在我知道你使用 Windows,所以请看这个线程:让 ssh-agent 与 git run 从 Windows 命令外壳一起工作