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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了权限错误github(无法识别ssh密钥)相关的知识,希望对你有一定的参考价值。

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

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

然后,我按照以下步骤重新生成密钥:

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

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

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

有什么想法吗?

答案

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

第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.

第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

第3步:生成新的SSH密钥

$ ssh-keygen -t rsa -C "your_email@youremail.com"
# 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 your_email@youremail.com

第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

然后,转到github,并执行:

  1. 转到您的帐户设置
  2. 单击左侧栏中的“SSH密钥”
  3. 点击“添加SSH密钥”
  4. 将您的密钥粘贴到“密钥”字段中
  5. 点击“添加密钥”
  6. 输入您的GitHub密码确认操作

第5步:测试一切

$ ssh -T git@github.com
# Attempts to ssh to github

好的,你会看到

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

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

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

要解决这个问题

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

原始信息

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

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

另一答案

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

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

The agent has no identities.

然后只需将密钥加载到SSH代理中,如下所示:

ssh-add ~/.ssh/github_rsa

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

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

Identity added: /Users/name/.ssh/github_rsa (/Users/cpotzinger/.ssh/github_rsa)
另一答案

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

jovimac-2:work joviano$ git clone git@git.xyz.com: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.
另一答案

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

ssh-copy-id user@host

ssh-agent应该在你的ssh-add之前运行。如果您在Linux下,可以将此行放在/etc/rc.local中:

eval $(ssh-agent)

编辑:现在我知道你使用windows,所以看看这个帖子:Getting ssh-agent to work with git run from windows command shell

以上是关于权限错误github(无法识别ssh密钥)的主要内容,如果未能解决你的问题,请参考以下文章

我是 Github 新手,无法生成 SSH 密钥

无法连接 SSH 密钥,权限被拒绝(公钥)

多个github帐户错误

无法为 github 和 aws 生成 ssh 密钥(非法选项 -- C)

Paramiko 无法识别 ssh-keygen 生成的 SSH 密钥:“不是有效的 RSA 私钥文件”

Paramiko 无法识别 ssh-keygen 生成的 SSH 密钥:“不是有效的 RSA 私钥文件”