你們在工做中可能會遇到須要在一臺電腦上配置不一樣的 SSH Key 的狀況,例如咱們須要同時使用我的 Github 以及公司的 Gitlab 狀況,這時咱們就須要配置不一樣的 SSH Key 了。具體操做步驟以下:git
一、打開終端,切換到系統的 SSH 目錄下github
cd ~/.ssh
複製代碼
二、生成本身 Github 的 SSH Keybash
ssh-keygen -t rsa -C "本身Github帳號" -f github_rsa
複製代碼
三、輸入 Github 帳號密碼ssh
四、Github SSH 公鑰獲取gitlab
cat ~/.ssh/id_rsa.pub
複製代碼
五、生成公司 Gitlab 的 SSH Key測試
ssh-keygen -t rsa -C "公司Gitlab帳號" -f company_rsa
複製代碼
六、公司 SSH 公鑰獲取網站
cat ~/.ssh/id_rsa.pub
複製代碼
七、添加配置文件 config (若是有則直接編輯,沒有則建立,路徑 ~/.ssh/config),配置寫法以下:ui
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_rsa
# gitlab
Host gitlab.com
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/company_rsa
複製代碼
八、把以前生成的 SSH Key 添加到相應平臺spa
九、測試一下是否添加成功code
# 測試 GitHub
ssh -T git@github.com
# 測試 GitLab
ssh -T git@gitlab.com
複製代碼
十、不一樣項目切換不一樣的 ssh
取消全局 用戶名/郵箱設置,並進入項目文件夾單獨設置
git config –global –unset user.name
git config –global –unset user.email
# 單獨設置每一個repo 用戶名/郵箱
git config user.email "xxxx@xx.com"
git config user.name "xxxx"
複製代碼