{"thread":{"id":"55400","subject":"[BUG] Git is not using the configured SSH key when there is another SSH key added to the SSH agent","startedAt":"2021-03-27T18:31:52Z","lastAt":"2021-03-27T19:00:55Z","messageCount":3,"participants":["Utku","brian m. carlson"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"420306","messageId":"967BFF88-A8E1-4EEC-B298-668012E42C03@gmail.com","threadId":"55400","inReplyTo":null,"subject":"[BUG] Git is not using the configured SSH key when there is another SSH key added to the SSH agent","fromName":"Utku","fromEmail":"ugultopu@gmail.com","sentAt":"2021-03-27T18:31:10Z","receivedAt":"2021-03-27T18:31:52Z","isPatch":false,"sender":{"key":"ugultopu@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9937444?v=4"},"body":"# Versions\n\n- Git 2.30.1\n- macOS 10.15.7\n\n# Steps to Reproduce\n\n- Have two SSH key pairs on your machine, one with a passphrase and\nother without. The keys were created by running `ssh-keygen` in my\ncase.\n\n- Add the key with a passphrase to the SSH agent by running `ssh-add`\nand entering the passphrase.\n\n- Have an entry like the following in your `~/.ssh/config`:\n\n      Host someName\n          HostName bitbucket.org\n          User git\n          IdentityFile ~/.ssh/private-key-of-the-pair-without-a-passphrase\n\n\n- Add the public key of the pair without a passphrase to your BitBucket\naccount.\n\n- Run `git clone someName:path/to/your/repository.git`. You will get a\nnot authorized error. As far as I can tell, this means that Git has\ntried to use the key with the passphrase (which is added to the SSH\nagent). Since this key is **not** the key that was added to the\nBitBucket account, we received a not authorized error.\n\n- Add the key without a passphrase to your SSH agent by running\n`ssh-add` and try the same command (`git-clone`) again. It will work.\n\n- Now, remove both keys from the SSH agent by running `ssh-add -D` and\ntry and try the same command (`git-clone`) again. It will still work.\n\nI didn't test but don't believe this has something to do with the key\nhaving a passphrase or not. Also, I believe that I'm not using macOS\nKeychain to store the decrypted keys.\n\n# Expected Behavior\n\nThe `git-clone` command to work as expected, regardless of which keys\nare present in the SSH agent, since the information that I provide to\nGit (via the `Host` information in the `~/.ssh/config` file) is\nsufficient.\n\n# Actual Behavior\n\nGit is trying to use the SSH key that is present in the SSH agent,\ninstead of using the SSH key which is explicitly declared in the\n`~/.ssh/config` file.\n\n"},{"id":"420309","messageId":"YF9/gCIWaFBicdpo@camp.crustytoothpaste.net","threadId":"55400","inReplyTo":"967BFF88-A8E1-4EEC-B298-668012E42C03@gmail.com","subject":"Re: [BUG] Git is not using the configured SSH key when there is another SSH key added to the SSH agent","fromName":"brian m. carlson","fromEmail":"sandals@crustytoothpaste.net","sentAt":"2021-03-27T18:54:56Z","receivedAt":"2021-03-27T18:56:00Z","isPatch":false,"sender":{"key":"sandals@crustytoothpaste.net","avatar":"https://avatars.githubusercontent.com/u/497054?v=4"},"body":"On 2021-03-27 at 18:31:10, Utku wrote:\n> # Versions\n> \n> - Git 2.30.1\n> - macOS 10.15.7\n> \n> # Steps to Reproduce\n> \n> - Have two SSH key pairs on your machine, one with a passphrase and\n> other without. The keys were created by running `ssh-keygen` in my\n> case.\n> \n> - Add the key with a passphrase to the SSH agent by running `ssh-add`\n> and entering the passphrase.\n> \n> - Have an entry like the following in your `~/.ssh/config`:\n> \n>       Host someName\n>           HostName bitbucket.org\n>           User git\n>           IdentityFile ~/.ssh/private-key-of-the-pair-without-a-passphrase\n> \n> \n> - Add the public key of the pair without a passphrase to your BitBucket\n> account.\n> \n> - Run `git clone someName:path/to/your/repository.git`. You will get a\n> not authorized error. As far as I can tell, this means that Git has\n> tried to use the key with the passphrase (which is added to the SSH\n> agent). Since this key is **not** the key that was added to the\n> BitBucket account, we received a not authorized error.\n> \n> - Add the key without a passphrase to your SSH agent by running\n> `ssh-add` and try the same command (`git-clone`) again. It will work.\n> \n> - Now, remove both keys from the SSH agent by running `ssh-add -D` and\n> try and try the same command (`git-clone`) again. It will still work.\n> \n> I didn't test but don't believe this has something to do with the key\n> having a passphrase or not. Also, I believe that I'm not using macOS\n> Keychain to store the decrypted keys.\n\nThis behavior is completely due to SSH.  Git doesn't tell OpenSSH to\nbehave in any particular way; instead, it tells it to make a connection\nwith the specified parameters to the specified server, and OpenSSH makes\nits own decision about what that means.\n\nIn your case, the option you want is \"IdentitiesOnly yes\".  This tells\nOpenSSH to always use the specified identity, even if a different one is\navailable in the agent.  You can add it to the configuration that you\nhave above and things should work.\n\nYou can also see an example of this configuration in the FAQ:\nhttps://git-scm.com/docs/gitfaq#multiple-accounts-ssh\n-- \nbrian m. carlson (he/him or they/them)\nHouston, Texas, US\n"},{"id":"420310","messageId":"CF3B8F00-BEEB-418B-8500-450141650779@gmail.com","threadId":"55400","inReplyTo":"YF9/gCIWaFBicdpo@camp.crustytoothpaste.net","subject":"Re: [BUG] Git is not using the configured SSH key when there is another SSH key added to the SSH agent","fromName":"Utku","fromEmail":"ugultopu@gmail.com","sentAt":"2021-03-27T18:58:52Z","receivedAt":"2021-03-27T19:00:55Z","isPatch":false,"sender":{"key":"ugultopu@gmail.com","avatar":"https://avatars.githubusercontent.com/u/9937444?v=4"},"body":"Adding `IdentitiesOnly yes` solves it. Thank you so much Brian!\n"}]}