Notification is an essential part of setting up Fail2ban and setting up email notifications in Fail2ban requires a mail server to send emails.
I've tried to configure sendmail (following this tutorial) to send an email with smtp port but it requires many steps to be performed to start using it. And then I found out about sSMTP, it works but Archwiki said:
sSMTP is unmaintained. Consider using something like msmtp or OpenSMTPD instead. [Archwiki/ssmtp]
So I decided to use msmtp to make my ubuntu server send email easily.
Step 1: Installation of msmtp
I use Ubuntu (a Debian-based system), install msmtp with the following command:
sudo apt-get install msmtp msmtp-mta
The configuration file is as follows:
sudo nano /etc/msmtprc
and then use the following settings for smtp account:
Replace with your smtp server configuration. For example:
mxroutewith your email provider's name
taylor.mxrouting.netto your server smtp hostname
mydomain.comin the domain section to your domain name.
- msmtp log will be created on
Save the file, and change permission with
chmod 600/etc/msmtprc because the file contains the user and password in plain text.
Chmod 600 means that
(U)ser / owner can read, can write and can't execute.
(G)roup can't read, can't write and can't execute.
(O)thers can't read, can't write and can't execute.
next step is to configure Fail2ban
Step 2: Configure Fail2ban
Create a new action.d with the name
and add the following code below:
This fail2ban action will send an email notification whenever IPs get banned or unbanned.
add the following in the
[DEFAULT] section, if you want to send email notifications to all jails:
[DEFAULT] ... mta = msmtp action = %(action_mw)s[firstname.lastname@example.org, email@example.com, firstname.lastname@example.org, sendername=Fail2Ban]
- From and Sender: The email account it sends from
- Destination: Where you want to send the notification.
- Sendername: Name of the sender
But if you want to send email notifications in only one specific jail, add
action to the
Step 3: Let's try it out
I use this
cf-wplogin jail from Fail2ban with Cloudflare tutorial to test.
Fail2ban successfully sends email notifications, and the email result will look like this:
-problem: At this time I posted (20/03/2023) Gmail not receiving any my msmtp email, I am still this figuring out
If you guys are interested more about configuring fail2ban notifications, you may read fail2ban notifications to pushover, telegram, or even to discord!
Thank you for reading!