Setup your own email server (MTA) on the cheap - part 7

All going well, this should be the last part of my posts about configuring Postfix as an MTA.

This configuration arose because I happened to be looking at my mail.log file while someone in the Ukraine was trying again and again to log in to my server. So, I installed Fail2Ban. Fail2Ban is a clever tool that watches the log files you configure and when suspicious activity is found, it uses iptables (a linux firewall) to block the source of traffic.

This tool is built for our setup so it’s relatively straight forward to setup:
1. Install Fail2Ban:
sudo apt-get install fail2ban 
2. In your favourite editor, create a file /etc/fail2ban/jail.local. This is where all the rules should be customised. Default rules are in jail.conf but these should be left (and will be updated in new versions) so customisation are all in your local file. This is the text I add to my jail.local file:

4. Finally, reload everything and you should be on your way
sudo service fail2ban restart

I’ve kept my configuration simple since smtp, smtps, and pop3s are the only services open to the public. If you have more, add configuration to enable these too. I also add the logpath because otherwise it looks for mail.warn which doesn't exist in my version.

That’s it. With this configuration my server has been working well, and by having SPF and DKIM we’re doing better than most email providers.

Big thanks to these sources:
https://importgeek.wordpress.com/2017/01/15/fail2ban-prevent-postfix-brute-force/

Setup Steps with links:
  1. Setup PostFix with email address forwarding
  2. Setup sending and use LetsEncrypt to secure the SMTP server 
  3. Setup SPAMAssassain so Gmail or Yahoo don’tblock us for passing on dodgy emails 
  4. Setup ClamAV to block viruses, these will get you blocked too 
  5. Setup DKIM using opendkim to check DKIM on incoming and sign our outgoing mail 
  6. Update sender addresses so SPF passes for forwarded messages 
  7. Use Fail2Ban to block brute force attempts on our server
Final notes:
After running my server for a few months, there are some tweaks I've made:
     1. I found the letsencrypt renewal logging hard to follow so I took more control by adding this line to my crontab:
0 0 1 * * /usr/bin/letsencrypt renew >> /var/log/letsencrypt/renew.log

   2. Freshclam had memory issues trying to update clamav yet ran fine manually. So I disabled the service and setup a cron job for this too:
sudo /etc/init.d/clamav-freshclam stop sudo update-rc.d clamav-freshclam disable 
   Add this to crontab: 

0 */6 * * * /usr/bin/freshclam >> /var/log/clamav/freshclam.log
   3. To tidy up the logs, correct the hostname by running:
hostnamectl set-hostname mail.mydomain.com

Comments

Popular posts from this blog

Sync iTunes with MythMusic

Using Homebridge and Broadlink RM Mini to automate

LetsEncrypt and AWS ELB Load Balancers