Git 最著名報錯 「ERROR Permission to XXX git denied to user」終極解決方案

今天和同事在弄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 ##解決思路git

  • ####買臺新電腦,得到新公鑰,這是最土豪也是最傻的方法
  • 利用本身惟一的電腦生成多公鑰,公鑰一多,不就能夠想綁定多少個都行了嗎,不怕你把它玩壞😂😂😂

##解決方案github

  • 一、生成一個新的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`
    複製代碼
  • 二、打開新生成的~/.ssh/id_rsa2.pub文件,將裏面的內容添加到GitHub後臺。

  • ####三、打開~/.ssh/config文件 沒有config文件則建立,終端輸入touch config ,建立完之後用Vim打開或者是在Finder打開同樣。 在不影響默認的github設置下咱們從新添加一個Host: 建一個本身能辨識的github別名,我取的是github-personal,新建的賬號使用這個別名作克隆和更新shell

    Host github-personal HostName github.com User git IdentityFile ~/.ssh/id_rsa_personalvim

    編輯完畢以後按下ESC,:wq,:wq是保存並退出vim編輯器 具體在終端代碼以下: cat config 是把config文件裏面的內容在終端輸出 AppledeiMac:.ssh Applevim 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
    複製代碼
  • ####四、將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 Applecd /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 Applegit 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 Applessh -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)app

最後咱們進行正常的push,fetch 操做,均可以dom

//更改後的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密鑰到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.
    複製代碼

這樣,一臺電腦生成的兩個公鑰讓兩個用戶成功鏈接,就能夠訪問別人的遠程倉庫,能夠進行多人開發了!!編輯器

相關文章
相關標籤/搜索