Bash: fixing “Too many authentication failures” for ssh with private key authentication

If you are using ssh private/public keypair authentication, and get an almost immediate error like below:

$ ssh -i id_rsa.pub myuser@a.b.c.d -p 22
Received disconnect from a.b.c.d port 22:2: Too many authentication failures
Disconnected from a.b.c.d port 22

Then try again using the ‘IdentitiesOnly‘ option.

ssh -o 'IdentitiesOnly yes' -i id_rsa.pub myuser@a.b.c.d -p 22

The root problem might be that your ssh client is testing too many public keys against the server, and this flag will limit it to the single public key you specified.

Because even though you have specified a single public key file with the ‘-i’ flag, there are still other keys your ssh client will attempt when connecting including those from ssh-agent and “~/.ssh/config”.  If you want to see the list of public keys your ssh client is sending, use full verbosity “-vvv”

# full verbosity, look for 'Trying private key'
ssh -vvv -i id_rsa.pub myuser@a.b.c.d -p 22

 

REFERENCES

Tom Donohue, Can’t ssh you may have too many keys

David Cao, 3 ways to fix SSH Too many authentication failures

superuser.com, Too many authentication errors

stackoverflow.com, best way to use multiple ssh private keys on one client

ssh_config man page

NOTES

The syntax “-i <key>” and “-o IdentityFile=<key>” are equivalent.