今天和同事在弄github的時候毅待,遇到了點小麻煩,在全球最大的中文網(wǎng)上一搜,果然不出所料叁征,找不到寫解決方案纳账,于是自己在stackOverFlower上看了好幾篇,總結(jié)了一下航揉,終于找到解決方案塞祈!報錯如下:
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.
初看知道大概是沒有權(quán)限,導(dǎo)致不能從遠(yuǎn)程倉庫讀取帅涂,后來詢問才知道我同事的電腦的SSH公鑰已經(jīng)綁定了他自己的GitHub 賬號,我就不能再用他的公鑰了尤蛮,具體的請看stackoverflow網(wǎng)友所說的:
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就不能區(qū)分到底是哪個用戶在安全登陸網(wǎng)站产捞,那安全登錄就起不到任何效果了醇锚,因為你能登進(jìn)去,我也能登進(jìn)去坯临,那服務(wù)器到底判斷是誰登了呢焊唬!但是要注意一個賬號可以擁有多個公鑰,這個是可以允許的看靠!比如赶促,在A電腦和B電腦上的公鑰都綁定了同個一個賬戶Tom,那么兩臺電腦在終端上輸入ssh -T git@github.com
最后都會顯示
Hi Tom! You've successfully authenticated, but GitHub does not provide shell access.
服務(wù)器依然會知道這是Tom的第二臺電腦在登陸挟炬,他是土豪鸥滨,賬號依然很安全!
場景
下面再舉個例子谤祖,Tom在公司有個公司賬戶publicAccount婿滓,然后回到家他也有自己創(chuàng)建私人的賬號privateAccount,但是他只有一臺電腦粥喜,意味著一般情況下凸主,他要么用公司賬戶綁定電腦公鑰,要么用家里私人賬號綁定额湘,但是不管哪一種綁定卿吐,最后都達(dá)不到這兩個賬號訪問同一個遠(yuǎn)程倉庫,那么協(xié)同開發(fā)也就成了泡沫缩挑!因為只有一臺電腦但两,如果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文件擅耽,將里面的內(nèi)容添加到GitHub后臺活孩。
-
3、打開~/.ssh/config文件
沒有config文件則創(chuàng)建乖仇,終端輸入touch config 憾儒,創(chuàng)建完以后用Vim打開或者是在Finder打開一樣。
在不影響默認(rèn)的github設(shè)置下我們重新添加一個Host:
建一個自己能辨識的github別名乃沙,我取的是github-personal起趾,新建的帳號使用這個別名做克隆和更新
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_personal
編輯完畢之后按下ESC,:wq警儒,:wq是保存并退出vim編輯器
具體在終端代碼如下:
cat config 是把config文件里面的內(nèi)容在終端輸出
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
替換后應(yīng)該是:github-personal:hbxn740150254/BestoneGitHub.git
或者git@github-personal:hbxn740150254/BestoneGitHub.git
親測都是可以的蜀铲,
如果是新建的倉庫边琉,直接使用替換后的URL克隆即可。如果已經(jīng)使用原地址克隆過了记劝,可以使用命令修改:
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.
//使用默認(rèn)的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)
最后我們進(jìn)行正常的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默認(rèn)的節(jié)點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
-
如果你的密鑰不是系統(tǒng)默認(rèn)的RSA文件名id_rsa忧额,比如像我一樣另外創(chuàng)了一對公鑰/密鑰id_rsa_personal,那么就把他們添加進(jìn)去愧口,注意:密鑰文件是不帶擴(kuò)展名的睦番,公鑰擴(kuò)展名是.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) //添加默認(rèn)密鑰 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.
這樣托嚣,一臺電腦生成的兩個公鑰讓兩個用戶成功連接,就可以訪問別人的遠(yuǎn)程倉庫厚骗,可以進(jìn)行多人開發(fā)了J酒簟!