为 Github 和 Gitee 生成、添加、部署不同的 SSH Key。
环境
git version 2.35.1.windows.2
生成 SSH Key
在命令行中分别执行生成 ssh key 的指令:
-t
指定密钥的类型,这里选择 github 和 gitee 文档中都使用的 ed25519
。
-f
指定密钥生成的目录和文件名,Windows 平台 ssh 的默认目录为用户文件夹/.ssh/
, 文件名可以按需修改。
-C "comment"
中的 comment
很多网站和文档都说写自己的邮箱,实际上不是必要的,可以根据自己的需要随意修改,只是作为一个标识。
配置 SSH Config
在 ssh 目录 ~/.ssh/
中新建一个 config
文件,添加内容:
# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_ed25519
# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_ed25519
Host
, Hostname
填写 Git 服务器域名。
IdentityFile
填写 SSH Key 私钥文件的位置。
配置 SSH Key 公钥
在
~/.ssh/
目录中找到对应的*.pub
文件,复制其中的内容。在 Github, Gitee 网站的
SSH keys
设置中添加相应的公钥。
测试 SSH 连接
|
|
测试结果:可以看到 SSH 已经连接成功.
End
- 本文分别在 GitHub 和 Gitee 上配置了不同的 SSH Key(当然也可以配置相同的 SSH Key), 在其他 Git 服务器配置更多 SSH Key 的操作也是类似的。
- 对于 Github, 我建议使用 Github 的官方命令行 Github CLI(
gh
),可以快捷登陆 Github、自动设置 SSH key、自动设置 https 仓库的凭据,非常方便。