12

Is it possible to access (fetch/push) a remote repository using ssh and an identity file (with the private key) without adding an entry in the file ~/.ssh/config such as:

Host tingle
  HostName 111.222.333.444
  User git
  IdentityFile c/tmp/my_id_rsa

Everything works fine when configuring the ~/.ssh/config file. However we have a script which clones from a remote repo, checks out, starts testing, commits results and pushes them. The script need to run on any machine without touching the ssh config file.

Paul Pladijs
  • 18,628
  • 5
  • 28
  • 31
  • Could you create a custom config file with what you have above that's only used where you need it (e.g. `ssh tingle -F my_custom_config`)? – Ownaginatious Jun 07 '16 at 18:23
  • This answer might be useful. I stumbled on it while looking for something similar earlier this week. http://stackoverflow.com/a/4565746/1789724 – austin Jun 07 '16 at 18:29
  • Check the post here: >http://stackoverflow.com/questions/7927750/specify-an-ssh-key-for-git-push-for-a-given-domain – prateek05 Jun 07 '16 at 18:31

4 Answers4

10

You can use the variable $GIT_SSH, see the documentation, to set a program that is invoked instead of ssh.

That way you can, e.g. do GIT_SSH=/my/own/ssh git clone https://my.own/repo.git

Adapt the contents of /my/own/ssh to your own need, e.g.:

#!/bin/bash
# Wrapper for ssh, to use identity file and known hosts file
exec /usr/bin/ssh -i /my/own/identity_file-o UserKnownHostsFile=/my/own/hosts.file "$@"

As far as I know this is currently the only way to do this without rather untidy path manipulations.

M. Glatki
  • 777
  • 6
  • 18
  • 1
    This works. Especially handy if our script creates an own host config file and add to the wrapper: exec /usr/bin/ssh - F our_ssh_host "$@" – Paul Pladijs Jun 17 '16 at 09:24
4

You could override the $GIT_SSH environment variable to use your own private key:

First, create a wrapper script. Let assume we call it gitssh.sh:

#!/bin/bash
ssh -i /path/to/mykey "$@"

Then, point $GIT_SSH to it:

export GIT_SSH=/path/to/gitssh.sh

Now, whenever you run a git command over ssh, it will be substituted with this script, and references your key.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
3

The following should do the trick

GIT_SSH_COMMAND="ssh -i c/tmp/my_id_rsa" git push

This allows you to add parameters to the ssh execution without the need of an additional script file.

Depending on your script you can fine tune this by defining and exporting the environment variable GIT_SSH_COMMAND before the actual execution of git or use if etc. to only use it when you are communicating with tingle

In your config file you mention user and host, too. This should already be part of the git remote definition. If you still need to override this you could add these to the above command definition.

One remark the command above is not checking which host actually is invoked by git. But if you are desperate you could try to build a inline shell script with this "trick" that checks the hostname and mimics the config file host restriction ;-).

As a starter that would maybe look like this: GIT_SSH_COMMAND="bash -c \"if [ \\\"$1\\\" = \\\"user@host\\\" ] ; then ssh -i c/tmp/my_id_rsa $@ ; else ssh $@ ; fi \" -- " git push

I didn't test this and be aware of quoting ;-). It's for the desperate.

murraybo
  • 895
  • 8
  • 13
3

If you want to use a custom ssh key you can try with this:

ssh-agent bash -c 'ssh-add /path/to/your/id_rsa; git clone git@github.com:repo'

In this way you don't need to write/edit any config file or modify your enviroment.

Dario
  • 3,905
  • 2
  • 13
  • 27