How to stop SSH brute-force attacks on a Linux server

Any server with a public IP address and port 22 open will see SSH login attempts within minutes of going online. Most come from automated scanners that work through lists of common usernames and leaked passwords. You can't stop them knocking, but you can make every attempt fail and keep the noise away from your logs and CPU.
1. Use keys, then switch passwords off
Key-based authentication makes password guessing irrelevant. Create a key on your own computer (ssh-keygen -t ed25519), copy it to the server (ssh-copy-id user@server), confirm you can log in with it, and only then turn password login off in /etc/ssh/sshd_config:
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin prohibit-password
PubkeyAuthentication yes
Reload with systemctl reload sshd (or ssh on Debian and Ubuntu) and keep your current session open until you've tested a new one.
2. Limit who can log in at all
AllowUsers deploy admin(orAllowGroups) means every other account is refused, even with the right password.MaxAuthTries 3andLoginGraceTime 30shorten each attacker's window.- If only your office or VPN needs SSH, allow port 22 from those addresses only in your firewall.
3. Block repeat offenders automatically
Even with keys, failed attempts fill logs and use resources. A log-watching blocker reads /var/log/auth.log or /var/log/secure as lines are written and bans an address after a handful of failures. Good blockers:
- escalate: each repeat offence earns a longer ban;
- never block you: your own addresses and VPN ranges are allowlisted;
- block early in the network stack (ipset, nftables sets or XDP) so a banned address costs almost nothing.
TurnUpSecurity Shield does this for SSH and for cPanel, webmail, FTP, IMAP/POP3 and SMTP logins, and shares proven offenders with your other servers.
4. What helps less than people think
- Changing the port cuts log noise from lazy scanners but isn't security; port scans find it quickly.
- Complex passwords alone still leave you exposed to leaked or reused credentials. Keys remove that risk.
5. Add a second factor where you can
For accounts that must keep passwords (shared hosting users, for example), a TOTP second factor through PAM, or a VPN in front of SSH, closes the gap.
Quick checklist
- Keys for every admin, passwords off.
PermitRootLogin prohibit-password(orno).AllowUsersorAllowGroups.- Automatic, escalating blocks with your own IPs allowlisted.
- Review
lastandlastbnow and then for logins you don't recognise.