新建user1 SSH key:
$ cd ~/.ssh
切換到C:\Users\Administrator.ssh
ssh-keygen -t rsa -C "user1@email.com"
一路回車即可颇蜡;
新建user2的SSH key
ssh-keygen -t rsa -C "user2@email.com"
設(shè)置名稱為Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa):** /c/Users/Administrator/.ssh/id_rsa_user2**
具體演示過(guò)程如下圖:
密鑰添加到SSH agent中
因?yàn)槟J(rèn)只讀取id_rsa世吨,為了讓SSH識(shí)別新的私鑰,需將其添加到SSH agent中:
ssh-add ~/.ssh/id_rsa_user2
如果出現(xiàn)Could not open a connection to your authentication agent的錯(cuò)誤,就試著用以下命令:
ssh-agent bash
ssh-add ~/.ssh/id_rsa_user2
修改config文件
在~/.ssh目錄下找到config文件,如果沒(méi)有就創(chuàng)建:
touch config
然后修改我的config配置如下:
如果存在的話,其實(shí)就是往這個(gè)config中添加一個(gè)Host:
建一個(gè)github別名,新建的帳號(hào)使用這個(gè)別名做克隆和更新
其規(guī)則就是:從上至下讀取config的內(nèi)容锈候,在每個(gè)Host下尋找對(duì)應(yīng)的私鑰。這里將GitHub SSH倉(cāng)庫(kù)地址中的git@github.com替換成新建的Host別名如:github2敞贡,那么原地址 是:git@github.com:test/Mywork.git泵琳,替換后應(yīng)該是:github2:test/Mywork.git.
打開(kāi)新生成的~/.ssh/id_rsa_user2.pub文件,將里面的內(nèi)容添加到GitHub后臺(tái)誊役。
第一個(gè)github帳號(hào)下的SSH Key中也要記得添加获列。
測(cè)試
$ ssh -T git@github.com
Hi chenlianjiang! You've successfully authenticated, but GitHub does not provide shell access.
$ ssh -T git@github2
Hi 1047353200! You've successfully authenticated, but GitHub does not provide shell access.
6
應(yīng)用
測(cè)試成功,嘗試克隆1047353200賬號(hào)下的遠(yuǎn)程倉(cāng)庫(kù)蛔垢。如下:
$ git clone git@github2:1047353200/git-demo.git