Github 权限错误(SSH 密钥未被识别)

19

我似乎在从另一个(本地)仓库推送到 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.

有什么想法吗?

4个回答

56

我将按照以下逐步说明解决这个问题:

第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 Keys"
  3. 点击"添加SSH key"
  4. 将你的密钥粘贴到"Key"字段中
  5. 点击"添加key"
  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)

有关原始信息,请参见

生成SSH密钥

错误:代理人承认签名失败


抱歉,请问您如何输入路径?我的路径是C:\ Documents and Settings \ admin \ .ssh文件夹。在键入路径时应该使用什么格式? - Thinkerer
在您的 Mac 上,您可能需要运行 'brew install homebrew/emacs/xclip-mode' 而不是 'sudo apt-get install xclip'。 - Nicholas Murray
ssh-add命令对我很有用。我认为这是由于macOS Sierra的新行为所需的。 - acoustic_north

5
如果您已经在~/.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)

3

执行$ 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.

1
你必须使用命令在服务器上导出你的密钥。
ssh-copy-id user@host

在运行ssh-add之前,应该先运行ssh-agent。 如果您使用的是Linux系统,可以将此行放入/etc/rc.local文件中:

eval $(ssh-agent)

编辑:现在我知道你使用的是Windows操作系统,所以请参考这个帖子:如何让ssh-agent在Windows命令行中与git一起工作


谢谢提醒 - 我正在Windows上使用git-bash接口,但ssh-copy-id命令未被识别。 - mike
所以,您必须手动从 id_rsa.pub 复制生成的行,并将其附加到服务器上的文件 ~user/.ssh/authorized_keys - Gilles Quénot

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接