Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select private key to use with Git

Tags:

git

ssh

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.)

like image 244
user349302 Avatar asked Jul 14 '11 04:07

user349302


People also ask

How do I force git to use a specific SSH key?

You can replace the two commands with this one command: git clone -c "core. sshCommand=ssh -i ~/. ssh/<your_key>" [email protected]:<user>/<repo>. git .

How do I specify a private SSH key?

To specify a private key file in SSH from the command line, you can simply use -i option in the ssh command. However, things get complicated when you have multiple private keys. In that case, you can declare which private key to use for each SSH server, in your SSH configuration file which is found at ~/. ssh/config .

How do I find my git private key?

Just follow these 5 steps: Go to this address, and download Git for Windows, after the download install it with default settings. Open Git Bash that you just installed (Start->All Programs->Git->Git Bash) Type in the following: ssh-keygen -t rsa (when prompted, enter password, key name can stay the same)

How do I choose which SSH key to use?

Method one, specify the key in command line with the -i option of ssh. Check more in the manual. Method two, use the ssh config file. This is useful when you do not have the -i option available, such as using git, rsync or lftp.


2 Answers

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.

like image 127
Richard Smith Avatar answered Oct 14 '22 01:10

Richard Smith


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.

like image 32
Cameron Skinner Avatar answered Oct 13 '22 23:10

Cameron Skinner