Git 最著名报错 “ERROR: Permission to XXX.git denied to user”解决方案

浪子不回头ぞ 提交于 2020-02-04 10:06:29

今天在github上pr项目时,报了一个错,后来百度了一下,折腾了好久,特意记下来。
在这里插入图片描述
报错如下:(自己忘了保留,借用一下网上的)
链接:https://www.jianshu.com/p/12badb7e6c10

ERROR: Permission to hbxn740150254/BestoneGitHub.git denied to Chenzuohehe. 
fatal: Could not read from remote repository.Please make sure you have the correct access rights and the repository exists.

看了解决办法之后,大致知道要重新生成一个SSH KEY了。
下面一步一步来:

  1. 生成SSH KEY
//先进入本地ssh目录
AppledeiMac:~ Apple$ cd ~/.ssh
//查看当前已有的密钥文件
AppledeiMac:.ssh Apple$ ls
id_rsa        id_rsa.pub  known_hosts
//生成密钥
AppledeiMac:.ssh Apple$ ssh-keygen -t rsa -C "iMac_personnal_publicKey"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/Apple/.ssh/id_rsa):               
/Users/Apple/.ssh/id_rsa_personal
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/Apple/.ssh/id_rsa_personal.
Your public key has been saved in /Users/Apple/.ssh/id_rsa_personal.pub.
The key fingerprint is:
SHA256:1gepuxDHwJRnFbKvc0Zq/NGrFGE9kEXS06jxatPPrSQ iMac_personnal_publicKey
The key's randomart image is:
+---[RSA 2048]----+
|      ....=*oo   |
|     o. ooo=+ .  |
|      oo. =+o.   |
|       o =.o..   |
|      . S =o.    |
|       = =++.    |
|      . B.=.Eo.. |
|       o B . +o .|
|          . o.. .. |
+----[SHA256]-----+
//再次查看密钥文件,可以看到刚刚添加的密钥文件已经生成了
AppledeiMac:.ssh Apple$ ls
id_rsa            id_rsa_personal     known_hosts
id_rsa.pub        id_rsa_personal.pub

SSH是什么?他是一种远程登录服务,登录后连接到服务器的终端上,然后就可以为所欲为了。git服务器也支持ssh登录(当然只给你控制仓库上传和下载)。

第一步,就是要生成私钥和公钥密钥对,这是一种加密方式,它给出一对密码,私钥加密,公钥可以解密,公钥加密,私钥可以解密,其中,公钥是可以公开发行的,别人用你的公钥加密了数据,这个密文数据发给你,你就能用私钥进行解密。然后你拿到对方的公钥,同样的步骤,你就可以发加密后的信息给他。这就达成了加密通信。理论上是很难破解的,只要你藏好私钥。

ssh-keygen 命令会生成一对秘钥,在linux中一般是放在~/.ssh/目录下面。秘钥文件是一个文本文件,可以打开读取。把公钥内容复制粘贴到服务器上面,服务器就拥有你的公钥了。

创建密钥对让你输入私钥的名字:比如id_rsa 是私钥,id_rsa.pub就是公钥。

但是,如果你有很多对秘钥,服务器怎么知道你要用哪个呢?答案在第三步。

  1. 打开新生成的~/.ssh/id_rsa_personal.pub文件,复制里面的内容添加到GitHub上SSH keys里。
    在这里插入图片描述
  2. 打开config文件
    没有config文件则创建,终端输入touch config ,创建完以后用Vim打开或者是在Finder打开一样。
    在不影响默认的github设置下我们重新添加一个Host:
    建一个自己能辨识的github别名,我取的是github-personal,新建的帐号使用这个别名做克隆和更新。或者直接复制代码添加到已有的config文件里面,添加之后是下面的内容。
#Default GitHub
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa

Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
  1. 将GitHub SSH仓库地址中的git@github.com替换成新建的Host别名
    如原地址是 git@github.com:hbxn740150254/BestoneGitHub.git 替换后应该是:github-personal:hbxn740150254/BestoneGitHub.git 或者git@github-personal:hbxn740150254/BestoneGitHub.git亲测都是可以的,
    如果是新建的仓库,直接使用替换后的URL克隆即可。如果已经使用原地址克隆过了,可以使用命令修改:
//进入项目文件夹目录
AppledeiMac:.ssh Apple$ cd /Users/Apple/Desktop/BestoneDemo 
//修改之前
Apple$ git remote -v
github  git@github.com:hbxn740150254/BestoneGitHub.git (fetch)
github  git@github.com:hbxn740150254/BestoneGitHub.git (push)
//修改 remote set-url
AppledeiMac:BestoneDemo Apple$ git remote set-url origin  github- personal:hbxn740150254/BestoneGitHub.git
//验证是否修改成功    
//使用修改后的github-personal SSH连接,连接成功用户是hbxn740150254,此时公钥是id_rsa_personal
AppledeiMac:BestoneDemo Apple$ ssh -T github-personal
Hi hbxn740150254! You've successfully authenticated, but GitHub does not provide shell access.
//使用默认的git@github.com SSH去连接,连接成功用户是FaxeXian,此时公钥是id_rsa
AppledeiMac:.ssh Apple$ ssh -T git@github.com
Hi FaxeXian! You've successfully authenticated, but GitHub does not provide shell access.
//修改之后
AppledeiMac:BestoneDemo Apple$ git remote -v
github  github-personal:hbxn740150254/BestoneGitHub.git (fetch)
github  github-personal:hbxn740150254/BestoneGitHub.git (push)

如果github账户如果还是显示之前id_rsa密钥账户的话,请把你的密钥加入sshAgent代理中

apple:.ssh apple$ eval "$(ssh-agent -s)"
Agent pid 19795
//添加密钥 id_rsa_personal
apple:.ssh apple$ ssh-add id_rsa_personal
Identity added: id_rsa_personal (github-personal)
//添加默认密钥 id_rsa
apple:.ssh apple$ ssh-add id_rsa
//密钥有密码的话就会要你提示输入 passphrase
Enter passphrase for id_rsa: 
//测试用密钥isa是否连接成功github
apple:.ssh apple$ ssh -T git@github.com
Hi hbxn740150254! You 've successfully authenticated, but GitHub does not provide shell access.
//测试密钥id_rsa_personal是否连接成功github
apple:.ssh apple$ ssh -T git@github-personal
Hi FaxeXian! You've successfully authenticated, but GitHub does not provide shell access.

这样,一台电脑生成的两个公钥让两个用户成功连接,就可以访问别人的远程仓库,可以进行多人开发了!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!