ssh
has the -i
option to tell which private key file to use when authenticating:
-i identity_file
Selects a file from which
the identity (private key) for RSA or DSA authentication is read.
The default is~/.ssh/identity
for protocol version 1,
and~/.ssh/id_rsa
and~/.ssh/id_dsa
for protocol version 2.
Identity files may also be specified on a per-host basis
in the configuration file. It is possible to have multiple-i
options
(and multiple identities specified in configuration files).
Is there a similar way to tell git
which private key file to use on a system with multiple private keys in the ~/.ssh
directory?
Best Answer
In
~/.ssh/config
, add:If the config file is new, you might need to do
chmod 600 ~/.ssh/config
Now you can do
git clone git@github.com:{ORG_NAME}/{REPO_NAME}.git
{ORG_NAME}
is your GitHub user account (or organization account)'s GitHub URI name.:
aftergithub.com
instead of the slash/
- as this is not a URI.{REPO_NAME}
is your GitHub repo's URI namegit clone git@github.com:torvalds/linux.git
).NOTE: On Linux and macOS, verify that the permissions on your
IdentityFile
are 400. SSH will reject, in a not clearly explicit manner, SSH keys that are too readable. It will just look like a credential rejection. The solution, in this case, is: