How to install and set up a well-known e-mail filtering program, spam-assassin on Ubuntu Server 20.04

Louis SanchezAugust 14th 2021, 4:16

Follow these steps to filter mail with spam-assassin

Step 1 : Install spam-assassin with the following command:

sudo apt-get update
sudo apt-get install spamassassin spamc

Step 2 : Create a user account and group for spam-assassin

sudo groupadd spamd
sudo useradd -g spamd -s /usr/sbin/nologin -d /var/log/spamassassin -m spamd

Step 3 : Change the default settings for the spam daemon. Open /etc/default/spamassassin and update the following lines:

ENABLED=1
SAHOME="/var/log/spamassassin/"
OPTIONS="--create-prefs --max-children 5 --username spamd --helper-home-dir ${SAHOME} -s ${SAHOME}spamd.log"
PIDFILE="${SAHOME}spamd.pid"
CRON=1

Step 4 : Optionally, configure spam rules by changing values in /etc/spamassassin/local.cf. All lines in this file are commented out by default. You can read the file to see what each rule does.

sudo nano /etc/spamassassin/local.cf

Step 5 : Next, we need to change the Postfix settings to pass e-mails through spam-assassin. Open /etc/postfix/master.cf

sudo nano /etc/postfix/master.cf
and find the following line
smtp      inet  n       -       y       -       -       smtpd
And add the following option to it.
-o content_filter=spamassassin

Define the content filter block by adding the following lines to the end of the file:

spamassassin unix -     n       n       -       -       pipe
    user=spamd argv=/usr/bin/spamc -f -e
    /usr/sbin/sendmail -oi -f ${sender} ${recipient}

Step 6 : Finally, restart spam-assassin and Postfix

sudo service spamassassin start
sudo service postfix reload

Step 7 : You can check spam-assassin and mail logs to verify that spam-assassin is working properly:

less /var/log/spamassassin/spamd.log
less /var/log/mail.log