35

I'm trying to setup a git client on linux. I uploaded my private key to the machine, and I understand that I should put it in ~/.ssh, but I don't have access to that folder.

How can I tell git to look for the private key somewhere else?

5 Answers 5

51

You can achieve that using a ssh config file.

First create a file inside your ~/.ssh folder named config, you can use some command like the following

$ nano ~/.ssh/config

Then, the content of the file should have the location of your key based on each host name. for example:

Host github.com
 IdentityFile ~/myPublicKeyFolder/myGitHubFile
Host heroku.com
 IdentityFile ~/myPublicKeyFolder/myHerokuFile

So, when git tries to access each host it will follow the rules inside this config file based on the git host your trying to reach

Sign up to request clarification or add additional context in comments.

Comments

43

One option is to use ssh-agent and provide a file name to ssh-add.

For example:

$ ssh-agent /bin/bash
$ ssh-add ~/mykeys/id_rsa

2 Comments

It did not work. $ ssh-agent /bin/bash $ ssh-add /home/deepakkv/gitkeys/id_rsa Identity added: /home/deepakkv/gitkeys/id_rsa (/home/deepakkv/gitkeys/id_rsa) $ git push origin master Permission denied (publickey). fatal: The remote end hung up unexpectedly $
git used your private key, so it looks like this part worked. Probably there is something missing on the server side (wrong key, key not registered, etc.) You may want to ask a new question.
12

I would have said put the file name in ~/.ssh/config, but you likely would not have access to this file, too.

You can give ssh the private key to use with the -i keyfile option.

Now how to say git which options to pass to ssh?

The GitTips page says create a wrapper script and point to it with the GIT_SSH environment variable.

It looks like you also can use the git configuration core.gitProxy, but I did not find a good example and some mailing list message suggests it is only for the git: protocol.

2 Comments

@dragon788 thanks for fixing the link, but you could have linked to the right section in that page (as I did now). (Just a hint for the next time.)
I wasn't quite sure from the original article name and link whether it was intentionally to a specific section, since the link name was 'Gittips page' which is where it now points. I was also tempted to add the GIT_SSH_COMMAND since that is now apparently a supported environment variable, but at the time I just wanted to fix the broken link.
4

Use ssh-agent

ssh-agent bash -c 'ssh-add /home/me/my_private_key; git clone [email protected]:uname/test-git-repo.git'

Comments

1

For a project I am working on my app needs to spit out a shell script with all of the git commands to init/commit/push to an external repository. The ~/.ssh/config is off limits so I have my public/private keys in my app directory. I used vhallac's answer. This is what I had to do in my shell script to use my key:

eval `/usr/bin/ssh-agent`
ssh-add /path/to/.ssh/id_rsa

hope this helps someone

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.