内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用
在从另一个(本地)存储库推送到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
然后,到histub去做:
步骤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中有一个公钥(并且已经将该密钥添加到您的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)