Git 配置多个SSH-Key

image.png

场景

当有多个git账号时,比如:
a. 一个gitee,用于公司内部的工作开发;
b. 一个github,用于自己进行一些开发活动;
c. 一个gitlab,用于自己进行一些开发活动;
d. 一个gitcoding,用于自己进行一些开发活动;

解决

  • 生成公司用的SSH-Key
1
$ ssh-keygen -t rsa -C 'zhangyuhai@company.com' -f ~/.ssh/gitee_id_rsa
  • 生成自己用的SSH-Key
1
2
3
$ ssh-keygen -t rsa -C '15736736888@163.com' -f ~/.ssh/github_id_rsa
$ ssh-keygen -t rsa -C '15736736888@163.com' -f ~/.ssh/gitlab_id_rsa
$ ssh-keygen -t rsa -C '15736736888@163.com' -f ~/.ssh/gitcoding_id_rsa
  • 在 ~/.ssh 目录下新建一个config文件,添加如下内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#  gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_id_rsa
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa
# gitlab
Host gitlab.com
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitlab_id_rsa
# gitcoding
Host e.coding.net
HostName e.coding.net
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitcoding_id_rsa

其中Host和HostName填写git服务器的域名,IdentityFile指定私钥的路径

  • 用ssh命令分别测试
1
2
$ ssh -T git@gitee.com
Hi xxx! You've successfully authenticated, but GITEE.COM does not provide shell access.

这里以gitee为例,成功的话会返回如上内容

转[https://gitee.com/help/articles/4229# article-header0](https://gitee.com/help/articles/4229# article-header0)

关联

[[Git 常用命令]]
[[Git Gitee仓库自动部署]]

-------------本文结束感谢您的阅读-------------
0%