美文网首页杂问题命令工具node
Git 最著名报错 “ERROR: Permission to

Git 最著名报错 “ERROR: Permission to

作者: 穿山甲到底说了什么 | 来源:发表于2016-08-24 02:02 被阅读10151次

今天和同事在弄github的时候,遇到了点小麻烦,在全球最大的中文网上一搜,果然不出所料,找不到写解决方案,于是自己在stackOverFlower上看了好几篇,总结了一下,终于找到解决方案!报错如下:
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公钥已经绑定了他自己的GitHub 账号,我就不能再用他的公钥了,具体的请看stackoverflow网友所说的:
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电脑上的公钥都绑定了同个一个账户Tom,那么两台电脑在终端上输入ssh -T git@github.com最后都会显示
Hi Tom! You've successfully authenticated, but GitHub does not provide shell access.
服务器依然会知道这是Tom的第二台电脑在登陆,他是土豪,账号依然很安全!

场景

下面再举个例子,Tom在公司有个公司账户publicAccount,然后回到家他也有自己创建私人的账号privateAccount,但是他只有一台电脑,意味着一般情况下,他要么用公司账户绑定电脑公钥,要么用家里私人账号绑定,但是不管哪一种绑定,最后都达不到这两个账号访问同一个远程仓库,那么协同开发也就成了泡沫!因为只有一台电脑,如果Tom试图访问没有绑定公钥的账户的时候,就会报错ERROR: Permission to hbxn740150254/BestoneGitHub.git denied to Tom

解决思路

  • 买台新电脑,获得新公钥,这是最土豪也是最傻的方法

  • 利用自己唯一的电脑生成多公钥,公钥一多,不就可以想绑定多少个都行了吗,不怕你把它玩坏😂😂😂

解决方案

  • 1、生成一个新的SSH KEY

    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`
    
  • 2、打开新生成的~/.ssh/id_rsa2.pub文件,将里面的内容添加到GitHub后台。

  • 3、打开~/.ssh/config文件

没有config文件则创建,终端输入touch config ,创建完以后用Vim打开或者是在Finder打开一样。
在不影响默认的github设置下我们重新添加一个Host:
建一个自己能辨识的github别名,我取的是github-personal,新建的帐号使用这个别名做克隆和更新

Host github-personal

HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal

编辑完毕之后按下ESC,:wq,:wq是保存并退出vim编辑器
具体在终端代码如下:
cat config 是把config文件里面的内容在终端输出
AppledeiMac:.ssh Apple$ vim config
AppledeiMac:.ssh Apple$ cat 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
  • 4、将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 github  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)

最后我们进行正常的push,fetch 操作,都可以

//更改后的github push成功!
AppledeiMac:BestoneDemo Apple$ git push github testMerge:master
Total 0 (delta 0), reused 0 (delta 0)
To github-personal:hbxn740150254/BestoneGitHub.git
 cd773e9..f622210  testMerge -> master

//github默认的节点origin我们也可以正常push操作
AppledeiMac:BestoneDemo Apple$ git push origin testMerge:testMerge
Counting objects: 460, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (240/240), done.
Writing objects: 100% (460/460), 16.69 MiB | 442.00 KiB/s, done.
Total 460 (delta 211), reused 453 (delta 205)
To git@git.coding.net:hbxn740150254/Local-Git.git
* [new branch]      testMerge -> testMerge

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

  • 添加你的ssh密钥到ssh-agent中

    # start the ssh-agent in the background
    eval "$(ssh-agent -s)"
    Agent pid 59566
    
  • 如果你的密钥不是系统默认的RSA文件名id_rsa,比如像我一样另外创了一对公钥/密钥id_rsa_personal,那么就把他们添加进去,注意:密钥文件是不带扩展名的,公钥扩展名是.pub,代表publicKey,
    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.
    

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

相关文章

网友评论

  • 许渺:github账户如果还是显示之前id_rsa密钥账户的话请把你的密钥加入sshAgent代理中
    添加你的ssh密钥到ssh-agent中
    # start the ssh-agent in the background
    eval "$(ssh-agent -s)"

    这个帮到我了 感谢:grin:
    许渺:不过有个问题 每次都要执行这个 :cry:
    穿山甲到底说了什么:@许渺 :smile::smile:不谢哈哈
  • 卓越笔记:我是这样弄得,公司一个账号,家里一个账号,把家里的账号加入到公司账号创建项目的成员组里面就可以推送上去。
  • 夏霂熠雨:博主,您好,非常感谢您的文章,解决了小白的一个大问题,很赞。不过文中有个小错误,修改仓库地址的命令:git remote set-url github github- personal:hbxn740150254/BestoneGitHub.git应该是改为:git remote set-url origin github- personal:hbxn740150254/BestoneGitHub.git
    穿山甲到底说了什么:@夏霂熠雨 客气啥,四海之类皆兄弟,哈哈,这个我没太注意,很久以前写的,github只是一个 远程仓库的别名, 可以 git remote -v 看下,你需要更改哪个就改哪个
  • Haha小酱:很赞!:clap:
  • 荔枝lizhi_iOS程序猿:我的电脑以前的同事用他的github 连过,我现在要替换成 自己的github 账号,怎么弄呀,老是提示我 ,以前的账号 不能连接我现在的github 库
    荔枝lizhi_iOS程序猿:@穿山甲到底说了什么 谢谢 听懂了 git免输入密码还是懂得
    穿山甲到底说了什么:如果 你有其他网站 绑定了 本机 默认的 id_rsa.pub 公钥的话,那就只有 重新生成了,比如 专门用于连接github的 id_rsa_github.pub,然后 cd ~/.ssh ,看有没有 config文件,没有的话 touch config,在该路径 创一个 config 文件 ,配置 ssh 连接文件,这个配置文件 可以指定 连接 不同的ssh网站,各自所对应用的 密钥,文章写得很清楚,最后 看你 ssh-T github-personal 可不可以连接,不能连接 的话 就 ssh-add ~/.ssh/id_rsa_personal 把你的密钥加进去,再ssh-T github-personal 应该就可以
    穿山甲到底说了什么:@张芝丽Sunny_是我 那 你还需要 让他用你的电脑连接github吗? 如果你不想让他用的话,简单暴力的方法,就是 把你本机 的 id_rsa 和 id_rsa.pub 一同删除,然后重新 在该路径 创建同名的 id_rsa 公私钥就可以,提醒 删除前 先保存,把两个文件可以先拷贝到另一个文件夹中!这种方法 只限于 id_rsa.pub 只关联了你朋友 的github,意思就是 如果你有其他地方 也绑定了这个id_rsa.pub 的公钥匙的话,就不能 删除这个了,因为会影响 你对其他网站的 ssh 连接,不知道你有没有 听懂:smile::smile:

本文标题:Git 最著名报错 “ERROR: Permission to

本文链接:https://www.haomeiwen.com/subject/pxbnsttx.html