How to set up authentication for two separate GitHub accounts from same ssh client?

前端 未结 2 1473
伪装坚强ぢ
伪装坚强ぢ 2021-01-07 05:59

The short version:

Is there any way to set up automatic public-key-based ssh authentication from one Linux account to two different Github accounts?

相关标签:
2条回答
  • 2021-01-07 06:21

    You need to create two sets of (public/private) keys, one for each account.

    You can reference them through an ssh config file, as detailed in "GitHub: Multiple account setup"/

    #Account one
    Host github.com
        HostName github.com
        PreferredAuthentications publickey
        IdentityFile /c/Users/yourname/.ssh/id_rsa
        User git
    
    #Account two
    Host ac2.github.com
        HostName github.com
        PreferredAuthentications publickey
        IdentityFile /c/Users/yourname/.ssh/id_rsa_ac2
        User git
    
    0 讨论(0)
  • 2021-01-07 06:24

    It seems GitHub doesn't allow the same RSA key for two repositories.

    As workaround, you've to create separate RSA keys for each site:

    ssh-keygen -t rsa -f rsa_site1
    ssh-keygen -t rsa -f rsa_site2
    

    This will generate private and public keys. Then add public keys into GitHub to Deploy keys.

    Then deploy your private keys into the remote:

    cat rsa_site1 | ssh user@remote "cat > ~/.ssh/rsa_site1 && chmod 600 ~/.ssh/rsa_site1"
    cat rsa_site2 | ssh user@remote "cat > ~/.ssh/rsa_site2 && chmod 600 ~/.ssh/rsa_site2"
    

    And to fetch your private repository on the server, you can use something like:

    ssh user@remote 'ssh-agent sh -c '\''cd /webroot/site1 && ssh-add ~/.ssh/rsa_site1 && git fetch git@github.com:priv/site1.git'\'
    ssh user@remote 'ssh-agent sh -c '\''cd /webroot/site2 && ssh-add ~/.ssh/rsa_site2 && git fetch git@github.com:priv/site2.git'\'
    
    0 讨论(0)
提交回复
热议问题