3

I have a SSH key to allow me to push to a certain repository. No problem there with the key itself - however, I don't want to copy the key to a .ssh folder or leave the SSH key with ssh-agent (on other computers) and would rather have a portable script solution.

So I'm looking for the equivalent of how I can specify an identity with -i when connecting with ssh:

ssh -i /my/individual/path/id_rsa user@server

Also, with HTTPS on GitHub, for example, I can generate a token online and then do:

git remote add origin "https://[email protected]/USER/REPO.git"

Is there something similar for SSH keys, so I can use them with git directly?

2 Answers 2

3

Yes... you can use ~/.ssh/config instead of ssh-agent
just add this in ~/.ssh/config file

Host github.com
  IdentityFile /my/individual/path/id_rsa
2

The GIT_SSH_COMMAND environment variable can be used to change the SSH command that Git will use:

GIT_SSH_COMMAND="ssh -i /path/to/your/id_rsa" git clone git@whatever

Alternatively, there is the core.sshCommand setting for git config:

git config core.sshCommand "ssh -i /path/to/your/id_rsa"

You can also use relative paths. So if you keep your id_rsa key in the parent folder of the repository root folder, the following config will make git commands use that key - no matter which repository folder is the current work directory:

git config core.sshCommand "ssh -i ../id_rsa"

Note: You should also add -F /dev/null to those commands to prevent the local ssh configs /etc/ssh/ssh_config and ~/.ssh/config from being read. For example, they might contain AddKeysToAgent yes which would automatically add the key to ssh-agent.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .