threads / discuss / 59153

When using several ssh key, Git match ssh key by host, instead of hostname in ssh config file.

Subject: When using several ssh key, Git match ssh key by host, instead of hostname in ssh config file.

## tl;dr

3 messages between Jan 26, 2023 and Jan 27, 2023.

replies: 2people: 2as markdown or json

Yangyang Hua· Jan 26, 2023, 14:46 UTC · lore

Hi, I find when I use several ssh keys with the right config file and clone my private repo, git can't match the key by hostname. I try ssh-add command to add the keys. "ssh -T git@github.com" can work, but git clone/push/pull these action display "Permission denied (publickey)". Test in git version 2.38.1.windows.1 with win10 and git version 2.34.1 with Ubuntu 22.04.1 LTS I check the -v and -vvv log, git does read my config file in ~/.ssh.

After I change the repo address to host("github") in my ssh config file, i can clone the repo and push/pull.
Host github
  HostName github.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_rsa_111
So I change the host to "github.com" in ssh config, everything is back to normal.
Host github.com
  HostName github.com
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_rsa_111
 I think when git read ssh config, it uses host to match the key instead of hostname. Is this bug?
Junio C Hamano· Jan 26, 2023, 15:56 UTC · re: Yangyang Hua · lore

Re: When using several ssh key, Git match ssh key by host, instead of hostname in ssh config file.

Yangyang Hua <hyy_41@live.com> writes:
Show 5 quoted lines
> Hi, I find when I use several ssh keys with the right config file
> and clone my private repo, git can't match the key by hostname.
> ...
> I think when git read ssh config, it uses host to match the key
> instead of hostname. Is this bug?

This useful feature is given by ssh, and Git does not deserve credit for it. The config file of SSH allows you to write multiple entries that points at the same host, e.g.

    Host host1
      HostName host.example.com
      IdentityFile ~/.ssh/id_rsa_111
    Host host2
      HostName host.example.com
      IdentityFile ~/.ssh/id_rsa_222

so that you can specify which key to use for the same destination when you have more than one user there. "ssh host1" uses id_rsa_111 while "ssh host2" uses the other one, both connections going to the same destination host.

If host.example.com were a hosting site like github.com, you can use this feature to say

    $ git push git@host1:/me/lesson1
to connect using id_rsa_111.  If you use
    $ git clone git@github.com:/me/lesson1
there is no clue which of the two entries you want to use.
Yangyang Hua· Jan 27, 2023, 01:57 UTC · re: Junio C Hamano · lore

Re: When using several ssh key, Git match ssh key by host, instead of hostname in ssh config file.

Thank you! I understand this feature of ssh.

________________________________________
From: Junio C Hamano <jch2355@gmail.com> on behalf of Junio C Hamano <gitster@pobox.com>
Sent: Friday, 27 January 2023 12:56 am
To: Yangyang Hua
Cc: git@vger.kernel.org
Subject: Re: When using several ssh key, Git match ssh key by host, instead of hostname in ssh config file.
Yangyang Hua <hyy_41@live.com> writes:
Show 5 quoted lines
> Hi, I find when I use several ssh keys with the right config file
> and clone my private repo, git can't match the key by hostname.
> ...
> I think when git read ssh config, it uses host to match the key
> instead of hostname. Is this bug?

This useful feature is given by ssh, and Git does not deserve credit for it. The config file of SSH allows you to write multiple entries that points at the same host, e.g.

    Host host1
      HostName host.example.com
      IdentityFile ~/.ssh/id_rsa_111
    Host host2
      HostName host.example.com
      IdentityFile ~/.ssh/id_rsa_222

so that you can specify which key to use for the same destination when you have more than one user there. "ssh host1" uses id_rsa_111 while "ssh host2" uses the other one, both connections going to the same destination host.

If host.example.com were a hosting site like github.com, you can use this feature to say

    $ git push git@host1:/me/lesson1
to connect using id_rsa_111.  If you use
    $ git clone git@github.com:/me/lesson1
there is no clue which of the two entries you want to use.

← back to recent threads