Skip to content

How to configure Fail2ban to protect SSH and Apache

Last updated on March 13, 2019

Let’s assume that you already installed fail2ban, you can check here how to do that: –

We need to copy this to a file called jail.local for Fail2Ban to find it.

cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
Configure defaults in jail.local

Open up the new Fail2Ban configuration file:

You can see the default section below:


# "ignoreip" can be an IP address, a CIDR mask or a DNS host. Fail2ban will not
# ban a host which matches an address in this list. Several addresses can be
# defined using space separator.
ignoreip =

# Override /etc/fail2ban/jail.d/00-firewalld.conf:
banaction = iptables-multiport

# "bantime" is the number of seconds that a host is banned.
bantime  = 600

# A host is banned if it has generated "maxretry" during the last "findtime"
# seconds.
findtime  = 600

# "maxretry" is the number of failures before a host get banned.
maxretry = 5

Configure Fail2ban For ssh

Although you can add this parameters in the global jail.local file, it is a good practice to create seperate jail files for each of the services we want to protect with Fail2Ban.

So lets create a new jail for SSH with the vi editor.

vi /etc/fail2ban/jail.d/sshd.local

In the above file, add the following lines of code:

enabled = true
port = ssh
action = iptables-multiport
logpath = /var/log/secure
maxretry = 5
bantime = 600
Restart Fail2Ban

After making any changes to the Fail2Ban config, always be sure to restart Fail2Ban.

systemctl restart fail2ban

You can see the rules that fail2ban puts in effect within the IP table:

iptables -L -n
Check Fail2Ban Status

Use fail2ban-client command to query the overall status of the Fail2Ban jails.

fail2ban-client status

You can also query a specific jail status using the following command:

fail2ban-client status sshd

Configure Fail2ban For Apache

Edit this file:

sudo nano /etc/fail2ban/jail.local

Add the following content. Note: Substitute your own static IP address for the sample address ( in this example:

# detect password authentication failures
enabled  = true
filter   = apache-auth
action   = iptables-multiport[name=auth, port="http,https"]
logpath  = /var/log/httpd/fail2ban_log
bantime  = 3600
maxretry = 3
ignoreip =

# detect spammer robots crawling email addresses
enabled  = true
filter   = apache-badbots
action   = iptables-multiport[name=badbots, port="http,https"]
logpath  = /var/log/httpd/fail2ban_log
bantime  = 3600
maxretry = 1
ignoreip =

# detect potential search for exploits
enabled  = true
filter   = apache-noscript
action   = iptables-multiport[name=noscript, port="http,https"]
logpath  = /var/log/httpd/fail2ban_log
bantime  = 3600
maxretry = 6
ignoreip =

# detect Apache overflow attempts
enabled  = true
filter   = apache-overflows
action   = iptables-multiport[name=overflows, port="http,https"]
logpath  = /var/log/httpd/fail2ban_log
bantime  = 3600
maxretry = 2
ignoreip =

Save and close the file, then restart Fail2ban for the changes to take effect:

sudo systemctl restart fail2ban

Now, configure the Fail2ban service to start on boot with the command:

sudo systemctl enable fail2ban

To verify the rules that were added to iptables by Fail2ban, use the following command:

sudo iptables -L
Published inLinuxShell