Manage multi-SSH KEY For One Mac

  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. 将id_rsa_derek.pub添加到derek帐号的后台ssh。

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

  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. 对于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)

  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。

  6. 6

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

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

  7. 7

    相关原理:

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

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

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值