你們在工做中可能會遇到須要在一臺電腦上配置不一樣的 SSH Key 的狀況,例如咱們須要同時使用我的 Github 以及公司的 Gitlab 狀況,這時咱們就須要配置不一樣的 SSH Key 了。具體操做步驟以下:git
一、打開終端,切換到系統的 SSH 目錄下github
cd ~/.ssh
二、生成本身 Github 的 SSH Keyssh
ssh-keygen -t rsa -C "本身Github帳號" -f github_rsa
三、輸入 Github 帳號密碼gitlab
四、Github SSH 公鑰獲取測試
cat ~/.ssh/id_rsa.pub
五、生成公司 Gitlab 的 SSH Key網站
ssh-keygen -t rsa -C "公司Gitlab帳號" -f company_rsa
六、公司 SSH 公鑰獲取spa
cat ~/.ssh/id_rsa.pub
七、添加配置文件 config (若是有則直接編輯,沒有則建立,路徑 ~/.ssh/config),配置寫法以下:3d
# 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 添加到相應平臺code
九、測試一下是否添加成功blog
# 測試 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"