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

今天在弄Hexo自动发布到github的时候,遇到了点小麻烦,总结了一下,终于找到解决方案!报错如下:

1
2
3
ERROR: Permission to sugen/sugen.github.io.git denied to biancard.
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.

初看知道大概是没有权限,导致不能从远程仓库读取,电脑SSH公钥已经绑定了一个GitHub账号,我就不能再用他的公钥了,具体的请看stackoverflow网友所说的:

1
2
3
GitHub will use the key as means to identify you when you connect to them via SSH. 
As such, you cannot have multiple accounts with the same key,
as GitHub won’t be able to tell then which of your accounts you want to use.

上面说的话很清楚,就是你不能有多个账号添加了同一个公钥,一旦那样github就不能区分到底是哪个用户在安全登陆网站,那安全登录就起不到任何效果了,因为你能登进去,我也能登进去,那服务器到底判断是谁登了呢!但是要注意一个账号可以拥有多个公钥,这个是可以允许的!比如,在A电脑和B电脑上的公钥都绑定了同个一个账户Steven,那么两台电脑在终端上输入ssh -T git@github.com最后都会显示

1
2
$ ssh -T git@github.com
Hi Steven! You've successfully authenticated, but GitHub does not provide shell access.

解决办法

一台电脑生成多公钥,每个公钥对应一个github仓库

解决方案

1、生成一个新的SSH KEY
1
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

使用电子邮件作为标签创建一个新的ssh密钥。

1
> Generating public/private rsa key pair.

当提示“输入文件以保存密钥”时,记得为新的key重新命名。 这接受默认文件位置。

1
2
> Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
> /Users/you/.ssh/github_rsa

在提示符下,键入安全密码(建议不要设置,直接回车)。

1
2
> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]
2、将SSH密钥添加到ssh-agent

在将新的SSH密钥添加到ssh-agent以管理密钥之前,您应该检查现有的SSH密钥并生成新的SSH密钥。 将SSH密钥添加到代理时,请使用默认的macOS ssh-add命令,而不是macports,homebrew或其他外部源安装的应用程序。

1, 在后台启动ssh-agent。

1
2
$ eval "$(ssh-agent -s)"
> Agent pid 59566

将SSH私钥添加到ssh-agent并将密码存储在密钥链中。 如果使用其他名称创建密钥,或者要添加具有不同名称的现有密钥,请将命令中的github_rsa替换为私钥文件的名称。

1
$ ssh-add -K ~/.ssh/github_rsa

编辑修改〜/.ssh/config,没有config文件则vi编辑后保存会自动创建,或者终端输入touch config,创建完以后用vi打开或者是在Finder打开一样。在不影响默认的github设置下我们重新添加一个Host,建一个自己能辨识的github别名,我取的是github-rsa,新建的帐号使用这个别名做克隆和更新

1
$ vi 〜/.ssh/config

加上如下一段代码

1
2
3
4
Host github-rsa.com
HostName github.com
User git
IdentityFile ~/.ssh/github_rsa

编辑完毕之后按下ESC,:x,:x是保存并退出vi编辑器
用cat 〜/.ssh/config 查看刚刚编辑的文件,具体在终端代码如下:

1
$ cat 〜/.ssh/config
1
2
3
4
5
6
7
8
9
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa

Host github-rsa.com
HostName github.com
User git
IdentityFile ~/.ssh/github_rsa
3、将github_rsa.pub密钥添加到您的GitHub帐户, 不知道怎么加,请看我上一条博客
4、将GitHub SSH仓库地址中的git@github.com替换成新建的Host别名。

如原地址是 git@github.com:sugen/comments.git 替换后应该是:git@github-rsa.com:sugen/comments.git 或者github-rsa:sugen/comments.git 都是可以的,如果是新建的仓库,直接使用替换后的URL克隆即可。如果已经使用原地址克隆过了,可以使用命令修改:
进入到GITHUB仓库目录下

1
$ cd /Users/steven/Project/sugen.cn

修改之前

1
2
3
4
$ git remote -v

> github git@github.com:sugen/comments.git (fetch)
> github git@github.com:sugen/comments.git (push)

修改 remote set-url

1
$ git remote set-url github  git@github-rsa.com:sugen/comments.git

验证是否修改成功,使用修改后的github-rsa SSH连接,连接成功用户是sugen,此时公钥是github_rsa

1
2
$ ssh -T github-rsa
> Hi sugen! You've successfully authenticated, but GitHub does not provide shell access.

使用默认的git@github.com SSH去连接,连接成功用户是steven,此时公钥是id_rsa

1
2
$ ssh -T git@github.com
> Hi steven! You've successfully authenticated, but GitHub does not provide shell access.

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