關鍵詞:Github GitLab 雙賬戶 設置 同時起效
0. 具體思路
在 config 文件中設置不同的 Host ,然後對應使用不同的 .pub 公鑰文件
1. 生成對應不同賬號的祕鑰
1.1 生成 github 祕鑰
$ ssh-keygen -t rsa -f ~/.ssh/id_rsa_github -C "GithubAccount"
1.2 生成 gitlab 祕鑰
$ ssh-keygen -t rsa -f ~/.ssh/id_rsa_gitlab -C "GitlabAccount"
2. 修改 config 配置文件
2.1 切換到 ~/.ssh 目錄
$ cd ~/.ssh
$ ls
config id_rsa_github id_rsa_github.pub id_rsa_gitlab id_rsa_gitlab.pub known_hosts
2.2 修改配置文件
$ sudo vim config
# github
# host 與 hostname 需要相同
Host github.com
HostName github.com
# 你的github賬號
User GithubAccount
# github對應的rsa祕鑰文件
IdentityFile ~/.ssh/id_rsa_github
# gitlab
# host 與 hostname 需要相同
Host gitlab.com
HostName gitlab.com
# 你的gitlab賬號
User GitlabAccount
# gitlab對應的rsa祕鑰文件
IdentityFile ~/.ssh/id_rsa_gitlab
3. 在 github 和 gitlab 上添加個人公鑰
3.1 打開 Settings
3.2 新建 SSH Key
3.3 填寫 title 和 key
將對應的 xx.pub 文件內容複製到 key 裏面就行
比如,我要配置的是 github 賬號
我就複製 ~/.ssh/id_rsa_github.pub 的內容到下圖中的 Key 中去
4. 測試
4.1 github 測試
$ ssh -T [email protected]
Hi GithubAccount! You've successfully authenticated, but GitHub does not provide shell access.
# 出現上邊這句,表示連接成功
4.2 gitlab 測試
$ ssh -T [email protected]
Hi GitlabAccount!