Git 配置多个 SSH key 实际问题 在日常工作中我们会使用不同的代码平台管理代码。如:
公司自己搭建的GitLab GitHub 码云Gitee 生成SSH key 因此我们要配置不同的SSH key对应不同的环境,具体的操作如下:
1、生成公司GitLab SSH key
SSH keygen -t rsa -f ~/.ssh/id_rsa.gitlab -C "email" 2、生成GitHub SSH key
SSH keygen -t rsa -f ~/.ssh/id_rsa.github -C "email" 3、生成码云Gitee SSH key
SSH keygen -t rsa -f ~/.ssh/id_rsa.gitee -C "email" 以GitHub为例: id_rsa.github就是我们指定的文件名,这时~/.ssh目录下会多出id_rsa.github和id_rsa.github.pub两个文件,id_rsa.github.pub里保存的就是我们要使用的SSH key。
$ ls -lh ~/.ssh/
-rw-r--r-- 1 yu Administ 1.6k Nov 27 20:55 id_rsa.github -rw-r--r-- 1 yu Administ 398 Nov 27 20:55 id_rsa.github.pub 创建并配置config文件 // 创建config文件 touch ~/.ssh/config
// 添加如下配置
github
Host github.com HostName github.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa.github
gitee
Host gitee.com HostName gitee.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa.gitee
公司 gitlab
Host 10.10.10.28 HostName 10.10.10.28 PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa.gitlab 添加SSH key到各代码平台 以GitHub为例,在GitHub的账户设置中添加SSH Key:
GitHub添加SSH key
测试SSH key是否配置成功 如测试GitHub配置,输出如下信息,则表示通过
$ ssh -T git@github.com Hi shark-yu! You've successfully authenticated, but GitHub does not provide shell access. 如需添加其它,按上述步骤生成key,并修改config配置文件即可。