↓ Skip to main content
Self Hosting Part 6

Fail2Ban Configuration

Adding longer and longer ban times for repeat attackers to the Fail2Ban that comes with iRedMail.

Series map 13/23
Self Hosting
  1. 0 Planning and Architecture
  2. 1 Post-Install Configuration
  3. 2 Mail Server Setup
    1. 2.1 Setting Up SSL for the Mail Server
    2. 2.2 Adding a New Domain to the Mail Server
    3. 2.3 Using a Mail Client
    4. 2.4 Moving the Mail Server
    5. 2.5 Extra Settings
  4. 3 NGINX Configuration
  5. 4 Docker Setup
    1. 4.1 A Ready Template for Docker Containers
  6. 5 Firewalld Configuration
  7. 6 Fail2Ban Configuration
  8. 7 WireGuard Setup
    1. 7.1 Adding WireGuard Clients
    2. 7.2 Putting Services Behind the VPN
    3. 7.3 Possible Problems
  9. 8 Vault Setup
  10. 9 Vaultwarden Setup
  11. 10 WordPress Setup
    1. 10.1 Moving an Old WordPress Site into This Setup
  12. 11 Usememos Setup
  13. 12 Syncthing Setup
On this page
Requirements

Assumptions

  • The VPN networks are 10.8.0.0/24 and fd42:8::/64 from 7 WireGuard Setup; these networks are never banned.
  • Commands are run on the server with root rights (sudo).

Fail2Ban was installed together with iRedMail. Let’s add a few useful settings on top of it.

Longer Bans for Repeat Offenders
#

Let’s create the file /etc/fail2ban/jail.d/99-local.local with the content below.

Note

Fail2Ban reads the files in this order: jail.conf → jail.d/*.conf → jail.local → jail.d/*.local (alphabetical). 99-local.local is read last, so its [DEFAULT] settings override the [DEFAULT] settings in the earlier files. But settings defined in jail sections (for example [sshd]) win over [DEFAULT]; this file doesn’t change them.

/etc/fail2ban/jail.d/99-local.localini
[DEFAULT]
bantime.increment = true
bantime.factor    = 2
bantime.maxtime   = 1w
ignoreip = 127.0.0.1/8 ::1 10.8.0.0/24 fd42:8::/64
Note

ignoreip: Only localhost and the VPN network are never banned. We can’t leave out localhost; Roundcube and iRedAdmin connect to Dovecot and Postfix from 127.0.0.1. The VPN network is never banned because after 7.2 Putting Services Behind the VPN SSH is reachable only over the VPN; if our VPN address gets banned, we lose access to the server.

Note

We leave the Docker network (172.20.0.0/16) out of ignoreip on purpose. Containers connect to the ports on the host from 172.20.x.x addresses; if a compromised container tries mail passwords, it should be banned. The ban only affects that container. A container with a wrong SMTP password can get banned too; then fixing the password and lifting the ban with sudo fail2ban-client unban <IP> is enough.

fail2ban.conf has dbpurgeage = 1d, so Fail2Ban remembers an IP in its database for at most 1 day. The longer bans work by reading an IP’s earlier bans from this database, so dbpurgeage must not be shorter than bantime.maxtime. Otherwise bans older than 1 day are forgotten and the ban time doesn’t grow.

Without changing fail2ban.conf, let’s create the file /etc/fail2ban/fail2ban.local with the content below (if the file already exists, add the dbpurgeage line to its [Definition] section):

/etc/fail2ban/fail2ban.localini
[Definition]
dbpurgeage = 1w

Apply and Check
#

bash
sudo fail2ban-client -t                  # OK: configuration test is successful
sudo systemctl restart fail2ban
sudo fail2ban-client get sshd bantime    # 3600
sudo fail2ban-client get dbpurgeage      # 604800 (1 week)