Table of Contents
What you will read?
Spam emails can flood your mail server, consume resources, and damage your domain reputation. Attackers use spam to distribute malware, phishing attempts, and fraudulent messages. Deploying an anti-spam filter ensures that only legitimate emails reach your inbox.
Install SpamAssassin
SpamAssassin is a widely used open-source spam filter that scans emails for spam characteristics.
sudo apt update
sudo apt install spamassassin
sudo systemctl enable spamassassin
sudo systemctl start spamassassin
Enable SpamAssassin for Postfix:
sudo nano /etc/postfix/master.cf
Add the following:
spamassassin unix - n n - - pipe
user=spamd argv=/usr/bin/spamc -f -e /usr/sbin/sendmail -oi -f ${sender} -- ${recipient}
Restart Postfix:
sudo systemctl restart postfix
Configure SpamAssassin Rules
Edit the configuration file to adjust spam detection thresholds:
sudo nano /etc/spamassassin/local.cf
Example settings:
required_score 5.0
rewrite_header Subject ***SPAM***
use_bayes 1
bayes_auto_learn 1
Restart SpamAssassin:
sudo systemctl restart spamassassin
Enable Postscreen in Postfix
Postscreen helps block spam bots before they connect. Edit the Postfix configuration file:
sudo nano /etc/postfix/main.cf
add:
postscreen_enable = yes
postscreen_greet_action = enforce
postscreen_dnsbl_sites = zen.spamhaus.org bl.spamcop.net b.barracudacentral.org
Restart Postfix:
sudo systemctl restart postfix
Use Rspamd for Advanced Filtering
Rspamd is a modern spam filtering system with better performance than SpamAssassin.
sudo apt install rspamd
Enable integration with Postfix:
sudo nano /etc/postfix/master.cf
Add:
rspamd unix - n n - - pipe
user=_rspamd argv=/usr/bin/rspamc -f -e /usr/sbin/sendmail -oi -f ${sender} -- ${recipient}
Restart Postfix:
sudo systemctl restart postfix
Enable Greylisting with Postgrey
Postgrey temporarily rejects unknown senders to filter out spam bots.
sudo apt install postgrey
sudo systemctl enable postgrey
sudo systemctl start postgrey
Edit Postfix configuration:
sudo nano /etc/postfix/main.cf
add:
smtpd_recipient_restrictions = permit_mynetworks,
permit_sasl_authenticated,
reject_unauth_destination,
check_policy_service inet:127.0.0.1:10023
Restart Postfix:
sudo systemctl restart postfix
Use DNS Blacklists (DNSBLs)
Blocking known spam sources using DNSBLs improves email filtering. Edit Postfix configuration:
sudo nano /etc/postfix/main.cf
add:
smtpd_recipient_restrictions = reject_rbl_client zen.spamhaus.org,
reject_rbl_client bl.spamcop.net,
reject_rbl_client b.barracudacentral.org
Restart Postfix:
sudo systemctl restart postfix
Monitor and Adjust Filters
Regularly check logs for spam detection performance:
tail -f /var/log/mail.log
tail -f /var/log/syslog
Adjust filter thresholds and blocklists based on spam trends.
