Tutorial

How To Install and Setup Spamassassin on Ubuntu 12.04

Published on October 25, 2013
Default avatar

By Miyuru Sankalpa

DevOps Engineer

How To Install and Setup Spamassassin on Ubuntu 12.04

Status: Deprecated

This article covers a version of Ubuntu that is no longer supported. If you are currently operate a server running Ubuntu 12.04, we highly recommend upgrading or migrating to a supported version of Ubuntu:

Reason: Ubuntu 12.04 reached end of life (EOL) on April 28, 2017 and no longer receives security patches or updates. This guide is no longer maintained.

See Instead:
This guide might still be useful as a reference, but may not work on other Ubuntu releases. If available, we strongly recommend using a guide written for the version of Ubuntu you are using. You can use the search functionality at the top of the page to find a more recent version.

About Spamassassin

Spamassassin is a free and open-source mail filter written in Perl that is used to identify spam using a wide range of heuristic tests on mail headers and body text. It will save your mailbox from much unwanted spam emails.

Prerequisites

Before installing Spamassassin, you need to install and setup a mail transfer agent such as Postfix on your virtual private server.

You can find instructions on that here

Install Spamassassin

Use apt-get to install Spamassassin and spamc.

apt-get install spamassassin spamc

Once Spamassassin is installed, there are a few steps that has to be taken to make it fully functional.

Adding Spamassassin User

To run Spamassassin you need to create a new user on your VPS.

First add the group spams:

groupadd spamd

then add the user spamd with the home directory /var/log/spamassassin:

useradd -g spamd -s /bin/false -d /var/log/spamassassin spamd

then create the directory /var/log/spamassassin:

mkdir /var/log/spamassassin

and change the ownership of the directory to spams:

chown spamd:spamd /var/log/spamassassin

Let's set up Spamassassin now.

Setting Up Spamassassin

Open the spamassassin config file using:

nano /etc/default/spamassassin

To enable Spamassassin find the line

ENABLED=0
and change it to
ENABLED=1

To enable automatic rule updates in order to get the latest spam filtering rules find the line

CRON=0
and change it to
CRON=1

Now create a variable named SAHOME with the Spamassassin home directory:

SAHOME="/var/log/spamassassin/"

Find and change the OPTIONS variable to

OPTIONS="--create-prefs --max-children 2 --username spamd \
-H ${SAHOME} -s ${SAHOME}spamd.log"

This specifies the username Spamassassin will run under as spamd, as well as add the home directory, create the log file, and limit the child processes that Spamassassin can run.

If you have a busy server, feel free to increase the max-children value.

Start the Spamassassin daemon by using the following code:

service spamassassin start

Now, let's config Postfix.

Configuring Postfix

The emails still do not go through Spamassasin. To do that, open Postfix config file using:

nano /etc/postfix/master.cf

Find the the line

smtp      inet  n       -       -       -       -       smtpd

and add the following

-o content_filter=spamassassin

Now, Postfix will pipe the mail through Spamassassin.

To setup after-queue content filter add the following line 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}

For the changes to take effect restart postfix:

service postfix restart

Now postfix will use spamassassin as a spam filter.

Configuring Spamassassin on your VPS

To get the maximum use of Spamassassin you have to create rules.

Open the Spamassassin default rules file using:

nano /etc/spamassassin/local.cf

To activate a rules uncomment line remove the # symbol.

To add a spam header to spam mail uncomment or add the line:

rewrite_header Subject [***** SPAM _SCORE_ *****]

Spamassassin gives a score to each mail after running different tests on it. The following line will mark the mail as spam if the score is more than the value specified in the rule.

required_score           3.0

To use bayes theorem to check mails, uncomment or add the line:

use_bayes               1

To enable bayes auto learning, uncomment or add the line:

bayes_auto_learn        1

After adding the above details, save the file and restart spam assassin.

service spamassassin restart

Testing

To see if Spamassassin is working, you can check the spamassassin log file using:

nano /var/log/spamassassin/spamd.log

or send the email from an external server and check the mail headers.

Conclusion

Using Spamassassin, it is very easy to protect your mailbox from spammers. The best thing about Spamassassin is that we can create rules by ourselves and manage it. If you have a mail server, then you must also have Spamassassin!

Submitted by: Miyuru Sankalpa

About the authors
Default avatar

DevOps Engineer

