If you're like me, you can:
Keep your ssh keys organized
Keep your git clone commands simple
Handle any number of keys for any number of repositories.
Reduce your ssh key maintenance.
I keep my keys in my ~/.ssh/keys
directory.
I prefer convention over configuration.
I think code is law; the simpler it is, the better.
STEP 1 - Create Alias
Add this alias to your shell: alias git-clone='GIT_SSH=ssh_wrapper git clone'
STEP 2 - Create Script
Add this ssh_wrapper script to your PATH:
#!/bin/bash
# Filename: ssh_wrapper
if [ -z ${SSH_KEY} ]; then
SSH_KEY='github.com/l3x' # <= Default key
fi
SSH_KEY="~/.ssh/keys/${SSH_KEY}/id_rsa"
ssh -i "${SSH_KEY}" "$@"
EXAMPLES
Use github.com/l3x key:
KEY=github.com/l3x git-clone https://github.com/l3x/learn-fp-go
The following example also uses the github.com/l3x key (by default):
git-clone https://github.com/l3x/learn-fp-go
Use bitbucket.org/lsheehan key:
KEY=bitbucket.org/lsheehan git-clone [email protected]:dave_andersen/exchange.git
NOTES
Change the default SSH_KEY in the ssh_wrapper script to what you use most of the time. That way, you don't need to use the KEY variable most of the time.
You may think, "Hey! That's a lot going on with an alias, a script and some directory of keys," but for me it's convention. Nearly all my workstations (and servers for that matter) are configured similarly.
My goal here is to simplify the commands that I execute regularly.
My conventions, e.g., Bash scripts, aliases, etc., create a consistent environment and helps me keep things simple.
KISS and names matter.
For more design tips check out Chapter 4 SOLID Design in Go from my book: https://www.amazon.com/Learning-Functional-Programming-Lex-Sheehan-ebook/dp/B0725B8MYW
Hope that helps. - Lex
-i
option likessh
does. – Nick Tgit config core.sshCommand 'ssh -i private_key_file'
. See my answer below – VonC