一、背景
项目托管的仓库多了,使用的账号多了,自然用到的key就不同了,比如gitlab,bitbucket, github, 公司的code仓库等,所以管理好key很重要。
二、git 配置多个SSH-Key
(1)生成一个公司用的SSH-Key
$ ssh-keygen -t rsa -C "1email@company.com” -f ~/.ssh/id-rsa
(2)生成一个github用的SSH-Key
$ ssh-keygen -t rsa -C "2email@github.com” -f ~/.ssh/github-rsa
此时,.ssh目录下应该有4个文件:id-rsa和id-rsa.pub,github-rsa和github-rsa.pub,分别将他们的公钥文件(id-rsa.pub,github-rsa.pub)内容配置到对应的code仓库上
(3)添加私钥
$ ssh-add ~/.ssh/id-rsa
$ ssh-add ~/.ssh/github-rsa
执行ssh-add时提示”Could not open a connection to your authentication agent”,可以现执行命令:
$ ssh-agent bash
# 然后再运行ssh-add命令。
(4)修改配置文件
# 若.ssh目录下无config文件,那么创建
touch config
# 添加以下内容
Host github.com HostName github.com PreferredAuthentications publickey IdentityFile ~/.ssh/github-rsa Host gitlab.com HostName git.timelink.cn PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa
(5)测试