Git配置多个SSH-Key
背景
当有多个账号时,比如:
- 一个gitLab,用于公司内部开发
- 一个github,用于个人开发
解决
1.生成一个公司用的SSH-Key
$ ssh-keygen -t rsa -C '企业邮箱' -f ~/.ssh/gitLab_id_rsa
2.生成一个自己用的SSH-Key
$ ssh-keygen -t rsa -C '个人邮箱' -f ~/.ssh/github_id_rsa
3.在~/.ssh目录下新建名为config的文件,添加如下内容(IdentityFiles指定私钥路径)
# github
HOST github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa
# gitLab
HOST gitLab服务器域名
HostName gitLab服务器域名
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitLab_id_rsa
4.用ssh命令测试
$ ssh -T git@github.com
成功如图