zoukankan      html  css  js  c++  java
  • 多个git账户生成多份rsa秘钥实现多个账户同时使用配置

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

  • 相关阅读:
    cookie
    iOS本地推送
    2020-06-17:红锁的设计思想?
    2020-06-16:Redis hgetall时间复杂度?
    2020-06-15:Redis分布式锁怎么解锁?
    2020-06-14:Redis怎么实现分布式锁?
    2020-06-13:Redis底层数据结构?
    2020-06-12:推箱子自动求解。
    2020-06-11:Redis支持的数据类型?
    2020-06-10:给定一个无序数组,里面数都是成双数的,只有一个数是成单数的,求这个数? 来自
  • 原文地址:https://www.cnblogs.com/jinjiangongzuoshi/p/4937590.html
Copyright © 2011-2022 走看看