git生成gitee和github两个不同的公钥

配置多个公钥

Windows 用户建议使用 Windows PowerShell 或者 Git Bash ,在 命令提示符 下无 cat 和 ls 命令。

1、生成公钥文件:

通过命令 ssh-keygen 生成 SSH Key:

bash 复制代码
ssh-keygen -t rsa -C "Gitee SSH Key" -f ~/.ssh/gitee_bealei_rsa


ssh-keygen -t rsa -C "Github SSH Key" -f ~/.ssh/github_bealei_rsa
  • -t key 类型
  • -C 注释
  • -f 指定用来保存密钥的文件名

输出,如:

bash 复制代码
Bealei@Y7000 MINGW64 /d/Desktop
$ ssh-keygen -t rsa -C "Gitee SSH Key" -f ~/.ssh/gitee_bealei_rsa
Generating public/private rsa key pair.
/c/Users/Administrator/.ssh/gitee_bealei_rsa already exists.
Overwrite (y/n)?

Bealei@Y7000 MINGW64 /d/Desktop
$ ssh-keygen -t rsa -C "Gitee SSH Key" -f ~/.ssh/gitee_bealei_rsa
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/Administrator/.ssh/gitee_bealei_rsa
Your public key has been saved in /c/Users/Administrator/.ssh/gitee_bealei_rsa.pub
The key fingerprint is:
SHA256:zTjk4lQJkyjatPJa8fucucXVkBPtV4o2LWj3CkcLfW4 Gitee SSH Key
The key's randomart image is:
+---[RSA 3072]----+
|     .o.  ..     |
|  o . .o . o.   .|
| + o    + ++ o o |
|o +    + +++X =  |
| o o  o S.+=.O   |
|  o .o o o. o E  |
| o   .. o  o o   |
|.   .. +    .    |
|     .*.         |
+----[SHA256]-----+
bash 复制代码
Bealei@Y7000 MINGW64 /d/Desktop
$ ssh-keygen -t rsa -C "Github SSH Key" -f ~/.ssh/github_bealei_rsa
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/Administrator/.ssh/github_bealei_rsa
Your public key has been saved in /c/Users/Administrator/.ssh/github_bealei_rsa.pub
The key fingerprint is:
SHA256:LveFZoIaZF5OdMHtNf5hb7VaH+2dhqPXiOxuIFT2QJw Github SSH Key
The key's randomart image is:
+---[RSA 3072]----+
|       .+o.      |
|        .E. o    |
|      . +.oo .   |
|     . o  ... o .|
|    o + S    o o+|
|   + + + . .  .++|
|    o + = * o *o=|
|     o o = = * ++|
|    .     =+o o  |
+----[SHA256]-----+

生成如下文件,其中gitee_bealei_rsa.pub为公钥,gitee_bealei_rsa为私钥:

2、读取公钥文件:
bash 复制代码
cat ~/.ssh/gitee_bealei_rsa.pub

输出,如:

bash 复制代码
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAB********************Zm8= Gitee SSH Key

复制终端输出的公钥。

bash 复制代码
cat ~/.ssh/github_bealei_rsa.pub

输出,如:

bash 复制代码
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAB********************Igs= Github SSH Key

复制终端输出的公钥。

3、配置仓库公钥

将生成的公钥,配置到gitee及github各自账户下

4、配置config文件

在.ssh目录下新建config.txt文件,编辑配置内容如下,去掉后缀保存文件名为config

复制代码
# gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/gitee_bealei_rsa

# github
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_bealei_rsa
5、测试连接

在本地git 的命令行中测试连接两种仓库,连接成功

bash 复制代码
# gitee
ssh -T git@gitee.com

# github
ssh -T git@github.com

输出:

bash 复制代码
# gitee
Bealei@Y7000 MINGW64 /d/Desktop
$ ssh -T git@gitee.com
Hi bealei(@bealei)! You've successfully authenticated, but GITEE.COM does not provide shell access.

# github
Bealei@Y7000 MINGW64 /d/Desktop
$ ssh -T git@github.com
Hi bealei! You've successfully authenticated, but GitHub does not provide shell access.
相关推荐
朱颜辞镜花辞树‎10 分钟前
GitHub 操作指南:项目协作与自动化工作流实践
运维·自动化·github
音元系统1 小时前
Copilot 在 VS Code 中的免费替代方案
python·github·copilot
ai小鬼头5 小时前
AIStarter3.0.2全新发布:解锁用户、创作者、共创模式,轻松玩转AI项目
人工智能·程序员·github
素雪风华6 小时前
Jenkins+Gitee+Docker容器化部署
java·docker·gitee·jenkins·springboot·持续部署
寻月隐君7 小时前
用 Rust 实现 HTTPie:一个现代 CLI 工具的构建过程
后端·rust·github
徐小夕8 小时前
开源了一款基于Nextjs实现的精美CRM系统, 卷王程序员狂喜!
前端·react.js·github
Casia_Dominic10 小时前
【三维重建工具】NeRFStudio、3D GaussianSplatting、Colmap安装与使用指南
git·3d·github·点云
构建的乐趣10 小时前
运行ssh -T git@github.com报错
git·ssh·github
寻月隐君19 小时前
想用 Rust 开发游戏?这份超详细的入门教程请收好!
后端·rust·github
SoFlu软件机器人1 天前
Cursor、飞算JavaAI、GitHub Copilot、Gemini CLI 等热门 AI 开发工具合集
人工智能·github·copilot