zoukankan      html  css  js  c++  java
  • Git配置多个github账号免密登录


    在公司开发中,有时候会存在公司账户跟私人账户共存,并随时需要切换的情况,这种情况下git可以配置多个ssh-key,无缝切换账号。
    假如有两个github账号,一个是私人github账号,一个是公司github账号
    私人账号:

    • github用户名:my
    • email:convict@qq.com

    公司账号:

    • github用户名:company
    • email:convict.yellow@work.com

    一、在用户目录下的.ssh目录下生成秘钥公钥

    如果用户目录下没有.ssh目录,则需要新建一个

    cd ~/.ssh
    ssh-keygen -t rsa -f id_rsa_my
    ssh-keygen -t rsa -f id_rsa_company
    

    一路回车即可

    • 注:国内很多博客都会带上-C "xxx邮箱" 这个参数,但其实-C 参数是用来做秘钥注释的,以便知道这个秘钥到底是干嘛的。填个邮箱进去可以当成是备注,但不是必须,你也可以填个我是世界首富

    上面ssh-keygen 命令参数:

    • -t: 指定生成rsa 类型秘钥
    • -f: 指定生成秘钥的名字,可以不指定该参数,默认就会生成2个文件:私钥id_rsa,公钥id_rsa.pub。由于需要生成两对私钥公钥,因此需要指定-f,否则生成两次后,私钥公钥会覆盖

    上面的命令调用完后会生成四个文件:

    • id_rsa_my
    • id_rsa_my.pub
    • id_rsa_company
    • id_rsa_company.pub

    二、将公钥配置到对应的github账号中

    公钥.pub文件可以直接用文本打开,内容粘贴到github的 Settings -> SSH and GPG keys -> New SSH Key,Title随便起,自己能认出来即可,Key里面填写复制的.pub里的内容,这样公钥就配置好了
    image

    三、创建config文件

    .ssh目录下创建config 文件,git通过这个文件才知道哪个私钥去对应哪个公钥

    touch config
    

    config文件内容:

    # my
    Host my
    HostName github.com
    IdentityFile ~/.ssh/id_rsa_my
    
    # company
    Host company
    HostName github.com
    IdentityFile ~/.ssh/id_rsa_company
    

    config文件部分参数含义,仅做记录

    # Host: 主机别名
    # HostName: 托管平台域名地址,如github.com
    # IdentityFile: 该Host私钥文件
    # User: 托管平台用户名
    # Port: 端口号,可不填(如果不是默认22号端口则需要指定)
    # PreferredAuthentications publickey
    

    四、测试ssh-key是否连通

    ssh -T git@my
    ssh -T git@company
    

    成功的情况下会返回:

    Hi xxx! You've successfully authenticated, but GitHub does not provide shell access.
    

    此时私钥公钥 都配置正常了

    五、测试clone不同github仓库

    此时测试一下clone私人git仓库,必须使用SSH链接
    SSH链接格式为:

    git@github.com:用户名/仓库名.git
    

    比如下面例子:

    git@github.com:convict/my-repo.git
    

    要clone这个仓库,需要进行改动,应使用:

    git clone git@my:convict/my-repo.git
    

    即把github.com 换成my,此时clone成功,同理需要clone公司账户下的仓库,需要把github.com 换成company即可

    如果直接使用复制下来的链接git clone git@github.com:convict/my-repo.git,会clone失败:

    Cloning into 'test'...
    Warning: Permanently added the RSA host key for IP address '13.250.177.223' to the list of known hosts.
    git@github.com: Permission denied (publickey).
    fatal: Could not read from remote repository.
    
    Please make sure you have the correct access rights
    and the repository exists.
    

    解释:

    1. 在SSH中,@:之间就是Host,因此在git clone git@github.com:convict/my-repo.git中,Host就是github.com,但在前面配置的config文件中,指定了两个Host,分别为mycompany,而没有一个加github.com的Host!这是尤其需要注意的。
    2. 使用git clone git@my:convict/my-repo.git时,会在config中找到一个值为my的Host,接着到其HostName上找到与其私钥对应的公钥的仓库地址。在本例中,就是根据其私钥id_rsa_my,在github.com托管平台上,匹配对应的公钥,然后匹配到convict/my-repo.git这个仓库。

    六、单账号ssh免密登录

    单账号跟多账号的操作步骤基本一致,但如果仅仅是一个账号配置免密登录,可以省略更多步骤,请看我的另一篇文章:
    https://www.cnblogs.com/convict/p/14888283.html



    尊重写作权利,转载请注明出处 ^_^
  • 相关阅读:
    根据前序遍历和中序遍历重建二叉树
    Java内部类
    Java浅克隆和深度克隆
    【leetcode】1354. Construct Target Array With Multiple Sums
    【leetcode】1352. Product of the Last K Numbers
    【leetcode】1351. Count Negative Numbers in a Sorted Matrix
    【leetcode】1342. Number of Steps to Reduce a Number to Zero
    【leetcode】1343. Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold
    【leetcode】1344. Angle Between Hands of a Clock
    【leetcode】1346. Check If N and Its Double Exist
  • 原文地址:https://www.cnblogs.com/convict/p/14887421.html
Copyright © 2011-2022 走看看