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.)
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 .
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 .
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)
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With