DevOps Engineer & Networking Student

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
10 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

i follwed this and worked until i cant seem to receive email i can send them just cant receive i have ubuntu 18.04

I am using sendmail in ubuntu 14.04.

I did steps up-to “Setting Up Spamassassin”. Then There configuration according to postfix is given, Then how can i edit the ‘Sendmail’.

Can you give proper installation and configuration of Spamassassin for Sendmail.

Great tutorial, but what’s the point to setup SpamAssassin when after that all emails still arrive into the inbox folder?

During the past month I started receiving about 400 to 500 spam mails per day which makes me very nervous and is disturbing my work. I followed the instructions in this article, installed and configured SpamAssassin.

The result - the word SPAM is added to the spam emails (as well as spam headers are added), but they all still arrive into the Inbox folder of my Outlook.

I looked for a solution, but can’t find a good tutorial how to do this.

Any help is greatly appreciated!

p.s. My MDA is dovecot

For some reason the SPAMD_HOME variable wasn’t being used and spamassassin wont work, when i started spamassassin service. Here is what the process looked like when i run a ps:

root      8302     1  8 00:27 ?        00:00:00 /usr/sbin/spamd -d --pidfile=/var/run/spamassassin.pid --create-prefs --max-children 5 --username spamd --helper-home-dir ${SPAMD_HOME} -s ${SPAMD_HOME}spamd.log

I had to hard code the path to the home directory. Any idea what might be missing? Here is my /etc/default/spamassassin file:

# /etc/default/spamassassin
# Duncan Findlay

# WARNING: please read README.spamd before using.
# There may be security risks.
# If you're using systemd (default for jessie), the ENABLED setting is
# not used. Instead, enable spamd by issuing:
# systemctl enable spamassassin.service
# Change to "1" to enable spamd on systems using sysvinit:
ENABLED=1

# Options
# See man spamd for possible options. The -d option is automatically added.

# SpamAssassin uses a preforking model, so be careful! You need to
# make sure --max-children is not set to anything higher than 5,
# unless you know what you're doing.

#OPTIONS="--create-prefs --max-children 5 --helper-home-dir"
SPAMD_HOME="/home/spamd"
#OPTIONS="--create-prefs --max-children 5 --username spamd --helper-home-dir ${SPAMD_HOME} -s ${SPAMD_HOME}/spamd.log"
OPTIONS="--create-prefs --max-children 5 --username spamd --helper-home-dir /home/spamd -s /home/spamd/spamd.log"

# Pid file
# Where should spamd write its PID to file? If you use the -u or
# --username option above, this needs to be writable by that user.
# Otherwise, the init script will not be able to shut spamd down.
#PIDFILE="/var/run/spamd.pid"
#PIDFILE="${SPAMD_HOME}/spamd.pid"
PIDFILE="/home/spamd/spamd.pid"
# Set nice level of spamd
#NICE="--nicelevel 15"

# Cronjob
# Set to anything but 0 to enable the cron job to automatically update
# spamassassin's rules on a nightly basis
CRON=1

This guide works on Ubuntu 14.04 too. But I had to do the following:

This bit:

-o content_filter=spamassassin

didn’t work when I pasted it into /etc/postfix/master.cf, when I tried to restart Postfix it said

/usr/sbin/postconf: fatal: file /etc/postfix/master.cf: line 17: bad field count

I got it to work by adding two spaces at the beginning of the line, so it’s:

  -o content_filter=spamassassin

and then Postfix restarted fine.

Thanks for the guide Miyuru!

Thanks for your tutorial. I have a question, Is it possible to use this server as a remote spam assassin server for another mail servers? Thanks.

What about CentOS users?

Hi,

Thanks very much for the guide.

Is there any reason why messages are piped to /usr/sbin/sendmail with ${recipient} instead of ${original_recipient}? Using $recipient, the X-Original-To header is overwritten by sendmail which means the name of the original recipient cannot be ascertained by any post-processing scripts.

Very good information. But i reinstalled centos on my server and if someone needs to configure spamassasin for centos, this article should help him.

Thank you DO for this tutorial btw.

This comment has been deleted

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Get our biweekly newsletter

    Sign up for Infrastructure as a Newsletter.

    Hollie's Hub for Good

    Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

    Become a contributor

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    Welcome to the developer cloud

    DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

    Learn more
    DigitalOcean Cloud Control Panel