1. 程式人生 > >同時使用多個 SSH Key 管理不同 Github 賬號的 Repo (設定 Host 別名)

同時使用多個 SSH Key 管理不同 Github 賬號的 Repo (設定 Host 別名)

Github 禁止不同的賬號使用相同的 SSH Key ,如果有多個 SSH Key ,使用 SSH 而非 HTTPS 方式推送 Git Repo 的時候,如果每次都手動替換預設的 id_rsa 會非常的不方便。

首先建立新使用者的 SSH Key :

cd ~/.ssh
ssh-keygen -t rsa -C "[email protected]"

然後將其新增到 SSH Agent 代理,以便能夠識別:

ssh-add ~/.ssh/id_rsa_2

然後新建或者修改 ~/.ssh/config :

touch ~/.ssh/config
# 預設的 github 使用者
Host github.com
 HostName github.com
 User git
 IdentityFile ~/.ssh/id_rsa
 
# 使用 id_rsa_2 驗證的 github 別名
Host github2
 HostName github.com
 User git
 IdentityFile ~/.ssh/id_rsa_2

今後,只需要在 git clone 的時候替換伺服器部分為別名即可,即把 [email protected]:dimpurr/Clearision.git 替換為 github2:dimpurr/Clearision.git 。可以測試一下:

$ ssh -T github2
Hi dimpurr! You've successfully authenticated, but GitHub does not provide shell
 access.

機器學習 / 資料科學 / 前端開發 / 互動設計 / 數學物理 / 本子畫師 / 帝都大學生 / 希望和有趣的人做有趣的事。