How to protect SSH with Fail2ban on a Linux VPS
Every server with SSH open to the internet receives automated login attempts all day. Fail2ban watches the logs and temporarily blocks IP addresses that fail to log in too many times. This guide shows how to harden SSH with Fail2ban on an AlmaLinux VPS, plus a few SSH settings that make brute-force attacks useless.
Step 1: Install Fail2ban
Fail2ban is in the EPEL repository. On AlmaLinux it works with firewalld out of the box:
sudo dnf install -y epel-release
sudo dnf install -y fail2ban
Step 2: Create your configuration
Do not edit jail.conf, because updates can overwrite it. Create /etc/fail2ban/jail.local instead:
[DEFAULT]
# your own static IP, so you never ban yourself
ignoreip = 127.0.0.1/8 ::1 YOUR_STATIC_IP
bantime = 1h
findtime = 10m
maxretry = 5
[sshd]
enabled = true
# if you changed the SSH port, write it here, e.g. port = 2222
port = ssh
- bantime: how long an IP stays blocked.
- findtime and maxretry: an IP is banned after
maxretryfailures withinfindtime. - ignoreip: addresses that are never banned. Add your office IP if it is static.
Step 3: Start Fail2ban and check it
sudo systemctl enable --now fail2ban
sudo fail2ban-client status
sudo fail2ban-client status sshd
The sshd status shows how many failures were detected and which IPs are currently banned.
Step 4: Unban an IP
If you or a colleague are blocked by mistake, log in from another IP (or the provider's console) and run:
sudo fail2ban-client set sshd unbanip 203.0.113.10
Step 5: Harden SSH itself
Fail2ban slows attackers down, but SSH keys stop password guessing completely. After you have added your SSH key and tested that key login works, set:
# /etc/ssh/sshd_config (or a file in /etc/ssh/sshd_config.d/)
PermitRootLogin prohibit-password
PasswordAuthentication no
MaxAuthTries 3
Test the config and reload SSH. Keep your current session open until you confirm you can log in from a new window:
sudo sshd -t && sudo systemctl reload sshd
If you change the SSH port, allow the new port in firewalld and SELinux (sudo semanage port -a -t ssh_port_t -p tcp 2222) before reloading, and update port in jail.local. For more hardening steps, see How to secure your Linux server.
Common problems
- Fail2ban does not start: run
sudo fail2ban-client -tto test the configuration and show the error. If it cannot find the SSH log file, addbackend = systemdunder[sshd]. - No IPs are ever banned: check that the jail is enabled with
fail2ban-client statusand thatportmatches your real SSH port. - You banned yourself: use the VNC/console access from the client area or ask support, then unban your IP and add it to
ignoreip.
Frequently asked questions
Can Fail2ban protect other services?
Yes. It ships with filters for Nginx, Apache, Postfix, Dovecot and more. Enable each jail you need in jail.local.
Should I ban IPs permanently?
Usually not. Attack IPs change often, and permanent bans can block a legitimate user who shares an address. A ban of one hour or one day is a good balance.