在 Github 和 Gitee 配置多个 SSH Key

在 Github 和 Gitee 生成、添加、部署不同的 SSH Key

环境

git version 2.35.1.windows.2

生成 SSH Key

bash中分别执行生成 ssh key 的指令,

1
2
ssh-keygen -t ed25519 -f ~/.ssh/github_id_ed25519 -C "Github-220210"
ssh-keygen -t ed25519 -f ~/.ssh/gitee_id_ed25519 -C "Gitee-220210"

-t指定密钥的类型,这里选择 github 和 gitee 文档中都使用的ed25519

-f指定密钥生成的目录和文件名,Windows 平台 ssh 的默认目录为用户文件夹/.ssh/, 文件名可以按需修改。

-C "comment"中的comment很多网站和文档都写自己的邮箱,事实上可以随意修改,只是作为一个标识。

配置 SSH Config

在 ssh 目录~/.ssh/中新建一个config文件,添加内容:

1
2
3
4
5
6
7
8
9
10
11
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_ed25519

# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_id_ed25519

Host, Hostname填写 Git 服务器域名

IdentityFile填写 SSH Key 私钥文件的位置

配置 SSH Key 公钥

  1. ~/.ssh/目录中找到对应的*.pub文件,复制其中的内容

  2. 在 Github, Gitee 网站的SSH keys设置中添加相应的公钥

测试 SSH 连接

1
2
ssh -T [email protected]
ssh -T [email protected]

测试结果:可以看到 SSH 已经连接成功

1
2
3
Hi Lixx! You've successfully authenticated, but GitHub does not provide shell access.

Hi Lixx! You've successfully authenticated, but GITEE.COM does not provide shell access.

小结

  • 本文分别在 GitHub 和 Gitee 上配置了不同的 SSH Key(当然也可以配置相同的 SSH Key), 在其他 Git 服务器配置更多 SSH Key 的操作也是类似的