1. 程式人生 > 其它 >Git配置多個SSH-Key

Git配置多個SSH-Key

背景

當有多個git賬號時,比如:

a. 一個gitee,用於公司內部的工作開發;
b. 一個github,用於自己進行一些開發活動;

解決方法

  1. 生成一個公司用的SSH-Key
$ ssh-keygen -t rsa -C '[email protected]' -f ~/.ssh/gitee_id_rsa
  1. 生成一個github用的SSH-Key
$ ssh-keygen -t rsa -C '[email protected]' -f ~/.ssh/github_id_rsa
  1. 在 ~/.ssh 目錄下新建一個config檔案,新增如下內容(其中Host和HostName填寫git伺服器的域名,IdentityFile指定私鑰的路徑)
# 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/github_id_rsa

4.用ssh命令分別測試

$ ssh -T [email protected]
$ ssh -T [email protected]

這裡以gitee為例,成功的話會返回下圖內容

用ssh命令分別測試

ssh -T [email protected]
ssh -T [email protected]

這裡以gitee為例,成功的話會返回下圖內容
5. 使用

git clone [email protected]:demo/test.git
git clone [email protected]:demo/test.git

ok

參考:https://gitee.com/help/articles/4229#article-header1