Lmsgsendnilself

Uninhibited Soul, Free Craziness

多个github账户配置

| Comments

  最近由于遇到有两个github账号需要同时使用,ssl配置需要专门处理下,大概需要做的事情如下:

  1. 新建两个ssh key并将key添加至ssh agent

    进入.ssh文件夹
    $ cd ~/.ssh

    新建 ssh key1:
    $ssh-keygen -t rsa -C "githubuser1@email.com"

    此时根据提示设置为Enter file in which to save the key (~/.ssh/id_rsa): ~/.ssh/id_rsa_github1 其他一直回车即可;

    新建SSH key2:
    $ssh-keygen -t rsa -C "githubuser2@email.com"

    此时根据提示设置为Enter file in which to save the key (~/.ssh/id_rsa): ~/.ssh/id_rsa_github2

    添加key的位置到ssh agent。由于不是使用默认的.ssh/id_rsa,因此需要将新key的位置添加到ssh agent

    $ ssh-add ~/.ssh/id_rsa_github1

    $ ssh-add ~/.ssh/id_rsa_github1

添加完后可以输入$ssh-add -l查看是否添加成功

  1. 配置config文件

    输入

    $vim config

    该文件内容配置如下

     Host github1.github.com
         HostName github.com
     PreferredAuthentications publickey
     IdentityFile ~/.ssh/id_rsa_github1
     github user1 
    
     Host github2.github.com
         HostName github.com
         PreferredAuthentications publickey
         IdentityFile ~/.ssh/id_rsa_github2
         github user2  
    
  2. 添加public key至github并测试

    完成前两步后,将id_rsa_github1.pub和id_rsa_github2.pub两个文件中的内容分别添加到相应的github账户网站ssh配置中。并利用如下指令进行测试是否配置成功

$ssh -T git@github1.github.com

如果成功,则会提示

`Hi github1! You've successfully authenticated, but GitHub does not provide shell access`

  配置完之后 务必要注意的是使用github repo 相关的 origin url时 一定要用 git@github1.github.com,而非git@github.com

例如git remote set-url origin git@github1.github.com:github1/xxxxxxx.git

Comments