同一台电脑设置多个公钥与不同GITHUB帐号交互

  • 浏览:2594
  • |
  • 更新:
  • |
  • 标签:电脑 

GITHUB公钥有哪些原则,如何创建和使用公钥?特别是同时在多个GITHUB仓库下工作时。

工具/原料

  • ssh git

方法/步骤

  1. 1

    制造第一把公钥:

    ssh-keygen -t rsa -C "mywork@email.com"

    # 设置名称为id_rsa_derek

    Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa): id_rsa_derek

    #添加到SSH agent中

    ssh-add id_rsa_derek

    制造第二把公钥:

    ssh-keygen -t rsa -C "mywork@email.com"

    # 设置名称为id_rsa_ranpop

    Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa): id_rsa_ranpop

    #添加到SSH agent中

    ssh-add id_rsa_ranpop

  2. 2

    将id_rsa_derek.pub添加到derek帐号的后台ssh。

    将id_rsa_ranpop.pub添加到ranpop帐号的后台ssh。

  3. 3

    在.ssh目录下配置config文件:

    Host derek

    HostName github.com

    User git

    IdentityFile ~/.ssh/id_rsa_derek

    Host ranpop

    HostName github.com

    User git

    IdentityFile ~/.ssh/id_rsa_ranpop

  4. 3
    此文章未经授权抓取自百度经验
  5. 4

    对于derek帐号下的仓库:

    git clone derek:githubname/repository.git

    (原地址是:git@github.com:githubname/repository.git,替换后应该是:derek:githubname/repository.git)

    对于ranpop帐号下的仓库:

    git clone ranpop::githubname/repository.git

    (原地址是:git@github.com:githubname/repository.git,替换后应该是:ranpop:githubname/repository.git)

  6. 5

    测试:

    ssh -T derek

    Hi derek! You've successfully authenticated, but GitHub does not provide shel l access.

    ssh -T ranpop

    Hi ranpop! You've successfully authenticated, but GitHub does not provide shel l access.

    说明OK。

  7. 6

    如果已经使用原地址克隆过了,可以使用命令修改:

    git remote set-url origin derek:githubname/repository.git

  8. 7

    相关原理:

    1、一把公钥只能被一个GITHUB帐号拥有->因此必须为不同的帐号创建公钥。

    2、交互时需要本地私钥与GITHUB帐号的公钥配对。

    因此,要在同一台电脑上给两个属于不同帐号的仓库提交时,必须在本地创建两对公/私钥匙,分别把两把公钥给两个帐号,提交时根据要提交的仓库所属帐号选择相应的本地私钥即可;

    当我们要在一个仓库上PUSH提交的内容时,使用以上的步骤可以选择对应的公钥,GITHUB服务器收到提交的内容时,先解析出仓库地址,然后从该仓库的所属帐号中找到一把能解锁该提交的公钥。

    END
经验内容仅供参考,如果您需解决具体问题(尤其法律、医学等领域),建议您详细咨询相关领域专业人士。
展开阅读全部
相关标签电脑