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!
相关文章
相关标签/搜索