怎麼給Git配置多個SSH Key?

有時候咱們可能須要在同一臺電腦上配置多個SSH Key,好比公司項目使用的是GitHub,我的開發用的是碼雲Gitee。這個時候咱們可能須要有兩個SSH Key,怎麼配置呢?git

假設你以前已經生成了一個GitHubSSH Key,能夠用命令cat ~/.ssh/id_rsa.pub查看已經生成的SSH Key:github

GitHub的SSH Key

複製命令ssh-keygen -t rsa -C 'xxxxx@youremail.com' -f ~/.ssh/gitee_id_rsa生成一個GiteeSSH Key,一路回車就能夠了(記得把郵箱改爲你本身的)。能夠看到.ssh文件夾下面多了兩個文件。服務器

.ssh文件夾

使用命令cat ~/.ssh/gitee_id_rsa.pub查看GiteeSSH Key,複製ssh開頭的那一串公鑰,添加到Gitee倉庫。ssh

Gitee的SSH Key


使用命令touch ~/.ssh/config,在~/.ssh文件夾下添加config文件,能夠看到文件夾下面多了一個config文件。

測試

右鍵使用記事本打開,複製如下信息添加到config文件保存,其中HostHostName填寫git服務器的域名,IdentityFile填寫私鑰的路徑。spa

# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_id_rsa
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa

使用如下命令分別測試GitHubGitee,查看SSH Key是否添加成功。3d

ssh -T git@gitee.com
ssh -T git@github.com

看到如下的提示,就表示添加成功,能夠拉取、推送代碼了。
code

相關文章
相關標籤/搜索