摘要
https://wylu.github.io/posts/e186bfe8/
大多數人有時會需要在同一臺機器上管理多個 github 賬號,本文以管理兩個 github 賬號爲例,記錄了配置 git 的過程,方便下次遇到相同問題時,能節省處理時間。
約定
- github 賬號 1: wylu, 對應郵箱爲 [email protected]
- github 賬號 2: 15wylu, 對應郵箱爲 [email protected]
生成第一對 SSH key
第一個密鑰對以 wylu 賬號爲例,在生成之前,我們可以通過 ls -al ~/.ssh
查看是否已有 SSH key。
例如:
$ ls -al ~/.ssh
total 24
drwx------ 2 wylu wylu 4096 Nov 24 2018 .
drwxr-xr-x 66 wylu wylu 4096 Jan 7 00:00 ..
-rw------- 1 wylu wylu 1675 Nov 24 2018 id_rsa
-rw-r--r-- 1 wylu wylu 398 Nov 24 2018 id_rsa.pub
-rw-r--r-- 1 wylu wylu 4841 Dec 22 21:46 known_hosts
如果 ~/.ssh
下有 “id_rsa”(私鑰) 和 “id_rsa.pub”(公鑰),說明之前已生成過 SSH key 了,我們可以直接複用這個密鑰對(SSH key)。
如果你沒有 SSH key 或者你想要重新生成一個 SSH key,可以執行以下命令生成:
- 不存在 SSH Key 時。
遇到輸入一路回車使用默認配置。
$ ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/pi/.ssh/id_rsa):
Created directory '/home/pi/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/pi/.ssh/id_rsa.
Your public key has been saved in /home/pi/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:nGAMmHYEKfJ3/iEDLxOsg1xinfg/IzuYTQVggEnVSWQ [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|==+B*E. |
|* =.o= |
|.+ooo + |
| +.+=o.o . |
|o.+o.* S |
|..ooo = . |
| =..o + . |
| o + + . |
| .+ o |
+----[SHA256]-----+
- 已存在 SSH Key 時。
當詢問是否 Overwrite (y/n)?
已存在的 id_rsa
時,直接輸入 y
然後回車,其餘的一路回車使用默認配置就好。
$ ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/home/pi/.ssh/id_rsa):
/home/pi/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/pi/.ssh/id_rsa.
Your public key has been saved in /home/pi/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:rPh6Y081aG7URerEmd6aA5FVYi+1w0KPHR59KggFwLk [email protected]
The key's randomart image is:
+---[RSA 2048]----+
| ..o.o*.*. |
| o .* # +. .|
| .o.X.X o |
| E. B.=... |
| S * .. |
| . = o + |
| . . + + |
| .+o . |
| .+.o. |
+----[SHA256]-----+
我們以此 SSH Key 作爲默認的密鑰對。
生成第二對 SSH Key
假設我們已經擁有了一對 SSH Key 對應於管理 wylu 的 github 賬號,現在需要生成另外一對 SSH Key 用於管理 15wylu 的 github 賬號。
執行下面的命令生成另一對 SSH Key:
$ cd ~/.ssh
$ ssh-keygen -t rsa -C "[email protected]" -f "15wylu_id_rsa"
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in 15wylu_id_rsa.
Your public key has been saved in 15wylu_id_rsa.pub.
The key fingerprint is:
SHA256:6cQHRLvxRovTNQgPh1JiMCe4U1tFgkxe541ZGN+NiHA [email protected]
The key's randomart image is:
+---[RSA 2048]----+
| +=oB=E+o |
| ..+*oOo& + o |
| o.o .O B = . |
| o . . X o . |
| . S * |
| o + |
| . |
| |
| |
+----[SHA256]-----+
方法與上面生成第一對 SSH Key 一樣,一路回車,這裏使用 -f
選項的作用是指定生成的密鑰對文件的名稱,以避免覆蓋之前的密鑰對文件。從輸出信息中可以看到,此次生成的私鑰文件名爲 15wylu_id_rsa
,公鑰文件名爲 15wylu_id_rsa.pub
。這樣同一臺機器下,wylu 和 15wylu 都擁有了各自的 SSH Keys:
$ ls -al ~/.ssh
total 24
drwx------ 2 pi pi 4096 Jan 6 17:00 .
drwxr-xr-x 7 pi pi 4096 Jan 6 16:55 ..
-rw------- 1 pi pi 1823 Jan 6 17:00 15wylu_id_rsa
-rw-r--r-- 1 pi pi 396 Jan 6 17:00 15wylu_id_rsa.pub
-rw------- 1 pi pi 1823 Jan 6 16:38 id_rsa
-rw-r--r-- 1 pi pi 396 Jan 6 16:38 id_rsa.pub
在對應的 github 賬號中添加 SSH key
參考文章 Git配置SSH Key 中的 添加 SSH Key 到 Github 部分,分別將 id_rsa.pub
文件中的公鑰添加到 wylu 賬號,將 15wylu_id_rsa.pub
文件中的公鑰添加到 15wylu 賬號。
使用 ssh-agent 註冊新的 SSH 密鑰
ssh-agent:是一個可以控制和保存公鑰身份驗證所使用的私鑰的程序,可以理解爲一個密鑰管理器,ssh-agent 是一個守護進程(daemon),設計它的唯一目的就是對解密的專用私鑰進行高速緩存。
如果沒有正在運行的 ssh-agent,則執行 eval "$(ssh-agent -s)"
以確保 ssh-agent 運行。
然後使用 ssh-add 添加私鑰:
$ ssh-add ~/.ssh/id_rsa
$ ssh-add ~/.ssh/15wylu_id_rsa
使用 ssh-add 將私鑰交給 ssh-agent 保管,其他程序需要身份驗證時,ssh 將直接從 ssh-agent 獲取私鑰,而不會提示你輸入密碼口令以獲取私鑰,這樣就不需要經常輸入密碼了。可以使用 ssh-add -l
查看已添加到 ssh-agent 中的密鑰。
接下來,需要使 ssh-agent 對不同的 SSH 主機使用各自的 SSH 密鑰,這是關鍵部分,我們有兩種不同的方法(使用其中一種即可):
- 使用 SSH 配置文件
- 在 ssh-agent 中只有一個活動的 SSH 密鑰
使用 SSH 配置文件(推薦)
使用 ~/.ssh/config
作爲我們的配置文件,如果文件不存在,我們就創建它。
$ cd ~/.ssh/
$ touch config
編輯 ~/.ssh/config
,使相關 GitHub 帳戶的配置類似於以下內容:
# github: wylu, email: [email protected]
# the default config
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
# github: 15wylu, email: [email protected]
Host 15wylu.github.com
HostName github.com
User git
IdentityFile ~/.ssh/15wylu_id_rsa
- Host: 別名,用於區分多個 git 賬號,可隨意
- HostName: 要連接的服務器的主機名
- IdentityFile: ssh 連接使用的私鑰
上面的配置要求 ssh-agent:
- 使用
id_rsa
作爲使用 @github.com 的任何 Git URL 的密鑰 - 對於使用 @15wylu.github.com 的任何 Git URL,則使用
15wylu_id_rsa
密鑰
其規則就是:從上至下讀取 config 的內容,在每個 Host 下尋找對應的私鑰,你可以根據需要添加更多的 Host。
clone 新倉庫
注意:如果使用 SSH 配置文件前,倉庫已存在,參考下方 “對於已存在的倉庫” 的內容。
這裏以上面的配置爲例,假設要克隆 15wylu 賬號的一個項目,原來使用的命令如下:
$ [email protected]:15wylu/15wylu.github.io.git
但是經過配置,我們已經將 15wylu 的 Host 設爲了 15wylu.github.com
,而不再是原來的 github.com
,所以相應地 clone 的命令也變成如下:
(請注意克隆時我們使用了 SSH 配置中使用的主機名)
$ [email protected]:15wylu/15wylu.github.io.git
對於已存在的倉庫
假設在配置之前,我們就已經 clone 了倉庫。
首先使用 git remote -v
列出本地倉庫對應的遠程庫,檢查該 URL 是否與要使用的 GitHub 主機匹配,否則更新遠程原始 URL:
以 15wylu 賬號的倉庫爲例:
$ git remote set-url origin [email protected]:15wylu/15wylu.github.io.git
確保 @
和 :
之間的字符串與我們在 SSH 配置中指定的主機(Host)匹配。
對於本地創建新的倉庫
在項目文件夾中使用 git init
中初始化目錄爲一個 Git 倉庫。然後在 GitHub 帳戶中創建新的倉庫,將其作爲遠程庫添加到本地倉庫中:
同樣以 15wylu 賬號爲例:
$ git remote add origin [email protected]:15wylu/remote_repo_name.git
確保 @
和 :
之間的字符串與我們在 SSH 配置中指定的主機(Host)匹配。將初始提交推送到 GitHub 倉庫:
$ git add .
$ git commit -m "initial commit"
$ git push -u origin master
小結
在這裏,我們實際上是在爲不同的主機(Host)添加 SSH 配置規則,說明要在哪個域中使用哪個身份文件(SSH key)。
測試
$ ssh -T [email protected]
The authenticity of host 'github.com (13.250.177.223)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,13.250.177.223' (RSA) to the list of known hosts.
Hi wylu! You've successfully authenticated, but GitHub does not provide shell access.
$ ssh -T [email protected]
Hi 15wylu! You've successfully authenticated, but GitHub does not provide shell access.
第一次執行 ssh -T [email protected]
時會有一段警告,直接輸入 yes 回車既可。從輸出的信息可以看到,wylu 和 15wylu 都認證成功了。
在 ssh-agent 中只有一個活動的 SSH 密鑰
這種方法不需要 SSH 配置規則,而是我們手動確保 ssh-agent 在執行任何 Git 操作時僅附加了相關的密鑰。
ssh-add -l
將列出附加到 ssh-agent 的所有 SSH 密鑰,刪除所有這些密鑰後,添加你將要使用的一個密鑰,以確保 ssh-agent 中只有一個活動的密鑰。
例如,假設你要使用 git 操作 15wylu 賬號,首先使用下面的命令刪除 ssh-agent 中的所有密鑰:
$ ssh-add -D
然後添加 15wylu 賬號對應的密鑰:
$ ssh-add ~/.ssh/15wylu_id_rsa
此時,ssh-agent 已將密鑰映射到 15wylu GitHub 帳戶,當我們使用 git 推送、克隆等操作時都是對 15wylu 賬號的倉庫進行操作。
類似地,假如要使用 git 操作 wylu 賬號,則:
$ ssh-add -D
$ ssh-add ~/.ssh/id_rsa
爲本地倉庫設置 git remote url
clone 或創建本地 Git 倉庫後,請確保 Git 配置的用戶名和電子郵件正是你想要的。GitHub 通過 commit 描述附隨的電子郵件 ID 來標識任何提交的作者。
要在本地 Git 目錄(本地 Git 倉庫下)中列出配置名稱和電子郵件,請執行 git config user.name
和 git config user.email
。如果找不到,則需要進行相應的更新,如:
$ git config user.name "wylu"
$ git config user.email "[email protected]"
References
How to manage multiple GitHub accounts on a single machine with SSH keys