(转)在GitHub多个帐号上添加SSH公钥

GitHub后台可以添加多个SSH Keys,但是同一个SSH Keys只能在添加在一个帐号上(添加时提示“Key is already in use”)。理由很容易想到,SSH公钥使用时相当于用户名密码,不可能两个不同的帐号使用同一个用户名密码。要想在多个GitHub帐号上添加公钥,就要在本地生成多个SSH Keys,每个GitHub帐号对应一个不同的SSH Keys。步骤如下:
1、生成一个新的SSH KEY

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
larry@thk:~$ ssh-keygen -t rsa -C 'git@webmaster.me'
Generating public/private rsa key pair.
Enter file in which to save the key (~/.ssh/id_rsa): ~/.ssh/id_rsa2 #这里输入一个新的ssh key文件名
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in ~/.ssh/id_rsa2.
Your public key has been saved in ~/.ssh/id_rsa2.pub.
The key fingerprint is:
3a:01:17:b3:f9:26:5b:53:b3:69:be:71:a8:66:f6:96 git@webmaster.me
The key's randomart image is:
+--[ RSA 2048]----+
| o |
| = |
| . + o |
| . . . + |
| o S + |
| B + . |
| + .+ + |
| .E..+ |
| +.oo |
+-----------------+
larry@thk:~$ ssh-add ~/.ssh/id_rsa2
Identity added: ~/.ssh/id_rsa2 (~/.ssh/id_rsa2)

~/.ssh/id_rsa2为新SSH Keys文件名,根据实际情况修改,保证每次不一样即可。
2、打开新生成的~/.ssh/id_rsa2.pub文件,将里面的内容添加到GitHub后台。
3、打开~/.ssh/config文件(没有则创建),添加一个Host:

1
2
3
4
5
#建一个github别名,新建的帐号使用这个别名做克隆和更新
Host github2
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa2.pub

4、将GitHub SSH仓库地址中的Git@github.com替换成新建的Host别名。
如原地址是:git@github.com:freehost/mail.git,替换后应该是:github2:freehost/mail.git
如果是新建的仓库,直接使用替换后的URL克隆即可。如果已经使用原地址克隆过了,可以使用命令修改:

1
git remote set-url origin github2:freehost/mail.git

  5. 测试

1
ssh -T git@github2
posted @   人艰不拆_zmc  阅读(299)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
历史上的今天:
2016-08-09 route命令(转)
2016-08-09 Linux ifconfig命令
2016-08-09 free 命令
2016-08-09 linux chmod命令(转)
2016-08-09 less 命令(转)
2016-08-09 cat 命令(转)
2016-08-09 cp 命令(转)
点击右上角即可分享
微信分享提示