下文分享一个多个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配置下的对应的私钥,一般如果私钥配对成功那基本都没有问题

多个git账户生成多份rsa秘钥实现多个账户同时使用配置的更多相关文章

  1. 一台电脑存放多个git账户的多个rsa秘钥

    未命名.html div.oembedall-githubrepos{border:1px solid #DDD;border-radius:4px;list-style-type:none;marg ...

  2. 一台电脑存放多个git账户的多个rsa秘钥(转)

    如何在一个电脑上存储多个git账户生成的多份rsa秘钥,实现多个账户同时使用配置的情况?今天,不幸又再次遇到这个问题. 问题描述 公司最近在开发一款开源产品,项目被托管在github上,但是公司内部一 ...

  3. 开源工具 DotnetRSA 快速生成和转换RSA秘钥

    一.简介 DotnetRSA 是一个利用 .NET Core 2.1 开发的 .NET Global Tool,是可以想npm全局安装一样,安装在你的系统中,只需敲一行命令便可以快速生成RSA加密算法 ...

  4. Git生成公钥.pub 及秘钥 命令

    Git生成公钥.pub 及秘钥 命令 ssh-keygen -t rsa -C "******@qq.com" 将.pub公钥里面内容复制到github或者将这文件交给git管理员 ...

  5. 利用SHA-1算法和RSA秘钥进行签名验签(带注释)

    背景介绍 1.SHA 安全散列算法SHA (Secure Hash Algorithm)是美国国家标准和技术局发布的国家标准FIPS PUB 180-1,一般称为SHA-1.其对长度不超过264二进制 ...

  6. 【Git 三】生成并添加SSH秘钥

    我们在 Windos 或 Linux 上克隆项目的时候, 如果走的是 http 会让输入 username 和 password,每次都要输入是很麻烦的. 为了省事儿.简单,我们可以选择 ssh 方式 ...

  7. WSL、Git on Windows 、Putty等的创建的rsa秘钥与连接linux的使用。

    1. 在windows 上面可以使用多种方式创建公钥和私钥 这里从一开始说: 1.1 windows subsystem linux 的方式最简单了 cmd 命令行 下 进入 WSL 输入命令 bas ...

  8. 自己实现简单的RSA秘钥生成与加解密(Java )

    最近在学习PKI,顺便接触了一些加密算法.对RSA着重研究了一下,自己也写了一个简单的实现RSA算法的Demo,包括公.私钥生成,加解密的实现.虽然比较简单,但是也大概囊括了RSA加解密的核心思想与流 ...

  9. 使用OpenSSL生成RSA秘钥对并对文件加解密

    生成RSA私钥 openssl genrsa -out rsa.key 1024 生成RSA公钥 openssl rsa -in rsa.key -pubout -out pub.key 创建明文文件 ...

随机推荐

  1. asp xml对象转换为string

    'xml文件中没有属性的情况 Dim xmlStrxmlStr="<root><count>1</count><error>0</err ...

  2. 滚动动画animate-scroll扩展

    原动画库地址:animate-scroll 扩展动画库下载地址:animate-scroll-ext 由于项目需要,对animate-scroll进行扩展,扩展后修改或增加了以下几点: 对原动画库进行 ...

  3. Node.js API

    Node.js v4.4.7 Documentation(官方文档) Buffer Prior to the introduction of TypedArray in ECMAScript 2015 ...

  4. XPath相关笔记

    <?xml version="1.0" encoding="utf-8" ?> <employees>   <employee o ...

  5. 新手购买海外VPS主机需要注意的几个账户安全问题

    一般我们用户的项目需要,可能会较多的购买海外的VPS主机项目居多,其实我也比较喜欢国内的阿里云等产品,但是后者需要的一些政策还是比较麻烦, 所以在操作一些调试项目的时候我比较倾向于选择较为便宜的海外V ...

  6. Counting Stars

    Counting Stars 题目链接:http://acm.xidian.edu.cn/problem.php?id=1177 离线+一维树状数组 一眼扫过去:平面区间求和,1e6的数据范围,这要h ...

  7. html 超链接(a)详细讲解

    a:link : http://www.cnblogs.com/yangfeng/archive/2009/07/25/1530962.html 超级链接 超级链接是网站中使用比较频繁的HTML元素, ...

  8. javascript中的事件处理

    事件处理:http://www.cnblogs.com/polk6/archive/2016/02/19/5154470.html#Menu2-DOMEventSpecification addEve ...

  9. HDU 4247 A Famous ICPC Team

    Problem Description Mr. B, Mr. G, Mr. M and their coach Professor S are planning their way to Warsaw ...

  10. POJ 2039 To and Fro

    To and Fro Description Mo and Larry have devised a way of encrypting messages. They first decide sec ...