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

Other 2014-11-22 git,ssh,公钥,id_rsa

使用过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秘钥实现多个账户同时使用

文章地址:http://www.qttc.net/201411453.html

除非标注,琼台博客所有博文均为原创,转载请加文字链接注明来源

乳名?小名?昵称?网名?均可

email,放心,我不会给你乱投广告的

想获得回访就把你的站点URL写上(没有留空)

[NOTICE]木要投放广告
[NOTICE]木要骂人,说不该说的话
[NOTICE]自由言论,但要遵纪守法

Comments 0

    Hi,你想第一个做沙发么?