前言:
很多時候压真,我們一臺機(jī)器上要使用多個Git庫娩嚼,比如 GitHub、GitLab滴肿、OSChina岳悟、CSDN、以及 自己公司的泼差。那么 rsa就要有多份贵少。那么該如何讓這些共同存在呢?
原理就是:建立多個不同的rsa 然后 在ssh config中分別不同的配置堆缘。
操作步驟:
一滔灶、生成公鑰和私鑰(SSH Keys)
說明:SSH key 可以讓你的電腦和 遠(yuǎn)程倉庫 之間建立安全的加密連接,從而實(shí)現(xiàn)免密登錄吼肥,能夠在不輸入Git密碼的情況下录平,進(jìn)行版本控制。
- 終端輸入如下命令:
// 使用如下命令來生成 SSH Keys
ssh-keygen -t rsa -C "你的Git賬號郵箱地址"
- 執(zhí)行完這條命令之后, 會彈出如下提示:
Enter file in which to save the key (/Users/renbo/.ssh/id_rsa):文件名
在這句話后面可以自定義要生成的文件名(如:github_rsa潜沦、oschina_rsa萄涯、csdn_rsa等);如果不輸入文件名唆鸡,默認(rèn)會生成id_rsa.pub
(公鑰)涝影、id_rsa
(私鑰)
下一步... 不輸入密碼直接回車。
二争占、將公鑰添加到網(wǎng)站
// 打開公鑰文件燃逻,將公鑰(`id_rsa.pub` 文件內(nèi)容)添加到對應(yīng)網(wǎng)站(如:GitHub / GitLab / OSChina / 自己公司的)上的SSH keys中。
open ~/.ssh/id_rsa.pub
四臂痕、修改 ssh config 配置文件
1.打開config文件
// 編輯config文件
vi ~/.ssh/config
2.添加如下內(nèi)容
# 編寫配置文件伯襟,支持多個賬號自動登錄
# 配置GitLab,公司賬號1:
Host git-server
HostName 192.168.68.85
Port 22
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/work1_rsa
# 配置GitLab握童,公司賬號2:
Host git-server
HostName 192.168.75.30
Port 22
User git
PreferredAuthentications publickey
IdentityFile ~/.ssh/work2_rsa
# 配置github
Host github.com
HostName ssh.github.com # 以前使用 github.com姆怪,現(xiàn)在會連接超時。ssh: connect to host github.com port 22: Operation timed out
User irenb@qq.com
IdentityFile ~/.ssh/github_rsa
# 配置碼云
Host gitee.com
HostName gitee.com
User irenb@qq.com
IdentityFile ~/.ssh/oschina_rsa
# 配置coding
Host coding.net
HostName coding.net
User developer@irenb.com
IdentityFile ~/.ssh/coding_rsa
五、測試SSH連接
- 終端輸入如下命令:
ssh -T git@github.com
- 輸入yes回車稽揭,如果有警告俺附,根據(jù)提示進(jìn)行操作。
如:下面需要添加GitHub的Host地址13.229.188.59 github.com
The authenticity of host 'github.com (13.229.188.59)' can't be established.
RSA key fingerprint is SHA256:xxxxxxxxWGl7E1IGOCspRomTxxxxxxxxxx8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com,13.229.188.59' (RSA) to the list of known hosts.
Hi 91renb! You've successfully authenticated, but GitHub does not provide shell access.
六溪掀、刪除本地全局設(shè)置
如果之前使用過程中使用過git config --global user.name "" 或者 git config --global user.email 命令事镣,git會在本地產(chǎn)生一個.gitconfig文件,這個文件中保存了全局的git帳號信息揪胃,應(yīng)該刪除掉璃哟。