git 配置多个SSH-Key

时间:2022-11-18 19:29:18

项目托管的仓库多了,使用的账号多了,自然用到的key就不同了,比如gitlab,bitbucket, github, 公司的code仓库等,所以管理好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命令。

# 可以通过 ssh-add -l 来确私钥列表
$ ssh-add -l
# 可以通过 ssh-add -D 来清空私钥列表
$ ssh-add -D

4,修改配置文件

# 若.ssh目录下无config文件,那么创建
touch config

# 添加以下内容
# 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

5,测试

$ ssh -T git@github.com

# 输出
Welcome to GitLab, your name!