Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

ssh has the -i option to tell it which private key file to use when authenticating:

-i identity_file
     Selects a file from which the identity (private key) for RSA or DSA
     authentication is read.  The default is ~/.ssh/identity for protocol ver-
     sion 1, and ~/.ssh/id_rsa and ~/.ssh/id_dsa for protocol version 2.  Iden-
     tity files may also be specified on a per-host basis in the configuration
     file.  It is possible to have multiple -i options (and multiple identities
     specified in configuration files).

Is there a similar way to tell git which private key file to use when on a system with multiple private keys in the .ssh directory?

share|improve this question

3 Answers 3

up vote 118 down vote accepted

In ~/.ssh/config, add:

Host github.com
        Hostname github.com
        User git
        IdentityFile ~/.ssh/somekey

Now you can do git clone ssh://gh/username/repo.git.

share|improve this answer
2  
The file specified in IdentityFile is the private key, not the public key. The .pub extension you used in your example confused me, I think it will be more clear to use "IdentityFile ~/.ssh/gh_id_rsa" –  Joaquin Cuenca Abela Apr 27 '11 at 9:17
    
@Joaquin Cuenca Abela: Answer edited. –  Laurent Pireyn May 13 '11 at 16:19
3  
@Quelltextfabrik - you can add another section with a different Host: nerderati.com/2011/03/… –  Ben Challenor Dec 4 '12 at 14:17
1  
Awesome, thanks! –  Valentin Klinghammer Dec 4 '12 at 19:08
2  
The Host should be github.com and the Hostname can be any name you want, e.g., gh. –  Grissiom Feb 8 at 3:35

Write a script that calls ssh with the arguments you want, and put the filename of the script in $GIT_SSH. Or just put your configuration in ~/.ssh/config.

share|improve this answer
3  
That's a handy trick –  Bryan Agee May 30 '12 at 17:13
1  
Another explanation of how to do this. –  Sithsu May 12 at 19:44

You can just use ssh-ident instead of creating your own wrapper.

You can read more at: https://github.com/ccontavalli/ssh-ident

It loads ssh keys on demand when first needed, once, even with multiple login sessions, xterms or NFS shared homes.

With a tiny config file, it can automatically load different keys and keep them separated in different agents (for agent forwarding) depending on what you need to do.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.