Fail2ban protects your Linux VPS by banning IP addresses that attempt unauthorized access. It scans your VPS log files (for example, /var/log/apache/error_log) to detect suspicious activity such as multiple failed login attempts or exploit scans.
You can also configure Fail2ban to automatically send reports to you and to the administrator of the attacking IP. It includes ready-made filters for various services such as Apache, Courier, SSH, Exim, Postfix, and others.
sudo dnf -y updatesudo dnf -y install epel-releasesudo dnf -y install fail2ban If you receive an error, run: sudo dnf -y update first.sudo systemctl enable fail2ban sudo systemctl start fail2bansudo apt -y update && sudo apt -y upgradesudo apt -y install fail2bansudo systemctl enable fail2ban sudo systemctl start fail2banBy default, Fail2ban uses the file /etc/fail2ban/jail.conf. However, updates may overwrite it, so it’s best to create a /etc/fail2ban/jail.local file instead. This file is never overwritten and its settings override jail.conf.
Include only the services that are actually running on your VPS (e.g., Exim or Postfix, not both).
sudo nano /etc/fail2ban/jail.local
Copy and edit this example configuration to suit your setup:
[DEFAULT]
# Ban for X amount of time
bantime = 604800
findtime = 3600
sender = fail2ban@example.com
destemail = admin@example.com
action = %(action_mwl)s
banaction = iptables-multiport
maxretry = 3
ignoreip = your.ip.address
[sshd]
enabled = true
port = ssh # replace ssh with your configured SSH port
[exim]
enabled = true
filter = exim
logpath = /var/log/exim/mainlog
# cPanel: use /var/log/exim_mainlog
# Debian: /var/log/exim4/mainlog
[postfix]
enabled = true
port = smtp,ssmtp
filter = postfix
failregex = \[<HOST>]: 535 Incorrect authentication data
logpath = /var/log/maillog
[dovecot]
enabled = true
port = pop3,pop3s,imap,imaps
filter = dovecot
logpath = /var/log/maillog
Edit sender, destemail, and ignoreip according to your needs, and enable only the services you actually use.
If you use Exim, extend the Exim filter:
sudo nano /etc/fail2ban/filter.d/exim.conf
In the [Definition] section, add:
\[<HOST>\]: 535 Incorrect authentication data
Save and exit (Ctrl + X → Y → Enter).
Finally, restart Fail2ban to apply your changes:
sudo systemctl restart fail2ban
To avoid permanently banning a legitimate user after a simple mistake, you can configure escalating bans: for example, one hour, then a day, then a week.
sudo nano /etc/fail2ban/filter.d/f2b-repeat.conf
[INCLUDES]
before = common.conf
[Definition]
failregex = (?i)\]\s+ban\s+<HOST>
ignoreregex = (?i)\[f2b-repeat.*\]\s+ban\s+<HOST>
jail.local, insert between [DEFAULT] and [sshd]:[DEFAULT]
# Ban for X amount of time
bantime = 3600
findtime = 86400
sender = fail2ban@example.com
destemail = admin@example.com
action = %(action_mwl)s
banaction = iptables-multiport
maxretry = 3
ignoreip = your.ip.address
[f2b-repeat2]
enabled = true
filter = f2b-repeat
bantime = 86400
findtime = 604800
logpath = /var/log/fail2ban.log
maxretry = 3
[f2b-repeat3]
enabled = true
filter = f2b-repeat
bantime = 604800
findtime = 2592000
logpath = /var/log/fail2ban.log
maxretry = 3
[f2b-repeat4]
enabled = true
filter = f2b-repeat
bantime = 2592000
findtime = 15552000
logpath = /var/log/fail2ban.log
maxretry = 3
sudo touch /var/log/fail2ban.log
sudo systemctl restart fail2ban
ls /etc/fail2ban/filter.d/sudo nano /etc/fail2ban/jail.local[apache-auth] enabled = true filter = apache-auth logpath = /var/log/apache2/error.logThe filter value corresponds to the file name in /etc/fail2ban/filter.d/ (without .conf), and logpath specifies the log file Fail2ban will monitor.
sudo fail2ban-client statussudo cat /var/log/fail2ban.logsudo systemctl status fail2bansudo systemctl restart fail2bansudo fail2ban-client set JAIL banip xxx.xxx.xxx.xxxsudo fail2ban-client set JAIL unbanip xxx.xxx.xxx.xxxsudo fail2ban-client statusThe installation and configuration of Fail2ban on your VPS is now complete! Your system is now protected against brute-force and unauthorized login attempts.
Contact our experts, they will be happy to help!
Contact us