How to tell git which private key to use?
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle2
--
Chapters
00:00 Question
01:01 Accepted answer (Score 1032)
02:34 Answer 2 (Score 709)
03:46 Answer 3 (Score 184)
05:59 Answer 4 (Score 93)
06:23 Thank you
--
Full question
https://superuser.com/questions/232373/h...
Accepted answer links:
[for the Linux kernel]: https://github.com/torvalds/linux
Answer 3 links:
[GIT_SSH]: https://github.com/git/git/commit/4852f7...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#ssh #git #authentication #privatekey
#avk47
ACCEPTED ANSWER
Score 1105
In ~/.ssh/config
, add:
Host github.com
HostName github.com
IdentityFile ~/.ssh/id_rsa_github
If the config file is new, check access permissions using
stat -c %a ~/.ssh/config
if returns NOT 600, you should do
chmod 600 ~/.ssh/config
Now you can do git clone git@github.com:{ORG_NAME}/{REPO_NAME}.git
- Where
{ORG_NAME}
is your GitHub user account (or organization account)'s GitHub URI name.- Note that there is a colon
:
aftergithub.com
instead of the slash/
- as this is not a URI.
- Note that there is a colon
- And
{REPO_NAME}
is your GitHub repo's URI name - For example, for the Linux kernel this would be
git 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:
chmod 400 ~/.ssh/id_rsa_github
ANSWER 2
Score 790
Environment variable GIT_SSH_COMMAND
From Git version 2.3.0, you can use the environment variable GIT_SSH_COMMAND
like this:
GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_example" git clone example
Note that -i
can sometimes be overridden by your config file, in which case, you should give SSH an empty config file, like this:
GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_example -F /dev/null" git clone git@github.com:example/example.git
Configuration core.sshCommand
Since Git version 2.10.0, you can configure this per repo or globally, using the core.sshCommand
setting. There's no more need to use the environment variable. Here's how you clone a repo and set this configuration at the same time:
git clone -c "core.sshCommand=ssh -i ~/.ssh/id_rsa_example -F /dev/null" git@github.com:example/example.git
cd example/
git pull
git push
If the repo already exists, run:
git config core.sshCommand "ssh -i ~/.ssh/id_rsa_example -F /dev/null"
The configuration is saved in .git/config
ANSWER 3
Score 195
There is no direct way to tell git
which private key to use, because it relies on ssh
for repository authentication. However, there are still a few ways to achieve your goal:
Option 1: ssh-agent
You can use ssh-agent
to temporarily authorize your private key.
For example:
$ ssh-agent sh -c 'ssh-add ~/.ssh/id_rsa; git fetch user@host'
Option 2: GIT_SSH_COMMAND
Pass the ssh arguments by using the GIT_SSH_COMMAND
environment variable
(Git 2.3.0+).
For example:
$ GIT_SSH_COMMAND='ssh -i ~/.ssh/id_rsa -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no' \
git clone user@host
You can type this all on one line — ignore $
and leave out the \
.
Option 3: GIT_SSH
Pass the ssh arguments by using the GIT_SSH
environment variable to specify alternate ssh
binary.
For example:
$ echo 'ssh -i ~/.ssh/id_rsa -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $*' > ssh
$ chmod +x ssh
$ GIT_TRACE=1 GIT_SSH='./ssh' git clone user@host
Note: The above lines are shell (terminal) command lines which you should paste into your terminal. They will create a file named ssh
, make it executable, and (indirectly) execute it.
Note: GIT_SSH
is available since v0.99.4 (2005).
Option 4: ~/.ssh/config
Use the ~/.ssh/config
file as suggested in other answers in order to specify the location of your private key, e.g.
Host github.com
User git
Hostname github.com
IdentityFile ~/.ssh/id_rsa
ANSWER 4
Score 100
Use custom host config in ~/.ssh/config
, like this:
Host gitlab-as-thuc
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa.thuc
IdentitiesOnly yes
then use your custom hostname like this:
git remote add thuc git@gitlab-as-thuc:your-repo.git