166

I have 2 Git servers that require 2 different SSH keys.

git clone user1@server1:blahblahblah uses ~/.ssh/id_rsa, but I need to specify which key to use depending on the server I am connecting to.

What Git command-line parameter does this job? (I am running Linux.)

3

9 Answers 9

254

There is another possibility. That's to set core.sshCommand, e.g.

git config --local core.sshCommand "/usr/bin/ssh -i /home/me/.ssh/id_rsa_foo"

There's one particular scenario when this strategy is particularly useful: that's when you have multiple accounts on Github, as all accounts ssh to Github as [email protected] and it uses the ssh key to determine which Github user you are. In this case neither .ssh/config nor ssh-agent will do what you want.

Update — You cannot run the above until you have a local repository, so if you're trying to clone a remote repository, you'll need to specify the key manually as per drewbie18's answer:

git clone -c core.sshCommand="/usr/bin/ssh -i /home/me/.ssh/id_rsa_foo" [email protected]:me/repo.git

Once you've cloned the repository you can use the git config command to set this permanently.

12
  • 23
    This should be the selected answer. The required configuration is nicely localized in the repository which needs to use different ssh key to access different git servers. No need to modify ~/.ssh/config (and even /etc/hosts, in case of multiple accounts on the GitHub).
    – mr.b
    Commented Aug 15, 2017 at 8:21
  • 1
    There's one problem, though: you can't clone a repo before you configure it, and in order to configure it, you need to have it cloned first. OR, you could git init a repo, configure it locally to use correct ssh command, and then add a remote (3 steps, compared to only 1 with "git clone").
    – mr.b
    Commented Aug 15, 2017 at 8:37
  • 3
    To clone the repo, I'd use @Guss's solution, below, by setting the GIT_SSH_COMMAND environment variable. But in normal use I prefer my solution as it avoids having to re-export that variable every time I start a new shell. Commented Aug 16, 2017 at 9:23
  • 3
    Thank you for this elegant solution. However I just want to precise that core.sshCommand is only available for git >= 2.10.0
    – slonepi
    Commented Jul 11, 2019 at 17:24
  • 1
    @akostadinov – Why would you want to disable strict host key checking? It will only ask you to add the host key the first time you ssh to that server, which will normally be the first time you clone a repo from that server. Commented Jan 8, 2021 at 16:10
101

If you are connecting via SSH then the key will be controlled by an SSH parameter, not a git parameter.

SSH looks in the ~/.ssh/config file for configuration parameters. Modify that file and add IdentityFile entries for the two Git servers like this:

Host server1.whatever.com
  IdentityFile /path/to/key_1
Host server2.whatever.com
  IdentityFile /path/to/key_2

This article has some more details.

7
  • 21
    That did not answer the question. The question is, how do I give git this parameter, when ssh handles authorization?
    – keks
    Commented Feb 4, 2013 at 21:54
  • 2
    @Keks: You cannot pass ssh arguments via the git command line. This answer gives a workaround. There are other workarounds possible, too, for example git.wiki.kernel.org/index.php/…. Note that that approach also does not use command line arguments to modify the ssh behavior. Commented Feb 4, 2013 at 22:58
  • 13
    That is true, however your original answer did not answer the question. You can also use ssh-agent and use ssh-add and then just use git. When git connects via ssh, the key is already enabled. You see, there are several ways, and your original answer just dosn't really help.
    – keks
    Commented Feb 5, 2013 at 22:31
  • 2
    This is a link-only answer pretty much. Could you summarise the relevant bits of the article in your answer?
    – Flimm
    Commented Aug 4, 2016 at 10:38
  • 2
    This answer is outdated.
    – nurettin
    Commented Oct 20, 2020 at 10:16
55

Generally, you want to use ~/.ssh/config for this. Simply pair server addresses with the keys you want to use for them as follows:

Host github.com
  IdentityFile ~/.ssh/id_rsa.github
Host heroku.com
  IdentityFile ~/.ssh/id_rsa.heroku
Host *
  IdentityFile ~/.ssh/id_rsa

Host * denotes any server, so I use it to set ~/.ssh/id_rsa as the default key to use.

2
  • 17
    This idea doesn't work if you have multiple accounts on one server like GitHub, and you want a different private key for each one.
    – Flimm
    Commented Aug 4, 2016 at 10:39
  • 2
    For multiple accounts on one server see this answer stackoverflow.com/questions/3225862/… Commented Jan 31, 2017 at 12:30
30

[Update]: see my other answer for a configuration-based method with a ready-made script for you to use

