How to use specified key when working with github

2020-02-06 12:58发布

问题:

I have two ssh keys to work with github - my own and one from organisation where I'm working. My key was generated automatically by github gui client and the other one was generated by portablegit. My .ssh folder looks like:

github_rsa            <--- my key
github_rsa.pub
id_rsa                <--- org key
id_rsa.pub

When I use portablegit it takes the key with name 'id_rsa' but sometimes I need to use my key too. How can I setup default key?

回答1:

You can by adding to your HOME/.ssh a config file:

Host wpengine 
user git
hostname git.wpengine.com
IdentityFile ~/.ssh/myPrivateKey

You can add as many 'Host' entry as you want, each one with a different IdentityFile

See for instance "Multiple SSH Keys settings for different github account"

#activehacker account
Host github.com-activehacker
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_activehacker

#jexchan account
Host github.com-jexchan
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa_jexchan

You can then use the scp syntax for cloning your repo:

git clone github.com-activehacker:activehacker/gfs.git gfs_jexchan

(instead of ssh://git@github.com/activehacker/gfs.git, which wouldn't be able to reference a specific private key and would always fall back to id_rsa.)



标签: git github ssh