A brute force attack is an automated attempt to log in by trying many passwords until one works. You prevent it by limiting failed attempts per account, slowing or locking after repeated failures, requiring multi-factor authentication, and making sure passwords are long and not on a breached-password list.
This article is about online attacks against a live login form, API or SSH server, where every guess costs a network round trip. Offline cracking of a stolen password hash is a different problem, covered in how long it takes to crack a password.
Three kinds of password-guessing attack
OWASP's Authentication Cheat Sheet separates three automated attacks, and the difference matters because each one dodges a different defence:
| Attack | What the attacker tries | What it dodges |
|---|---|---|
| Brute force | Many passwords from a dictionary against one account | Nothing, if attempts per account are limited |
| Password spraying | One weak password (for example Winter2026!) against many accounts | Per-account lockout, because each account sees only one or two failures |
| Credential stuffing | Username and password pairs leaked from another site | Password strength rules, because the leaked password may be long and complex |
A defence that only counts failures per account stops the first, but not the second. A defence that only blocks noisy IP addresses misses all three when the attacker spreads requests over thousands of addresses. You need layers.
Limit failed attempts per account
NIST SP 800-63B-4, section 3.2.2, is the clearest rule: the verifier must limit consecutive failed attempts on one authenticator to no more than 100, by disabling that authenticator. NIST calls 100 "an upper bound" and allows lower limits. That sounds generous until you do the arithmetic. If the attacker is limited to 100 guesses per account, a password that isn't in the top 100 of their list survives. That is why the same document requires the blocklist check described below.
To keep lockouts from hurting real users, NIST lists optional techniques that cut down how often legitimate users hit the limit:
- a bot-detection challenge before an attempt
- a wait after each failure that grows as the account nears its limit (NIST's example runs from 30 seconds to an hour)
- risk-based checks using IP address, geolocation, timing or browser metadata
OWASP describes the same mechanism in terms of a lockout threshold (failures allowed), an observation window (the time they must fall within) and a lockout duration. It also suggests an exponential lockout that starts at around one second and doubles after each failed login. Two rules from OWASP are easy to get wrong:
- Count failures against the account, not the source IP address. An attacker with a botnet changes IP on every request.
- Don't let lockout become a denial of service. Anyone who knows a username can lock that person out by failing on purpose. OWASP's suggested mitigation is to let locked-out users recover through the forgotten-password flow.
Rate-limit by IP address as well
Per-IP limits don't replace per-account limits, but they stop a single machine from spraying thousands of accounts. In nginx it takes two lines. The nginx documentation's example allows an average of one request per second per client address, with a burst of five:
http {
limit_req_zone $binary_remote_addr zone=login:10m rate=1r/s;
server {
location /login {
limit_req zone=login burst=5;
limit_req_status 429;
}
}
}
By default nginx rejects excess requests with 503; limit_req_status 429 returns the more accurate "Too Many Requests" instead. The 10 MB zone is plenty: the documentation says one megabyte holds about 8,000 client states on 64-bit platforms, so 10 MB tracks roughly 80,000 addresses.
Put the arithmetic next to that. One request per second is 86,400 guesses per day from one address, so the per-IP limit alone does not protect a weak password. It does make spraying from one address slow, and it keeps your login endpoint from being flooded.
Require multi-factor authentication
OWASP is blunt about this: "Multi-factor authentication (MFA) is by far the best defense against the majority of password-related attacks." A correct guess or a leaked password is useless without the second factor. That is the only defence on this list that fully covers credential stuffing, since the attacker already holds the right password. Phishing-resistant options such as passkeys go further and remove the password from the login entirely.
Make the password itself hard to guess
SP 800-63B-4, section 3.1.1.2, sets these rules for services that use passwords:
- at least 15 characters when the password is the only factor, and at least 8 when it is used as part of MFA
- accept passwords of at least 64 characters
- check new passwords against a blocklist of commonly used, expected or compromised passwords, and tell the user why one was rejected
- no composition rules (such as "one digit and one symbol")
- no forced periodic changes, only a forced change when there is evidence of compromise
The blocklist is the rule that does the work against online guessing. An attacker limited to 100 attempts per account will try the 100 most common passwords; if your signup form already rejects those, the attempts are wasted. For users, a random password from a generator such as larely's password generator, stored in a password manager, is never on anyone's list.
Don't help the attacker
Error messages should not reveal whether the username exists. OWASP's example of a correct response is Login failed; Invalid user ID or password. The same applies to the password reset form and to response timing. If "unknown user" returns noticeably faster than "wrong password", an attacker can build a list of valid usernames first and then spray only those.
OWASP treats CAPTCHA as defence in depth, not prevention, and suggests showing it only after a few failed attempts.
How to protect an SSH server from brute force
An SSH server on a public IP address gets password guesses constantly. The most effective fix is to stop accepting passwords at all and log in with a key (see how to generate an Ed25519 SSH key). Once your key works, set these in /etc/ssh/sshd_config:
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin prohibit-password
Keep your current session open, then check the syntax and reload from a second terminal:
sudo sshd -t
sudo systemctl reload ssh
The service is called ssh on Debian and Ubuntu and sshd on Fedora and RHEL. According to the sshd_config(5) manual, both PasswordAuthentication and KbdInteractiveAuthentication default to yes, so you have to turn both off. PermitRootLogin already defaults to prohibit-password in current OpenSSH, but distributions sometimes override it. Other relevant defaults from the same manual:
| Setting | Default | What it does |
|---|---|---|
MaxAuthTries | 6 | Authentication attempts allowed per connection |
LoginGraceTime | 120 seconds | Disconnects clients that haven't logged in by then |
MaxStartups | 10:30:100 | Randomly drops new unauthenticated connections once 10 are pending, refusing all at 100 |
PerSourcePenalties | Enabled | Penalises client addresses whose behaviour looks like an attack |
Older OpenSSH versions don't have every one of these settings. Run ssh -V to see your version, and sudo sshd -T | grep -i penalties to see what your server actually uses.
How to tell an attack is happening
Log every failed login with the account name, source address and time, and alert on patterns rather than single failures: many failures for one account (brute force), one or two failures across many accounts from related addresses (spraying), or a jump in successful logins from new locations (credential stuffing that worked). On Linux, failed SSH logins appear as Failed password for lines in journalctl -u ssh, or in /var/log/auth.log on Debian and Ubuntu. If you see them on a server where passwords are disabled, they are harmless noise.
Frequently asked questions
- What is the difference between a brute force attack and credential stuffing?
- A brute force attack guesses many passwords for one account, usually from a dictionary of common passwords. Credential stuffing tries username and password pairs leaked from another site's breach, betting that people reuse passwords. Rate limits help against both, but only unique passwords and multi-factor authentication stop credential stuffing reliably.
- How many failed login attempts should lock an account?
- NIST SP 800-63B-4 sets an upper bound: no more than 100 consecutive failed attempts on one authenticator before it is disabled, and lower limits are allowed. Many services combine a lower threshold with growing wait times. OWASP warns that hard lockouts can be abused to lock real users out, so give locked users a way back in, such as the password reset flow.
- Does a CAPTCHA stop brute force attacks?
- Not on its own. OWASP describes CAPTCHA as a defence-in-depth control and suggests showing it only after a few failed attempts, so that it slows automated guessing without annoying every user. Rate limiting, lockout rules and multi-factor authentication do the real work.
- Is my SSH server being brute forced?
- If it accepts connections from the internet on port 22, almost certainly someone is trying. Check the authentication log (journalctl -u ssh or /var/log/auth.log on Debian and Ubuntu) for repeated 'Failed password' lines. Switching to key-only authentication with PasswordAuthentication no makes those guesses useless.