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.
You can use the variable $GIT_SSH
, see the documentation , to set a program that is invoked instead of ssh.
That way you can, eg do GIT_SSH=/my/own/ssh git clone https://my.own/repo.git
Adapt the contents of /my/own/ssh
to your own need, eg:
#!/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.
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.
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 -ic/tmp/my_id_rsa $@ ; else ssh $@ ; fi \\" -- " git push
GIT_SSH_COMMAND="bash -c \\"if [ \\\\\\"$1\\\\\\" = \\\\\\"user@host\\\\\\" ] ; then ssh -ic/tmp/my_id_rsa $@ ; else ssh $@ ; fi \\" -- " git push
I didn't test this and be aware of quoting ;-). It's for the desperate.
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.
You can use the .pem file to create a secure connection to the git server
ssh -i "yourfile.pem" yourHostName.com
.pem is nothing but like a certificate file which has the key info.
Check out http://www.beginninglinux.com/home/server-administration/openssh-keys-certificates-authentication-pem-pub-crt ! you can find more details of the method here.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.