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 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 version 1, and ~/.ssh/id_rsa and ~/.ssh/id_dsa for protocol version 2.  Identity 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 on a system with multiple private keys in the ~/.ssh directory?

share|improve this question
    
See this question in StackOverflow as well. –  Flimm May 8 at 9:46

6 Answers 6

up vote 159 down vote accepted

In ~/.ssh/config, add:

host github.com
 HostName github.com
 IdentityFile ~/.ssh/id_rsa_github
 User git

Now you can do git clone git@github.com:username/repo.git.

share|improve this answer
2  
What if you need to connect to the same host with different keys? –  Valentin Klinghammer Nov 30 '12 at 11:24
3  
@Quelltextfabrik - you can add another section with a different Host: nerderati.com/2011/03/… –  Ben Challenor Dec 4 '12 at 14:17
    
I'm using this trick in my github-keygen tool that I built to manage SSH keys and settings for Github. –  dolmen Sep 20 '13 at 10:10
    
@Grissiom You've got that backwards. Host can be anything, even wildcards and patterns; Hostname must have the hostname or IP address. Source: ssh_config manpage. –  Cliff Jan 5 at 21:41
    
@Cliff Nop, in my manpage: "HostName: Specifies the real host name to log into. This can be used to specify nicknames or abbreviations for hosts." My ssh version is openssh-6.7p1. –  Grissiom Jan 7 at 2:17

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 '14 at 19:44
    
~/.ssh/config Is the way to go. –  hek2mgl May 8 at 13:56

From Git version 2.3.0, you can use the environment variable GIT_SSH_COMMAND like this:

GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_example" git clone example

Note that -i can sometimes be overridden by your config file, in which case, you should give SSH an empty config file, like this:

GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_example -F /dev/null" git clone example
share|improve this answer

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

There is no direct way to tell git which private key to use, because it rely on ssh for repository authentication.

However you can use ssh-agent to temporary authorize your private key. In example:

ssh-agent sh -c 'ssh-add ~/.ssh/rsa_key; git fetch user@host'

Or you can pass the ssh arguments by using GIT_SSH variable and the script, in example:

$ echo 'ssh -i ~/.ssh/rsa_key -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $*' > ssh
$ chmod +x ssh
$ GIT_TRACE=1 GIT_SSH='./ssh' git clone user@host

Note: Above lines are terminal command-lines which you should paste into your terminal. It'll create a file ssh, make it executable and executes it.

Alternatively use ~/.ssh/config file as suggested in other answers in order to specify your location of your private key.

share|improve this answer

After my struggle with $GIT_SSH I would like to share what worked for me.

Through my examples I will assume you have your private key located at/home/user/.ssh/jenkins

Error to avoid: GIT_SSH value includes options

$ export GIT_SSH="ssh -i /home/user/.ssh/jenkins"

or whatever similar will fails, as git will try to execute the value as a file. For that reason, you have to create a script.

Working example of $GIT_SSH script /home/user/gssh.sh

The script will be invoked as follows:

$ $GIT_SSH [username@]host [-p <port>] <command>

Sample script working could look like:

#!/bin/sh
ssh -i /home/user/.ssh/jenkins $*

Note the $* at the end, it is important part of it.

Even safer alternative, which would prevent any possible conflict with anything in your default config file (plus explicitly mentioning the port to use) would be:

#!/bin/sh
ssh -i /home/user/.ssh/jenkins -F /dev/null -p 22 $*

Assuming the script is in /home/user/gssh.sh, you shall then:

$ export GIT_SSH=/home/user/gssh.sh

and all shall work.

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.