{"thread":{"id":"59153","subject":"When using several ssh key, Git match ssh key by host, instead of hostname in ssh config file.","startedAt":"2023-01-26T14:47:02Z","lastAt":"2023-01-27T02:03:03Z","messageCount":3,"participants":["Yangyang Hua","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"471067","messageId":"OSYP286MB0215D880FA9D82B74E393DD691CF9@OSYP286MB0215.JPNP286.PROD.OUTLOOK.COM","threadId":"59153","inReplyTo":null,"subject":"When using several ssh key, Git match ssh key by host, instead of hostname in ssh config file.","fromName":"Yangyang Hua","fromEmail":"hyy_41@live.com","sentAt":"2023-01-26T14:46:54Z","receivedAt":"2023-01-26T14:47:02Z","isPatch":false,"sender":{"key":"hyy_41@live.com","avatar":null},"body":"Hi, \nI 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.\nI 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)\".\nTest in git version 2.38.1.windows.1 with win10 and git version 2.34.1 with Ubuntu 22.04.1 LTS\nI check the -v and -vvv log, git does read my config file in ~/.ssh.\n\nAfter I change the repo address to host(\"github\") in my ssh config file, i can clone the repo and push/pull.\nHost github\n  HostName github.com\n  PreferredAuthentications publickey\n  IdentityFile ~/.ssh/id_rsa_111\nSo I change the host to \"github.com\" in ssh config, everything is back to normal.\nHost github.com\n  HostName github.com\n  PreferredAuthentications publickey\n  IdentityFile ~/.ssh/id_rsa_111\n\n I think when git read ssh config, it uses host to match the key instead of hostname. Is this bug?"},{"id":"471069","messageId":"xmqqk019cmss.fsf@gitster.g","threadId":"59153","inReplyTo":"OSYP286MB0215D880FA9D82B74E393DD691CF9@OSYP286MB0215.JPNP286.PROD.OUTLOOK.COM","subject":"Re: When using several ssh key, Git match ssh key by host, instead of hostname in ssh config file.","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2023-01-26T15:56:03Z","receivedAt":"2023-01-26T15:56:09Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Yangyang Hua <hyy_41@live.com> writes:\n\n> Hi, I find when I use several ssh keys with the right config file\n> and clone my private repo, git can't match the key by hostname.\n> ...\n> I think when git read ssh config, it uses host to match the key\n> instead of hostname. Is this bug?\n\nThis useful feature is given by ssh, and Git does not deserve credit\nfor it.  The config file of SSH allows you to write multiple entries\nthat points at the same host, e.g.\n\n    Host host1\n      HostName host.example.com\n      IdentityFile ~/.ssh/id_rsa_111\n\n    Host host2\n      HostName host.example.com\n      IdentityFile ~/.ssh/id_rsa_222\n\nso that you can specify which key to use for the same destination\nwhen you have more than one user there. \"ssh host1\" uses id_rsa_111\nwhile \"ssh host2\" uses the other one, both connections going to the\nsame destination host.\n\nIf host.example.com were a hosting site like github.com, you can use this\nfeature to say\n\n    $ git push git@host1:/me/lesson1\n\nto connect using id_rsa_111.  If you use\n\n    $ git clone git@github.com:/me/lesson1\n\nthere is no clue which of the two entries you want to use.\n"},{"id":"471084","messageId":"OS0P286MB02117BE1F8660BA82064AC2491CC9@OS0P286MB0211.JPNP286.PROD.OUTLOOK.COM","threadId":"59153","inReplyTo":"xmqqk019cmss.fsf@gitster.g","subject":"Re: When using several ssh key, Git match ssh key by host, instead of hostname in ssh config file.","fromName":"Yangyang Hua","fromEmail":"hyy_41@live.com","sentAt":"2023-01-27T01:57:09Z","receivedAt":"2023-01-27T02:03:03Z","isPatch":false,"sender":{"key":"hyy_41@live.com","avatar":null},"body":"Thank you!\nI understand this feature of ssh.\n\n________________________________________\nFrom: Junio C Hamano <jch2355@gmail.com> on behalf of Junio C Hamano <gitster@pobox.com>\nSent: Friday, 27 January 2023 12:56 am\nTo: Yangyang Hua\nCc: git@vger.kernel.org\nSubject: Re: When using several ssh key, Git match ssh key by host, instead of hostname in ssh config file.\n\nYangyang Hua <hyy_41@live.com> writes:\n\n> Hi, I find when I use several ssh keys with the right config file\n> and clone my private repo, git can't match the key by hostname.\n> ...\n> I think when git read ssh config, it uses host to match the key\n> instead of hostname. Is this bug?\n\nThis useful feature is given by ssh, and Git does not deserve credit\nfor it.  The config file of SSH allows you to write multiple entries\nthat points at the same host, e.g.\n\n    Host host1\n      HostName host.example.com\n      IdentityFile ~/.ssh/id_rsa_111\n\n    Host host2\n      HostName host.example.com\n      IdentityFile ~/.ssh/id_rsa_222\n\nso that you can specify which key to use for the same destination\nwhen you have more than one user there. \"ssh host1\" uses id_rsa_111\nwhile \"ssh host2\" uses the other one, both connections going to the\nsame destination host.\n\nIf host.example.com were a hosting site like github.com, you can use this\nfeature to say\n\n    $ git push git@host1:/me/lesson1\n\nto connect using id_rsa_111.  If you use\n\n    $ git clone git@github.com:/me/lesson1\n\nthere is no clue which of the two entries you want to use.\n"}]}