Introduction
SSH servers enforce a MaxAuthTries limit (default 6) to protect against brute force password attacks. When this limit is exceeded, the server disconnects with Received disconnect: Too many authentication failures. This can affect legitimate users who have multiple SSH keys loaded in their agent, as each key counts as one authentication attempt. Users with 10+ keys in their agent may exhaust the limit before their correct key is tried.
Symptoms
ssh user@hostdisconnects withReceived disconnect from X port 22: 2: Too many authentication failures for userAuthentication failed.after several key offersssh -vshows multipleOffering public key:lines before disconnect- Connection works with
ssh -o IdentitiesOnly=yesbut not without it - Server log shows
maximum authentication attempts exceeded
Common Causes
- SSH agent has many keys loaded, each counting toward MaxAuthTries
- MaxAuthTries set very low (e.g., 3) by security hardening
- User's correct key is not the first one tried by the SSH client
- Fail2ban or similar tools reducing effective attempt count
- Multiple SSH keys added via
ssh-addover time without cleanup
Step-by-Step Fix
- 1.Check which keys are being offered:
- 2.```bash
- 3.ssh -v user@host 2>&1 | grep "Offering public key"
- 4.# Count the number of keys being offered before your correct one
- 5.
` - 6.Connect with only the specific key:
- 7.```bash
- 8.ssh -o IdentitiesOnly=yes -i ~/.ssh/id_ed25519_correct user@host
- 9.# This prevents the agent from offering all its keys
- 10.
` - 11.Clean up the SSH agent:
- 12.```bash
- 13.# List all loaded keys
- 14.ssh-add -l
- 15.# Remove all keys
- 16.ssh-add -D
- 17.# Add only the key you need
- 18.ssh-add ~/.ssh/id_ed25519_correct
- 19.
` - 20.Configure SSH to prefer specific keys per host:
- 21.```bash
- 22.# In ~/.ssh/config:
- 23.Host production
- 24.HostName prod.example.com
- 25.User deploy
- 26.IdentityFile ~/.ssh/prod_key
- 27.IdentitiesOnly yes
- 28.
` - 29.Adjust MaxAuthTries on the server if appropriate:
- 30.```bash
- 31.sudo nano /etc/ssh/sshd_config
- 32.# Change from default 6 to a higher value if needed
- 33.MaxAuthTries 10
- 34.sudo systemctl restart sshd
- 35.# Note: Lower values are more secure against brute force
- 36.
` - 37.Configure server-side key-only authentication to reduce attempts:
- 38.```bash
- 39.sudo nano /etc/ssh/sshd_config
- 40.# Disable password authentication entirely
- 41.PasswordAuthentication no
- 42.PubkeyAuthentication yes
- 43.sudo systemctl restart sshd
- 44.
`
Prevention
- Use
IdentitiesOnly yesin SSH config for all production hosts - Keep the SSH agent clean - remove keys that are no longer needed
- Use SSH certificates instead of individual keys for large key inventories
- Implement bastion hosts to reduce the number of keys each user needs
- Monitor failed SSH attempts with fail2ban but configure it to not lock out legitimate users