To stop SSH brute force attacks, do two things. Make passwords useless by allowing only key-based logins, with no password login for root. Then use fail2ban (or CrowdSec) to ban the addresses that keep trying. Changing the SSH port reduces log noise, but it is not a security control on its own.
Any server with port 22 open to the internet gets login attempts within minutes of going online. Most come from automated bots trying common usernames and leaked passwords. The steps below take about fifteen minutes and work on Debian, Ubuntu and the RHEL family (RHEL, Rocky Linux, AlmaLinux).
Step 1: Switch to SSH key authentication
A brute force attack only works if a password can be guessed. Once you allow only keys, the attempts are just noise.
On your local machine, create a key if you don’t have one, then copy it to the server:
ssh-keygen -t ed25519 -C "you@laptop"
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@your-server
Open a new terminal and check that ssh user@your-server logs you in without asking for the account password. Do not go on to the next step until this works.
Step 2: Disable password login and root password login
Recent releases (Debian 12, Ubuntu 22.04 and later, RHEL 9) read drop-in files from /etc/ssh/sshd_config.d/. For each setting, sshd uses the first value it reads. Cloud images often ship a file such as 50-cloud-init.conf that turns PasswordAuthentication back on, so give your file a name that sorts earlier:
sudo tee /etc/ssh/sshd_config.d/00-hardening.conf >/dev/null <<'EOF'
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
PermitRootLogin prohibit-password
MaxAuthTries 3
EOF
Use PermitRootLogin no if you never log in as root. Keep prohibit-password if a tool such as Coolify connects as root with a key. On RHEL 8, check that /etc/ssh/sshd_config has an Include /etc/ssh/sshd_config.d/*.conf line near the top. If it doesn’t, put these settings in the main file instead.
Check the syntax and the settings sshd will actually use, then reload:
sudo sshd -t
sudo sshd -T | grep -Ei '^(passwordauthentication|kbdinteractiveauthentication|permitrootlogin)'
# Debian / Ubuntu
sudo systemctl reload ssh
# RHEL / Rocky / AlmaLinux
sudo systemctl reload sshd
Keep your current session open and test a new login before you close it. The OpenSSH manual describes every option: sshd_config(5).
Step 3: Install fail2ban
Even with keys only, bots fill your logs and use resources. fail2ban reads the authentication log and adds a firewall rule against any IP that fails too often.
Debian / Ubuntu:
sudo apt update
sudo apt install fail2ban python3-systemd
RHEL / Rocky / AlmaLinux (fail2ban comes from EPEL):
sudo dnf install epel-release # Rocky / AlmaLinux
sudo dnf install fail2ban
sudo systemctl enable --now fail2ban
On RHEL itself, see the EPEL documentation for how to enable the repository.
Step 4: Configure the sshd jail in jail.local
Never edit jail.conf, because package updates overwrite it. Put your settings in /etc/fail2ban/jail.local:
sudo tee /etc/fail2ban/jail.local >/dev/null <<'EOF'
[DEFAULT]
# Your own static IPs: never ban them
ignoreip = 127.0.0.1/8 ::1 203.0.113.10
bantime = 1h
findtime = 10m
maxretry = 5
# Ban repeat offenders for longer each time
bantime.increment = true
[sshd]
enabled = true
port = ssh
backend = systemd
EOF
Here is what each setting does:
- maxretry: how many failures are allowed within
findtimebefore a ban. Values from 3 to 5 are typical. - findtime: how far back fail2ban counts failures.
- bantime: how long the ban lasts. Bans of a few minutes only slow bots down. One hour, increasing for repeat offenders, works well.
- ignoreip: your office or VPN addresses, so you never lock yourself out.
Why backend = systemd matters on Debian 12
Debian 12 no longer installs rsyslog by default, so /var/log/auth.log does not exist. A jail that watches that file finds nothing to read, and fail2ban may refuse to start. Setting backend = systemd makes fail2ban read sshd messages straight from the journal. This needs the python3-systemd module installed above. The setting also works on Ubuntu and on the RHEL family. If you prefer files, install rsyslog and leave the backend at its default.
Test the configuration and restart:
sudo fail2ban-client -t
sudo systemctl restart fail2ban
sudo systemctl enable fail2ban
The fail2ban project documents every jail option in the comments of jail.conf.
Step 5: Verify that bans actually happen
A fail2ban process that is running is not proof that it works. Check the jail:
sudo fail2ban-client status sshd
The output shows Currently failed, Total failed, Currently banned and the Banned IP list. If “Total failed” stays at 0 while the journal is full of failed logins, the jail is not reading the right log source. Compare with:
sudo journalctl _COMM=sshd --since "1 hour ago" | grep -Ei 'failed|invalid' | tail
sudo journalctl -u fail2ban --since today
Other useful commands:
sudo fail2ban-client banned # all jails, all banned IPs
sudo fail2ban-client set sshd unbanip 198.51.100.7
sudo fail2ban-regex systemd-journal sshd # test the filter against the journal
Should you change the SSH port?
Moving SSH from port 22 to another port such as 2222 cuts the number of automated attempts a lot, because most bots only try the default port.
Pros: much smaller logs, fewer wasted CPU cycles, and failed logins that matter stand out more.
Cons: it does not stop a targeted attacker, because a port scan finds the new port in seconds. You also have to update firewall rules, scripts and every client’s ~/.ssh/config. On some systems there are extra steps:
- RHEL family with SELinux: allow the new port with
sudo semanage port -a -t ssh_port_t -p tcp 2222and open it in firewalld. - Ubuntu 22.10 and later: SSH uses socket activation, so after changing
Portrunsudo systemctl daemon-reloadandsudo systemctl restart ssh.socket.
Treat a port change as a way to reduce noise on top of keys and fail2ban, never as a replacement. Keep port = ssh in the jail in step with it (for example port = 2222).
Alternatives: CrowdSec and sshguard
CrowdSec splits the job in two. An agent detects attacks in your logs, and a bouncer enforces the decisions in the firewall. It can also use blocklists shared by the community. Install it from the official repositories, then add a firewall bouncer such as crowdsec-firewall-bouncer-iptables or crowdsec-firewall-bouncer-nftables. Without a bouncer, CrowdSec detects attacks but blocks nothing. List active decisions with:
sudo cscli decisions list
sshguard is a lighter tool that does one job: it watches authentication logs and blocks offenders. It is a reasonable choice on small machines.
Pick one tool per server. Running fail2ban and CrowdSec against the same log just adds firewall rules twice.
How Avanguard helps
- Avanguard detects SSH brute force attacks, including distributed ones, and shows failed logins and bans per hour.
- It checks that fail2ban (or CrowdSec with a bouncer, or sshguard) is installed and running, that the sshd jail is enabled and can read its log, that
maxretryis 10 or lower and that bans last at least 5 minutes. It alerts you when fail2ban is not stopping an attacker. - It reports SSH root login and password login as vulnerabilities and prepares the fix commands for your distribution. You run them yourself, because the agent never installs or changes anything.
See SSH brute force protection for details, or how Avanguard works.
Summary
- SSH key login works in a new session
-
PasswordAuthentication noandKbdInteractiveAuthentication noare in effect (check withsshd -T) - Root login is
noorprohibit-password - fail2ban is installed and enabled, with an
[sshd]jail injail.local -
backend = systemdon Debian 12 or any server without/var/log/auth.log - Sensible
maxretry,findtime,bantime, and your own IPs inignoreip -
fail2ban-client status sshdshows failures being counted and IPs being banned - Port change only as an extra step, with SELinux and firewall updated