Linux Spam Filter With SpamAsassin

Are you tired of spam in your inbox? If the answer is yes, you can reduce spam by installing SpamAssassin on your mail server. Or you can show this post to your network/web server administrator to install SpamAssassin at your mail server.

Here we will use SpamAssassin with Postfix to handle email spam filtering. Postfix is a widely used mail transport agent (MTA) used on many popular Unix/Linux systems. SpamAssassin is an Apache project for filtering email. Below are the steps to install and configure SpamAssassin; these steps are for Ubuntu/Debian — for other OS please refer to the SpamAssassin official site.

How to install SpamAssassin

1. Install SpamAssassin and spamc with this command:

sudo apt-get install spamassassin spamc

2. Create a specific user for SpamAssassin with this command:

groupadd -g 5001 spamd
useradd -u 5001 -g spamd -s /sbin/nologin -d /var/lib/spamassassin spamd
mkdir /var/lib/spamassassin
chown spamd:spamd /var/lib/spamassassin

3. Configure settings in SpamAssassin — open /etc/default/spamassassin and make the settings like below:

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

4. Configure SpamAssassin rules — open /etc/spamassassin/local.cf and make the settings like below:

rewrite_header Subject [***** SPAM _SCORE_ *****]
required_score           2.0
#to be able to use _SCORE_ we need report_safe set to 0
#If this option is set to 0, incoming spam is only modified by adding some "X-Spam-" headers and no changes will be made to the body.
report_safe     0

# Enable the Bayes system
use_bayes               1
use_bayes_rules         1
# Enable Bayes auto-learning
bayes_auto_learn        1

# Enable or disable network checks
skip_rbl_checks         0
use_razor2              0
use_dcc                 0
use_pyzor               0

5. Start SpamAssassin with this command:

sudo/etc/init.d/spamassassin start

6. Now we need to tell Postfix to use SpamAssassin. Edit /etc/postfix/master.cf and change the line:

smtp      inet  n       -       -       -       -       smtpd

to

smtp      inet  n       -       -       -       -       smtpd -o content_filter=spamassassin

Then add this:

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

7. Reload Postfix with this command:

sudo /etc/init.d/postfix reload

That’s it. Hope this post can help you reduce spam in your inbox. Have a nice day…

Disclaimer: Use these instructions at your own risk. I am not an expert on spam filtering: this set of instructions just gave me what I needed to do my own local spam filtering. If you set it up and it loses you the email clinching a one million dollar contract, I assume no responsibility.