問題說明
當我們使用 Git 進行代碼版本控制時,經常出現一臺電腦需要連接多個Git 賬號的情況,此時需要在一臺電腦上生成多份 ssh 私鑰和密鑰,同時映射多個 Git 賬號;這里我們需要同時連接 GitHub,碼云,兩個賬號;
配置
1.生成 ssh 密鑰檔案;
ssh-keygen -t rsa -C "Zbc521.gitee.com" -f ~/.ssh/id_rsa_gitee
ssh-keygen -t rsa -C "Zbc521.github.com" -f ~/.ssh/id_rsa_github
圖示:
2.通過ssh-add添加密鑰至ssh-agent;
ssh-add ~/.ssh/id_rsa_gitee
ssh-add ~/.ssh/id_rsa_github
3.添加 config 組態檔分別映射不同的 Git 賬號;
進入~/.ssh目錄,新建config檔案,添加如下內容:
# GitHub 公鑰
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github
# Gitee 碼云 公鑰
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_gitee
4.向你的 Git 帳戶中添加 SSH Key 認證,這里以 GitHub 賬號為例 ;
- 登錄 GitHub 賬號,打開 /settings/SSH and GPG keys,新建 SSH key;
- 將 本地生成的 id_rsa_github.pub 公鑰檔案內容,拷貝到 GitHub 中;
5.測驗連接配置是否成功;
輸入命令:
ssh -T [email protected]
出現如下資訊,表示成功:
Hi Zbc521! You've successfully authenticated, but GitHub does not provide shell access.
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/417045.html
標籤:Java
