Ssh Auth Errors

2021-03-13

I’ve been having a weird issue with my Macbook at work in the last month or so whereby I would be happily working away and then at some point, seemingly at random I would suddenly get rejected when connecting to a server with the following error message:

Received disconnect from XX.XX.XX.XX port 22:2: Too many authentication failures Disconnected from XX.XX.XX.XX port 22

It had been bugging me for a little while but a restart fixed it but I had a lot of patching to do so wanted to fix it properly rather than rebooting all day.

After a fair bit of googling I came across this blog post which helped me understand the root cause.

For reasons unknown at some point I had added AddKeysToAgent to my ~/.ssh/config. What this was effectively doing was running ssh-add on any key that I was using to connect to a server. Once added these were then being offered to each server I tried to SSH to by default and after a certain point causing the authentication failure and breaking my ssh.

Once I understood it was a fairly easy fix.

  1. ssh-add -D to remove all the cached keys.
  2. Edit the ~/.ssh/config file. Mine is now as follows:

UseKeychain yes AddKeysToAgent no

Host bitbucket.org IdentitiesOnly yes IdentityFile ~/.ssh/bitbucket

This means my ssh config no longer adds the keys to my agent when I connect which stops the error from occurring. While I was at it I also set up a rule for bitbucket which tells SSH to use that specific key when connecting to the bitbucket.org domain.

Enter your instance's address