A rather unusual situation perhaps, but I want to specify a private SSH-key to use when executing a shell (git) command from the local computer.
Basically like this:
git clone git@github.com:TheUser/TheProject.git -key "/home/christoffer/ssh_keys/theuser"
Or even better (in Ruby):
with_key("/home/christoffer/ssh_keys/theuser") do
sh("git clone git@github.com:TheUser/TheProject.git")
end
I have seen examples of connecting to a remote server with Net::SSH that uses a specified private key, but this is a local command. Is it possible?
Way better idea to add that host or ip to the
.ssh/config
file like so:To sum up answers and comments, the best way to set up git to use different key files and then forget about it, which also supports different users for the same host (e.g. a personal GitHub account and a work one), which works on Windows as well, is to edit
~/.ssh/config
(orc:\Users\<your user>\.ssh\config
) and specify multiple identities:Then, to clone a project as your personal user, just run the regular
git clone
command.To clone the repo as the
workuser
, rungit clone git@github-work:company/project.git
.Something like this should work (suggested by orip):
if you prefer subshells, you could try the following (though it is more fragile):
Git will invoke SSH which will find its agent by environment variable; this will, in turn, have the key loaded.
Alternatively, setting
HOME
may also do the trick, provided you are willing to setup a directory that contains only a.ssh
directory asHOME
; this may either contain an identity.pub, or a config file setting IdentityFile.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:
EXAMPLES
Use github.com/l3x key:
The following example also uses the github.com/l3x key (by default):
Use bitbucket.org/lsheehan key:
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 use
zsh
and different keys are loaded to my zsh shell'sssh-agent
automatically for other purposes (i.e. access to remote servers) on my laptop. I modified @Nick's answer and I'm using it for one of my repos that needs to be refreshed often. (In this case it's mydotfiles
which I want same and latest version across my all machines, wherever I'm working.)cd
to repo dir is successful, pull from remote repoWith git 2.10+ (Q3 2016: released Sept. 2d, 2016), you have the possibility to set a config for
GIT_SSH_COMMAND
(and not just an environment variable as described in Rober Jack Will's answer)See commit 3c8ede3 (26 Jun 2016) by Nguyễn Thái Ngọc Duy (
pclouds
).(Merged by Junio C Hamano --
gitster
-- in commit dc21164, 19 Jul 2016)It means the
git clone
can be: