git 配置多個SSH-Key

git 配置多個SSH-Key

項目託管的倉庫多了,使用的帳號多了,天然用到的key就不一樣了,好比 gitlabgitee, github, 公司的code倉庫等,因此管理好key很重要。

一、生成一個gitlab用的SSH-Key

$ ssh-keygen -t rsa -C "1_email@company.com」 -f ~/.ssh/gitlab-rsa

二、生成一個github用的SSH-Key

$ ssh-keygen -t rsa -C "2_email@github.com」 -f ~/.ssh/github-rsa

此時,.ssh目錄(通常在C:\Users\Administrator\.ssh目錄下) 下應該有4個文件:gitlab-rsagitlab-rsa.pubgithub-rsagithub-rsa.pub,分別將他們的公鑰文件(gitlab-rsa.pubgithub-rsa.pub)內容配置到對應的code倉庫上git

具體配置SSH祕鑰的位置在github/gitlab網站的 我的信息 ----> 設置/settings ----> SSH公鑰/SSH public key 中設置。github

三、添加私鑰

$ ssh-add ~/.ssh/gitlab-rsa 
$ ssh-add ~/.ssh/github-rsa

若是執行ssh-add時提示」Could not open a connection to your authentication agent」,能夠現執行命令:bash

$ ssh-agent bash
# 而後再運行ssh-add命令。

##  下面可省略.
# 能夠經過 ssh-add -l 來確私鑰列表
$ ssh-add -l
# 能夠經過 ssh-add -D 來清空私鑰列表
$ ssh-add -D

四、修改配置文件

# 若.ssh目錄下無config文件,那麼建立
touch config

# config文件中添加如下內容
# gitee 碼雲
Host gitee.com ## Host 這個指明的是HOST地址,也就是項目的HostName,如:git@gitee.com:ghostgithub/xUtils.git   gitee.com就是其對應的Host(訪問的項目的地址)
HostName gitee.com  ## HostName 就是訪問的地址,如:https://gitee.com/   就是其HostName(IP地址,訪問的碼雲的網頁上的url地址)  (https://建議不要加上)
PreferredAuthentications publickey  ## 指明配置的是公鑰
IdentityFile ~/.ssh/gitee-rsa ## 指定弓腰的位置及文件
# gitlab
Host gitlab.com
HostName gitlab.com  
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_rsa

五、測試

$ ssh -T git@github.com

輸出ssh

Welcome to GitLab, your name!
相關文章
相關標籤/搜索