Knowledgebase

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.

cPanel/WHM servers: do not install Fail2ban on a cPanel server. WHM already includes cPHulk Brute Force Protection, and EgWeb servers also use Imunify360. Running several blocking tools together can lock out real users. This guide is for VPS servers without a control panel.

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 maxretry failures within findtime.
  • 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 -t to test the configuration and show the error. If it cannot find the SSH log file, add backend = systemd under [sshd].
  • No IPs are ever banned: check that the jail is enabled with fail2ban-client status and that port matches 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.

Need help? Open a support ticket or message us on WhatsApp +20 127 113 3655.

  • Fail2ban, SSH, security, brute force, VPS, AlmaLinux

Was this answer helpful?

0 Users Found This Useful