开发的电脑上,有时候会与多个git账号共存的情况,比如gitee或者github等。[案例参考]
1、生成秘钥及公钥
如果用户目录没有.ssh目录,则新增一个,然后进入该目录下
# 进入 .ssh 目录
$ cd ~/.ssh
# 创建gitee的 一组秘钥,输入命令后回车
# 然后提示需要输入文件名称,这里输入id_rsa_gitee 与后续的 github账号 秘钥组分开
$ ssh-keygen -t rsa -C "xxx@gitee.com"
Enter file in which to save the key (~/.ssh/id_rsa): id_rsa_gitee
# 文件名称输入完成之后,后续直接都回车就可以了
# 然后这里创建了 两个文件 分别是 id_rsa_gitee(私钥) 和 id_rsa_gitee.pub(公钥)
2、复制公钥内容到github或者gitee上
- gitee:将 id_rsa_gitee.pub 内容复制到
设置 -> SSH公钥 -> 公钥里面,然后设置标题确定保存即可。 - github:将 id_rsa_github.pub 内容复制到
Settings -> SSH and GPG keys -> New SSH Key里面,然后设置Title确定保存即可。

3、多git账号配置
个git账号时,需要在 用户目录下的.ssh 目录下,创建一个 config 文件 配置多组ras的公钥私钥。如果没有该文件就手动创建,以下是我的配置:
# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_gitee
User git
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_github
User git
4、测试ssh key 是否成功,使用命令
$ ssh -T git@gitee.com
Hi ding! You've successfully authenticated, but GITEE.COM does not provide shell access.
$ ssh -T git@github.com
Hi ding! You've successfully authenticated, but GitHub does not provide shell access.