How to Install SpamAssassin with Postfix on Ubuntu

Updated on September 29, 2021
How to Install SpamAssassin with Postfix on Ubuntu header image

SpamAssassin is an open-source mail filter that identifies spam emails using a wide range of heuristic tests. This guide explains how to install SpamAssassin on Ubuntu 20.04 LTS or 21.04.

Prerequisites

  • Deploy an Ubuntu cloud server instance at Vultr.
  • Install Postfix mail transfer agent.
  • Create a sudo user.
  • Verify the server has outbound port 25 open.

1. Install SpamAssassin

  1. Connect to your server with SSH.

  2. Install SpamAssassin.

     $ sudo apt-get update
     $ sudo apt-get install spamassassin spamc -y
  3. Add a SpamAssassin user and disable the login.

     $ sudo adduser spamd --disabled-login

2. Configure SpamAssassin

Assign the spamd user account to SpamAssassin and set the log and default home directories.

  1. Edit the configuration settings.

     $ sudo nano /etc/default/spamassassin
  2. Find the line:

     ENABLED=1

    Uncomment it by removing # and change the value from 1 to 0.

     ENABLED=0
  3. Find the line:

     OPTIONS="--create-prefs --max-children 5 --helper-home-dir"

    Change it to include the SpamAssassin user account and log files directory.

     OPTIONS="--create-prefs --max-children 5 --username spamd --helper-home-dir /home/spamd/ -s /home/spamd/spamd.log"
  4. Find the line:

     CRON=0

    Change the value from 0 to 1.

     CRON=1
  5. Save and close the file.

  6. Make a backup of the SpamAssassin local configuration file.

     $ sudo mv /etc/spamassassin/local.cf  /etc/spamassassin/local.cf.bk
  7. Create a new SpamAssassin local configuration file.

     $ sudo nano /etc/spamassassin/local.cf 
  8. Paste the information below to the file.

     rewrite_header Subject ***** SPAM _SCORE_ *****
     report_safe             0
     required_score          5.0
     use_bayes               1
     use_bayes_rules         1
     bayes_auto_learn        1
     skip_rbl_checks         0
     use_razor2              0
     use_dcc                 0
     use_pyzor               0
     ifplugin Mail::SpamAssassin::Plugin::Shortcircuit
     endif
  9. Save and close the file.

3. Configure Postfix

  1. Edit your Postfix configuration file.

     $ sudo nano /etc/postfix/master.cf
  2. Locate these entries.

     smtp      inet  n       -       y       -       -       smtpd
     #smtp      inet  n       -       y       -       1       postscreen
     #smtpd     pass  -       -       y       -       -       smtpd
     #dnsblog   unix  -       -       y       -       0       dnsblog
     #tlsproxy  unix  -       -       y       -       0       tlsproxy
     submission inet n       -       y       -       -       smtpd
  3. Below the smtp configuration, add a SpamAssassin content filter.

     smtp      inet  n       -       y       -       -       smtpd
    
     -o content_filter=spamassassin
     spamassassin unix -     n       n       -       -       pipe
     user=spamd argv=/usr/bin/spamc -f -e  
     /usr/sbin/sendmail -oi -f ${sender} ${recipient}
  4. Save and close the file.

  5. Restart Postfix and enable SpamAssassin to run at system startup.

     $ sudo systemctl restart postfix.service
     $ sudo systemctl enable spamassassin.service
     $ sudo systemctl start spamassassin.service 

More Information

To learn more about SpamAssassin, please see the official website.