In my scenario, similar to @Richard Smith scenario (whose solution, BTW, didn't work for me), I need to use different keys for the same server under different repositories.

The workaround for me was to set up the session correctly with the environment variable GIT_SSH_COMMAND, like so:

export GIT_SSH_COMMAND="ssh -o IdentitiesOnly=yes -i ~/.ssh/my-secret-identitiy"

Update:

Another thing to note here is that setting the environment variable correctly can be a hustle, so I'm using the command prompt modification facilities provided by things like Liquid Prompt or Fish Shell to hook into the shell and keep updating the environment variables according to the current directory and some rules. For example, all my personal projects that need to my personal SSH key with Gitlab are under ~/Documents/Projects/personal so when the shell hook runs pwd and finds that the current directory is under that path, it automatically sets the GIT_SSH_COMMAND variables as needed.

1
  • Older git clients (1.7.1 in my case) call this environment variable GIT_SSH. Commented Mar 11, 2021 at 21:30
22

You can use --global or --local configuration, global will update ~/.gitconfig file, local will update configuration in the repository .git/config and override the global (~/.gitconfig) configuration

git config --local --add core.sshCommand 'ssh -i ~/.ssh/my_key'
2
  • 4
    worked as a charm, this was exactly what I was needing, a solution for a specific repo. Commented Jun 23, 2019 at 1:45
  • 1
    Easy and clean, thanks. Commented Jul 16, 2024 at 17:20
17

Use ssh-add path-to-private-key it works out of the box.

1
  • 11
    Not if you have two deploy keys and they only work with a certain repositories. In that case, it may use the wrong key and say you don't have access.
    – cdmckay
    Commented May 14, 2019 at 13:43
16

Windows user here, I just ran into this issue and have a slightly different solution then I have read here so far. The problem I faced is that I simply wanted to clone a repo using a specific private ssh key and not have to globally configure my git config or add specific git bash settings, as I do my work in PowerShell. Essentially I just want to have some private keys sitting in my .ssh folder and I reference them in specific repos as required.

The following command works for this:

git clone -c core.sshCommand="ssh -i ~/.ssh/<PRIVATE KEY NAME>" <CLONE URL>

Essentially what this does is upon the initialization of the git repo it sets the core.sshCommand option before running the clone. So the specific ssh key you wish to use for this repo is set ONLY for this repo. This may not be an ideal solution for all cases but for what I want it is.

3
  • Thank you. I wasn't aware of the -c option to git clone. This is better than setting the environment as I suggest when cloning. Commented Sep 12, 2019 at 8:04
  • this should really be the accepted answer. It answers the actual question posed (and the one I wanted the answer to too....). PS it works just fine on linux too. Commented Feb 25, 2021 at 4:52
  • (note that for other commands, you can put the -c before the command, eg for git push, do git push -c ... etc ... push Commented Feb 25, 2021 at 4:52
5

Another option is to write a small script to make core.sshCommand a bit smarter - check if the current working directory has a specific SSH key configured and if so use it, otherwise - rely on the standard SSH key resolution.

Here is my first version:

#!/bin/bash
key="$(git config ssh.key)"
if [ -n "$key" ]; then
        ssh -o IdentitiesOnly=yes -i "$key" "$@"
else
        ssh "$@"
fi

Then set it up as the global git SSH command:

chmod 755 ~/.local/bin/git-ssh-command
git config --global core.sshCommand ~/.local/bin/git-ssh-command

(~/.local/bin is the current standard for "place user scripts here" on SystemD operating systems)

After you set this up, you can configure any repository to use a specific SSH key by setting the configuration option ssh.key:

git config --local ssh.key ~/.ssh/my-non-default-private-key

Additional Optional Tricks

  • Set the global ssh.key to have a "default fallback to non-default SSH key" or something.
  • As git executes core.sshCommand in the root directory of the repository, your custom git-ssh-command can look at that and have some heuristics about directory names. This can be done in the else section so heuristics only kick in if there is no specific key in ssh.key.
  • You can add git remote -v check to add heuristics based on the the remotes, like in Eike's script
  • If you want to look at the repository remotes but have multiple remotes that need different keys, you can add remote="$1:$(sed "s,.* ,,;s,',,g"<<<"$2")" at the beginning of the script to resolve the remote being operated on - and check against that ($remote would look like the middle column in git remote -v output).

Update:

Here's a version of the custom SSH command that looks at the remote URL and does some heuristics - in my case, I have several different identities with the same public git hosting service (one for work, one for personal, etc) and I can select the correct identity by looking at the remote URL (examining the Gitlab group or the Github organization, etc):

#!/bin/bash
while ! [[ "$1" =~ "git@" ]]; do shift; done # git may send ssh options
if [[ "$2" =~ ^git-lfs-authenticate.* ]]; then # reconstruct url for git-lfs
        remote="$1:$(awk '{print$2}'<<<"$2")"
else # reconstruct url for standard git commands
        remote="$1:$(sed "s,.* ,,;s,',,g"<<<"$2")"
fi
echo "Detected $remote from $@" >&2
key="$(git config ssh.key)"
if [ -n "$key" ]; then # use specified SSH key, if set
        ssh -o IdentitiesOnly=yes -i "$key" "$@"
elif [[ "$remote" == [email protected]:my-company* ]]; then # use company id
        ssh -o IdentitiesOnly=yes -i ~/.ssh/company-id "$@"
elif [[ "$remote" =~ [email protected]:.*other-org.* ]]; then # bitbucket has weird urls
        ssh -o IdentitiesOnly=yes -i ~/.ssh/custom-org-key "$@"
else # otherwise use whatever the agent has (my personal key)
        ssh "$@"
fi
1

The other answers inspired me to write a little script that chooses the ssh key depending on command line options or (if present) the values of git remote -v. Hope it helps!

To actually answer the question: Use gat.

See also https://bitbucket.org/eikerobert/gat/src/master/

#!/bin/bash

usegat=false

for VAR in "$@"
do
    if [ "$VAR" != "${VAR/[email protected]:myaccount/}" ]; then
        usegat=true
    fi
done

if [ $usegat=false ]; then
   /usr/bin/git rev-parse --is-inside-work-tree >/dev/null 2>&1
   isinsidegitrepo=$?
    #echo $isinsidegitrepo
   if [ $isinsidegitrepo = 0 ]; then
       remote=`/usr/bin/git remote -v`
       if [ "$remote" != "${remote/[email protected]:myaccount/}" ]; then
           usegat=true
       fi
   fi
fi


if [ $usegat = true ]; then
    # echo "TRUE"
    /usr/bin/git -c core.sshCommand="/usr/bin/ssh -i /home/myaccount/.ssh/mykey" "$@"
else
     #echo "FALSE"
    /usr/bin/git "$@"
fi

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.