Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify the private SSH-key to use when executing shell command on Git?

Tags:

git

bash

shell

ssh

A rather unusual situation perhaps, but I want to specify a private SSH-key to use when executing a shell (git) command from the local computer.

Basically like this:

git clone [email protected]:TheUser/TheProject.git -key "/home/christoffer/ssh_keys/theuser" 

Or even better (in Ruby):

with_key("/home/christoffer/ssh_keys/theuser") do   sh("git clone [email protected]:TheUser/TheProject.git") end 

I have seen examples of connecting to a remote server with Net::SSH that uses a specified private key, but this is a local command. Is it possible?

like image 788
Christoffer Avatar asked Dec 30 '10 19:12

Christoffer


People also ask

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 use a specific SSH key in GitHub?

First you should declare your different keys in ~/. ssh/config file. By doing this you associate the second key with a new friendly name "XXX" for github.com. Then you must change the remote origin of your particular repository, so that it uses the friendly name you've just defined.

How does Git decide which SSH key to use?

Git does not know, or care. It just runs ssh. Specifies that ssh(1) should only use the authentication identity files configured in the ssh_config files, even if ssh-agent(1) or a PKCS11Provider offers more identities.

How do I SSH to a Git bash key?

To connect, use Login and Password credentials pair or an SSH key pair. If you don't have keys yet, you can generate them on your side using the Git Bash app. This command creates a new SSH key, using the provided email as a label. When you're prompted to “Enter a file in which to save the key”, press Enter.


2 Answers

None of these solutions worked for me.

Instead, I elaborate on @Martin v. Löwis 's mention of setting a config file for SSH.

SSH will look for the user's ~/.ssh/config file. I have mine setup as:

Host gitserv     Hostname remote.server.com     IdentityFile ~/.ssh/id_rsa.github     IdentitiesOnly yes # see NOTES below 

And I add a remote git repository:

git remote add origin git@gitserv:myrepo.git 

And then git commands work normally for me.

git push -v origin master 

NOTES

  • The IdentitiesOnly yes is required to prevent the SSH default behavior of sending the identity file matching the default filename for each protocol. If you have a file named ~/.ssh/id_rsa that will get tried BEFORE your ~/.ssh/id_rsa.github without this option.

References

  • Best way to use multiple SSH private keys on one client
  • How could I stop ssh offering a wrong key
like image 107
HeyWatchThis Avatar answered Oct 02 '22 21:10

HeyWatchThis


Something like this should work (suggested by orip):

ssh-agent bash -c 'ssh-add /somewhere/yourkey; git clone [email protected]:user/project.git' 

if you prefer subshells, you could try the following (though it is more fragile):

ssh-agent $(ssh-add /somewhere/yourkey; git clone [email protected]:user/project.git) 

Git will invoke SSH which will find its agent by environment variable; this will, in turn, have the key loaded.

Alternatively, setting HOME may also do the trick, provided you are willing to setup a directory that contains only a .ssh directory as HOME; this may either contain an identity.pub, or a config file setting IdentityFile.

like image 31
Martin v. Löwis Avatar answered Oct 02 '22 22:10

Martin v. Löwis