Git配置多个SSH-Key

背景

有时候我们的项目托管在多个平台上, 这就需要为每个托管平台设置相应的 SSH-key

当有多个git账号时,比如:

  • 一个 gitee 用于公司内部的工作开发
  • 一个 github 用于自己的项目开发

解决方法

1.生成一个 giteeSSH-Key
$ ssh-keygen -t rsa -C 'x@x.com' -f ~/.ssh/gitee_id_rsa

此命令会生成 gitee_id_rsagitee_id_rsa.pub两个文件
gitee_id_rsa.pub 添加到 gitee 个人中心里的 SSH 公钥里即可 教程传送门

2.生成一个 githubSSH-Key
$ ssh-keygen -t rsa -C 'x@x.com' -f ~/.ssh/github_id_rsa

此命令会生成 github_id_rsagithub_id_rsa.pub两个文件
github_id_rsa.pub 添加到 github 个人中心里的 SSH keys 里即可

3.在 ~/.ssh 目录下新建一个config文件

添加如下内容, 其中 HostHostName 填写 git 服务器的域名 IdentityFile指定私钥的路径

# 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

用ssh命令分别测试

$ ssh -T git@github.com
Hi HoeXHe! You've successfully authenticated, but GitHub does not provide shell access.
$ ssh -T git@gitee.com
Hi Hoe! You've successfully authenticated, but GITEE.COM does not provide shell access.

这样就完成了~

git

我来吐槽

*

*

已有 10 条评论

  1. YuYe

    有用有用!

  2. shansan

    正好需要😁

  3. 张波博客

    喜迎2019,元旦快乐

    1. Hoe

      2019万事如意☺

  4. 熊猫小A

    原来还能这样的

    1. Hoe

      是啊,这样可以管理多个git平台的项目😁

  5. 张波博客

    有时间跟你学学

    1. Hoe

      相互学习, 一起进步哈

  6. w4ctech

    get到了,很实用。