I have generated several ssh keys and have placed in git server. Is it possible somehow to know which ssh key was used while executing git clone
command?

- 17,051
- 45
- 159
- 315
-
Per http://stackoverflow.com/questions/25388499/how-can-i-run-git-push-pull-commands-with-ssh-verbose-mode/36038548#36038548 you can doc `GIT_SSH_COMMAND="ssh -v" git clone example` – sideshowbarker Dec 08 '16 at 13:28
2 Answers
You can see your currently active ssh-key
by ssh-add
command.
$ ssh-add # show active ssh-key file path
You can customize it also. Open ~/.ssh/config
file and find Host <hostname>
, then the IdentifyFile
points the id_rsa
file that git clone is using for that <hostname>
.
$ cat ~/.ssh/config
// sample output
Host bitbucket.org
User git
Hostname bitbucket.org
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
Here, As git clone git@bitbucket.org:<user>/<repo>.git
is using bitbucket.org
Host and user git
, so ~/.ssh/id_rsa
file is using as ssh-key and you need to save ~/.ssh/id_rsa.pub
in BitBucket account.
Now, if you add another ssh-key in ~/.ssh/config file like -
Host bitbucket-alice
User git
Hostname bitbucket.org
PreferredAuthentications publickey
IdentitiesOnly yes
IdentityFile ~/.ssh/alice
You need to clone with git clone git@bitbucket-alice:<user>/<repo>.git
and it will use ~/.ssh/alice
and you need to add ~/.ssh/alice.pub
in your BitBucket account.

- 22,878
- 9
- 63
- 73
-
Well, that would say which would be used now, not which one was used when doing the clone. ;-) – Vampire Dec 08 '16 at 10:07
-
@Vampire I've updated my answer showing which `ssh-key` is using when cloning a repo. – Sajib Khan Dec 08 '16 at 10:23
-
But it has nothing to do with the question. He already cloned and want to know which key was used. You just tell him how to lookup which would be used if he would do a fresh clone now. – Vampire Dec 08 '16 at 10:42
-
Simply he can check `git clone url` that has used and `~/.ssh/config` file, then it is clear which ssh-key was used. – Sajib Khan Dec 08 '16 at 10:46
-
Only if ssh config didn't change and other config also didn't change anything in that selection, like some auth agent being active and so on. – Vampire Dec 08 '16 at 10:49
-
ok. In that case, `ssh-add` will show currently active ssh-key. I've updated the answer. – Sajib Khan Dec 08 '16 at 10:56
-
Exactly, the **currently** active key, not the key with which the repo was cloned, whenever it was. – Vampire Dec 08 '16 at 11:24
You can look into the authentication log file of your Git server. In the local Git clone you have no chance.

- 35,631
- 4
- 76
- 102