天天看點

同一台電腦設定多個公鑰與不同GITHUB帳号互動

工具/原料

ssh git

方法/步驟

制造第一把公鑰:

ssh-keygen -t rsa -C "[email protected]"

# 設定名稱為id_rsa_derek

Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa): id_rsa_derek

#添加到SSH agent中

ssh-add id_rsa_derek

制造第二把公鑰:

ssh-keygen -t rsa -C "[email protected]"

# 設定名稱為id_rsa_ranpop

Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa): id_rsa_ranpop

#添加到SSH agent中

ssh-add id_rsa_ranpop

将id_rsa_derek.pub添加到derek帳号的背景ssh。

将id_rsa_ranpop.pub添加到ranpop帳号的背景ssh。

在.ssh目錄下配置config檔案:

Host derek

HostName github.com

User git

IdentityFile ~/.ssh/id_rsa_derek

Host ranpop

HostName github.com

User git

IdentityFile ~/.ssh/id_rsa_ranpop

對于derek帳号下的倉庫:

git clone derek:githubname/repository.git

(原位址是:[email protected]:githubname/repository.git,替換後應該是:derek:githubname/repository.git)

對于ranpop帳号下的倉庫:

git clone ranpop::githubname/repository.git

(原位址是:[email protected]:githubname/repository.git,替換後應該是:ranpop:githubname/repository.git)

測試:

ssh -T derek

Hi derek! You've successfully authenticated, but GitHub does not provide shel l access.

ssh -T ranpop

Hi ranpop! You've successfully authenticated, but GitHub does not provide shel l access.

說明OK。

如果已經使用原位址克隆過了,可以使用指令修改:

git remote set-url origin derek:githubname/repository.git

相關原理:

1、一把公鑰隻能被一個GITHUB帳号擁有->是以必須為不同的帳号建立公鑰。

2、互動時需要本地私鑰與GITHUB帳号的公鑰配對。

是以,要在同一台電腦上給兩個屬于不同帳号的倉庫送出時,必須在本地建立兩對公/私鑰匙,分别把兩把公鑰給兩個帳号,送出時根據要送出的倉庫所屬帳号選擇相應的本地私鑰即可;

當我們要在一個倉庫上PUSH送出的内容時,使用以上的步驟可以選擇對應的公鑰,GITHUB伺服器收到送出的内容時,先解析出倉庫位址,然後從該倉庫的所屬帳号中找到一把能解鎖該送出的公鑰。

轉載于:https://blog.51cto.com/5404542/1748211