Git配置多個SSH Key
Git配置多個SSH Key
打開git bash:
添加公鑰
生成第一個SSH Key
ssh-keygen -t rsa -C "[email protected]” -f ~/.ssh/gitlab_rsa
生成第二個SSH Key
ssh-keygen -t rsa -C "[email protected]” -f ~/.ssh/github_rsa
此時,.ssh目錄下應該有4個文件:gitlab_rsa和gitlab_rsa.pub,github_rsa和github_rsa.pub,
分別將他們的公鑰文件(gitlab_rsa.pub,github_rsa.pub)內容配置到對應的code倉庫上。
配置公鑰:登錄github或你的代碼託管平臺。右上角你的賬號登錄個人信息處,點擊settings,選SSH Keys。
Title自己定義,Key輸入你的公鑰文件複製的內容,最後點擊Add SSH Key按鈕即可。
添加私鑰
ssh-add ~/.ssh/gitlab_rsa
ssh-add ~/.ssh/github_rsa
## 如果執行ssh-add時提示”Could not open a connection to your authentication agent”,
## 可以先執行命令:
ssh-agent bash
## 重新執行ssh-add命令
## 添加後我們可以通過 ssh-add -l 來確私鑰列表
ssh-add -l
## 如果想刪除私鑰列表,可以通過 ssh-add -D 來清空私鑰列表
ssh-add -D
修改配置文件
如果.ssh目錄(就是私鑰所在的文件夾)下無config文件,那麼創建,
touch config ## 創建一個config文件
vi config ## 編輯
## 在config文件添加以下內容
# github
Host github.com
Port 22
HostName github.com
PreferredAuthentications publickey
IdentityFile C:/Users/DELL/.ssh/github_rsa
User [email protected]
# gitlab
Host gitlab.com
Port 22
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile C:/Users/DELL/.ssh/gitlab_rsa
User [email protected]
# 配置文件參數
# Host : Host可以看作是一個你要識別的模式,對識別的模式,進行配置對應的的主機名和ssh文件(可以直接填寫ip地址)
# Port: 端口號(默認22)
# HostName : 要登錄主機的主機名(建議與Host一致)
# User : 登錄名(如gitlab的username)
# IdentityFile : 指明上面User對應的identityFile路徑
測試
ssh -T [email protected]
如果出現“Enter passphrase for key”提示,請輸入密碼。
輸出“Hi zjie0723! You’ve successfully authenticated” 或者 “Welcome to GitLab”表示成功。