多个git账户生成多份rsa秘钥实现多个账户同时使用配置

简介: 下文分享一个多个git账户生成多份rsa秘钥实现多个账户同时使用配置例子了,这个例子非常的好用对于有多个git的朋友有不小的帮助。     使用过git的童鞋应该对id_rsa秘钥不陌生,总得用github吧,生成id_rsa很容易: ssh-keygen -t rsa -C "$your_email" 默认情况下,这个秘钥是在你账户的.

下文分享一个多个git账户生成多份rsa秘钥实现多个账户同时使用配置例子了,这个例子非常的好用对于有多个git的朋友有不小的帮助。

 

 

使用过git的童鞋应该对id_rsa秘钥不陌生,总得用github吧,生成id_rsa很容易:

ssh-keygen -t rsa -C "$your_email"

默认情况下,这个秘钥是在你账户的.ssh目录生成id_rsa文件,对应一个id_rsa.pub公钥文件,

$ ssh-keygen -t rsa -C "test@test.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/zhong/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/zhong/.ssh/id_rsa.
Your public key has been saved in /c/Users/zhong/.ssh/id_rsa.pub.
The key fingerprint is:
5b:44:7e:b8:e5:20:3b:a8:2b:63:45:c0:f8:73:87:f3 test@test.com
The key's randomart image is:
+--[ RSA 2048]----+
| o        .      |
|. o      o .     |
| . . .  . = o    |
|  o = .. + *     |
|   + +. S o .    |
|    ..E  +       |
|   ..   .        |
|  +  .           |
| . o.            |
+-----------------+

秘钥是跟email地址绑定的,上面生成的秘钥是秘钥是以test@test.com生成的,所以你可以在任何用以test@test.com地址认证的地方使用id_rsa.pub公钥校验你的私钥,因为系统默认在校验的时候就会读取你.ssh/id_rsa这个秘钥文件。
这在使用单一秘钥的用户来说,是没有任何问题,然而你可能还有别的邮箱账户,需要生成两份秘钥,于是你在生成的时候呢就要注意重名,别把已存在的id_rsa给覆盖了。


$ ssh-keygen -t rsa -C "test2@domain.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/zhong/.ssh/id_rsa): /c/Users/zhong
/.ssh/id_rsa_gitlab
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/zhong/.ssh/id_rsa_gitlab.
Your public key has been saved in /c/Users/zhong/.ssh/id_rsa_gitlab.pub.
The key fingerprint is:
5b:44:7e:b8:e5:20:3b:a8:2b:63:45:c0:f8:73:87:f3 test2@domain.com
The key's randomart image is:
+--[ RSA 2048]----+
| o        .      |
|. o      o .     |
| . . .  . = o    |
|  o = .. + *     |
|   + +. S o .    |
|    ..E  +       |
|   ..   .        |
|  +  .           |
| . o.            |
+-----------------+
在Enter file inwhich to save the key提示这个地方敲入生成私钥的名称,因为是要保留默认的私钥,所以你需要另外取一个名称,如果你敲入的秘钥名称已经存在,那么会提示你同名的秘钥文件已经存在,是否要覆写:


$ ssh-keygen -t rsa -C "test@test.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/zhong/.ssh/id_rsa):
/c/Users/zhong/.ssh/id_rsa already exists.
Overwrite (y/n)?
以上我生成了两份秘钥,分别是默认的id_rsa与id_rsa_gitlab,所以.ssh目录下应该有这些文件:


zhong@LEE ~
$ ls .ssh
id_rsa  id_rsa.pub  id_rsa_gitlab  id_rsa_gitlab.pub  known_hosts
对应pub结尾的都是公钥,接下来,就是要配置一下ssh让系统可以找到id_rsa_gitlab这个公钥,按照google的结果,只需要执行ssh-add 秘钥路径即可


$ ssh-add /c/Users/zhong/.ssh/id_rsa_gitlab
Could not open a connection to your authentication agent.
而我在执行命令时提示:Could not open a connection to your authentication agent.,也许这是很多人遇到的问题,所以相应的都提示如果出现这个错误就先执行以下ssh-agent bash然后在执行ssh-add命令就可以了


$ ssh-agent bash
bash-3.1$ ssh-add /c/Users/zhong/.ssh/id_rsa_gitlab
Could not open a connection to your authentication agent.
结果显示,我即使先执行了ssh-agent bash还是不能解决问题,搜罗一大圈也没发现可解决的方案。这一步是一定要配置的,否则即使你把公钥贴到git主机并且尝试连接id_rsa_gitlab这个账户的主机的时候,因为它默认会找到id_rsa这个秘钥做校验,结果肯定是校验不过,于是就会有以输入密码提示:


$ ssh -T git@192.168.1.2
The authenticity of host '192.168.1.2 (192.168.1.2)' can't be established
 
RSA key fingerprint is d9:8c:82:18:e7:86:3f:7d:ee:01:9d:bb:7d:40:86:5e.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.2' (RSA) to the list of known hosts.
git@192.168.1.2's password:
而默认的id_rsa账户校验仍然是没有问题的


