Setting Up SpamAssassin on Ubuntu 16.04
SpamAssassin is a powerful tool for e-mail spam filtering. It employs various techniques, including DNS-based and fuzzy-checksum-based spam detection, Bayesian filtering, external programs, blacklists, and online databases. This guide will walk you through installing and configuring SpamAssassin on Ubuntu 16.04, but it is applicable to other Debian and Ubuntu distributions as well.
Step 1: Update Your System
First, update your server to ensure all packages are current. Run:
apt-get update
Step 2: Install SpamAssassin
Install SpamAssassin and its companion tool spamc
:
apt-get install spamassassin spamc -y
During the installation, you might see a message about the kernel version. If you are using a stable kernel version like 2.6.32 for OpenVZ, you can safely ignore this and proceed by clicking OK.
Step 3: Add a User for SpamAssassin
To run SpamAssassin, you need to create a dedicated user. Follow these steps:
Add the spamd
group:
groupadd spamd
Add the spamd
user with the home directory /var/log/spamassassin
:
useradd -g spamd -s /bin/false -d /var/log/spamassassin spamd
Create the home directory for spamd
:
mkdir /var/log/spamassassin
Change the ownership of the directory to spamd
:
chown spamd:spamd /var/log/spamassassin
Step 4: Configure SpamAssassin
Open the SpamAssassin configuration file:
nano /etc/default/spamassassin
If nano
is not installed, you can install it with:
apt-get install nano
In the configuration file, set the following variables:
Create a new variable named SAHOME
with the SpamAssassin home directory:
SAHOME="/var/log/spamassassin/"
Update the OPTIONS
variable:
OPTIONS="--create-prefs --max-children 2 --username spamd \
-H ${SAHOME} -s ${SAHOME}spamd.log"
Start the SpamAssassin daemon:
service spamassassin start
Step 5: Configure Postfix
To integrate SpamAssassin with Postfix, edit the Postfix configuration file:
nano /etc/postfix/master.cf
Find the line:
smtp inet n - - - - smtpd
Add the following at the end of the line:
-o content_filter=spamassassin
Add the following lines at the end of the file to set up an after-queue content filter:
spamassassin unix - n n - - pipe
user=spamd argv=/usr/bin/spamc -f -e
/usr/sbin/sendmail -oi -f ${sender} ${recipient}
Restart Postfix for the changes to take effect:
service postfix restart
Step 6: Configure SpamAssassin Rules
To get the most out of SpamAssassin, create custom rules. Open the default rules file:
nano /etc/spamassassin/local.cf
Uncomment the following lines by removing the #
symbol:
rewrite_header Subject *****SPAM***** - To add a spam header to spam mail.
required_score 5.0 - Spamassassin gives a score to each mail after running different tests on it. This line marks the mail as spam if the score is more than the value specified in the rule.
use_bayes 1 - To use Bayes theorem to check mails.
bayes_auto_learn 1- To enable Bayes auto-learning.
Save the file and restart SpamAssassin:
service spamassassin restart
Step 7: Test SpamAssassin
To verify that SpamAssassin is working, check the log file:
nano /var/log/spamassassin/spamd.log
You can also send an email from an external server and check the mail headers.
Conclusion
With SpamAssassin, you can effectively protect your mailbox from spam. Its flexibility allows you to create and manage your own rules for optimal spam filtering.