$ ssh -T git@github.com
Hi lizhong! You've successfully authenticated, but GitHub does not provide s
hell access.
在找半天发现不能解决ssh-add命令问题后无意中看到了config配置,于是我就跳过ssh-add直接在.ssh下新建config文件:


Host github.com
    HostName        github.com
    User            git
    IdentityFile    /c/Users/zhong/.ssh/id_rsa
     
Host 192.168.1.2
    HostName        192.168.1.2
    User            git
    IdentityFile    /c/Users/zhong/.ssh/id_rsa_gitlab
清空known_hosts文件再测试,都正常了

zhong@LEE ~
$ ssh -T git@github.com
The authenticity of host 'github.com (192.30.252.130)' can't be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.252.130' (RSA) to the list of know
n hosts.
Hi lizhong8532! You've successfully authenticated, but GitHub does not provide s
hell access.
 
zhong@LEE ~
$ ssh -T git@192.168.1.2
The authenticity of host '192.168.1.2 (192.168.1.2)' can't be established.
 
RSA key fingerprint is d9:8c:82:18:e7:86:3f:7d:ee:01:9d:bb:7d:40:86:5e.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.2' (RSA) to the list of known hosts.
Welcome to GitLab, lizhong!

如果配置不好,还提示输密码的童鞋可以这样测试

ssh -vv "your githost"

就能看见整个debug过程,重点是看看有没有读到相应的config文件并匹配到host然后读取host配置下的对应的私钥,一般如果私钥配对成功那基本都没有问题

技术改变世界! --狂诗绝剑
目录
相关文章
|
9月前
|
存储 安全 开发工具
Git安装与配置:操作步骤+Gitee绑定
本文系统介绍了Git从安装配置到远程协作的全流程,涵盖基础概念、常用命令、分支管理、冲突解决及Gitee实战操作,助你高效掌握代码版本控制与团队协作技巧。
1335 155
|
10月前
|
网络安全 开发工具 git
配置本地环境以管理Git多账户SSH连接的方法
通过以上步駟设置后, 您可以轻松管理多個 Git 账户并且根据不同项目需求切换 SSH 密匙进行版本控制操作。
1226 20
|
10月前
|
存储 Java 开发工具
【Git】零基础入门:配置与初始操作实战指南
本文聚焦 Git 的“从 0 到 1”配置与基础操作,旨在帮助 Java 开发者快速掌握环境搭建、用户配置、仓库初始化、代码提交、版本回退等核心技能。内容设计上避免涉及复杂的分支策略或高级命令,以实用为导向,适合零基础入门者系统学习,为后续参与企业级项目开发奠定版本控制基础。
|
安全 Linux 网络安全
在Linux(CentOS和AWS)上安装更新的git2的方法并配置github-ssh
经过以上这些步骤,你现在就能在GitHub上顺利往返,如同海洋中的航海者自由驰骋。欢迎你加入码农的世界,享受这编程的乐趣吧!
603 10
|
安全 Shell 开发工具
Windows下使用git配置gitee远程仓库
就在前几天因为一些原因,我的电脑重装了系统,然后再重新配置git的环境的时候就遇到了一些小问题。所以我决定自己写一篇文章,以便以后再配置git时,避免一些错误操作,而导致全网搜方法,找对的文章去找对应的解决方法。下面为了演示方便就拿gitee来演示,不拿GitHub了写文章了。
632 0
|
移动开发 jenkins 持续交付
jenkins配置git
通过上述步骤,您可以在 Jenkins 中成功配置 Git,从而实现自动拉取代码并进行构建和部署。这些配置不仅提高了开发效率,还保证了代码的连续集成和交付。确保每一步配置正确,以避免在实际使用中遇到问题。
2200 1
|
前端开发 Java 开发工具
Git使用教程-将idea本地Java等文件配置到gitte上【保姆级教程】
本内容详细介绍了使用Git进行版本控制的全过程,涵盖从本地仓库创建到远程仓库配置,以及最终推送代码至远程仓库的步骤。
2074 0
|
Ubuntu Shell 开发工具
ubuntu/debian shell 脚本自动配置 gitea git 仓库
这是一个自动配置 Gitea Git 仓库的 Shell 脚本,支持 Ubuntu 20+ 和 Debian 12+ 系统。脚本会创建必要的目录、下载并安装 Gitea,创建 Gitea 用户和服务,确保 Gitea 在系统启动时自动运行。用户可以选择从官方或小绿叶技术博客下载安装包。
906 2
|
Linux 网络安全 开发工具
IDEA如何配置git和github
【11月更文挑战第14天】本指南详细介绍了如何在 IntelliJ IDEA 中配置 Git 和 GitHub,包括检查和设置 Git 路径、测试配置,以及通过 SSH 或 HTTPS 方式配置 GitHub 仓库的具体步骤。完成配置后,用户可在 IDEA 中轻松进行版本控制操作。
3723 0
|
编译器 网络安全 开发工具
git学习五:切换本地仓库出现的问题。修改git配置初始化。error:src refspec master does not match any。错误总结,送上几个案例
这篇文章是关于Git使用中遇到的一些问题及其解决方案的总结,包括切换本地仓库时的问题、修改Git初始化配置、以及解决"error: src refspec master does not match any"错误等。
1646 0