Email 5 min read

Postfix Spam Filter on Ubuntu: Set Up Rspamd Safely

Rspamd can filter a Postfix server in about half an hour. The part worth slowing down for is running it without rejections until you trust its scores.

By BashPilot Team
BashEmail Email spam protection Keep outbound mail clean and off blacklists, automatically. Explore BashEmail

A Postfix spam filter on Ubuntu no longer has to mean a chain of content filters and cron jobs. Rspamd plugs straight into Postfix as a milter, scores each message and tells Postfix what to do with it. Installing it is the easy half. The half that protects your users is running it in a no-reject mode first, so a bad score costs you a header on a message and not a lost invoice.

Install Rspamd and Redis

These commands come from the official installation guide. Redis is a separate install, and it matters: without it Rspamd turns off Bayes, greylisting, rate limiting and neural networks, though static checks such as SPF, DKIM, DMARC and RBLs still run.

add the repository and install
sudo apt-get update
sudo apt-get install -y lsb-release wget gpg
sudo mkdir -p /etc/apt/keyrings
wget -O- https://rspamd.com/apt-stable/gpg.key | gpg --dearmor | sudo tee /etc/apt/keyrings/rspamd.gpg > /dev/null
CODENAME=$(lsb_release -c -s)
echo "deb [signed-by=/etc/apt/keyrings/rspamd.gpg] http://rspamd.com/apt-stable/ $CODENAME main" | sudo tee /etc/apt/sources.list.d/rspamd.list
sudo apt-get update
sudo apt-get --no-install-recommends install rspamd
sudo apt-get install redis-server
sudo systemctl enable --now rspamd redis-server

The shipped configuration does not point at any Redis server, so tell it where to look. Rspamd's convention is to leave its stock files alone and put your changes in /etc/rspamd/local.d/, which survives upgrades.

connect Rspamd to Redis and verify
echo 'servers = "127.0.0.1";' | sudo tee /etc/rspamd/local.d/redis.conf
redis-cli -h 127.0.0.1 -p 6379 ping
sudo rspamadm configtest
sudo systemctl restart rspamd

You want PONG from Redis. Read all of the configtest output, since warnings can appear above a final syntax OK.

Switch off rejection before Postfix sees it

Do this before you connect Postfix. With the settings below, Rspamd scores everything and marks likely spam with an X-Spam: Yes header, but it delivers all of it. This is the same approach Rspamd's documentation suggests for testing alongside SpamAssassin.

observation mode
printf 'reject = null;\ngreylist = null;\n' | sudo tee /etc/rspamd/local.d/actions.conf
echo 'enabled = false;' | sudo tee /etc/rspamd/local.d/greylist.conf
sudo rspamadm configtest && sudo systemctl restart rspamd

Connect Postfix

By default Rspamd's workers listen on localhost only: 11332 is the milter endpoint, 11333 the scanner and 11334 the web interface. Add the milter to Postfix with postconf, which edits main.cf for you.

point Postfix at the milter
sudo postconf -e 'smtpd_milters = inet:localhost:11332'
sudo postconf -e 'non_smtpd_milters = inet:localhost:11332'
sudo postconf -e 'milter_default_action = accept'
sudo postfix check
sudo systemctl restart postfix

smtpd_milters covers mail arriving over SMTP, and non_smtpd_milters covers mail submitted locally through sendmail, which is how most PHP applications send. If your server already uses a milter, such as OpenDKIM, keep it in the list and separate the entries with a comma.

Prove it works with a real test message

A one-line test tells you little, because a message with no headers looks suspicious all by itself. Build a complete message, including the standard GTUBE test string that every Rspamd install recognises as spam.

create a test message and score it
cat > /tmp/gtube.eml <<EOF
From: Test Sender <sender@example.com>
To: you@example.net
Subject: Rspamd test
Date: $(date -R)
Message-ID: <$(date +%s).test@example.com>

XJS*C4JDBQADN1.NSBN3*2IDNEN*GTUBE-STANDARD-ANTI-UBE-TEST-EMAIL*C.34X
EOF
rspamc /tmp/gtube.eml

The output should list a GTUBE symbol. The string is a deliberate override, so it can report a reject action even in observation mode, and rspamc only reports a result without delivering anything. Next, send normal mail through the server from an outside account and read its headers, then watch the log.

see decisions as they happen
sudo grep -i milter /var/log/mail.log | tail -n 20
sudo tail -f /var/log/rspamd/rspamd.log

Use the web interface without exposing it

Rspamd's controller on port 11334 has a History tab that shows each message, its score and the symbols behind it. It is the best tool you have during the observation period. Leave it on localhost and reach it through an SSH tunnel, then open http://127.0.0.1:11334 in your browser.

from your own computer
ssh -L 11334:127.0.0.1:11334 you@mail.example.com

Be aware that tunnelled connections arrive from localhost, and localhost is trusted by default, so anyone with shell access on the server can use the interface without a password. If you do publish it, set a password with rspamadm pw, store the hash in /etc/rspamd/local.d/worker-controller.inc, and restrict access at the firewall.

Start enforcing

Give it a week or two of real traffic. Look through the History tab for wanted mail with high scores. When nothing legitimate is near 15, remove the observation files and Rspamd returns to its default thresholds. If you would like spam filed instead of tagged, a Dovecot Sieve rule can move anything with X-Spam: Yes into a Junk folder.

back to default thresholds
sudo rm /etc/rspamd/local.d/actions.conf /etc/rspamd/local.d/greylist.conf
sudo rspamadm configtest && sudo systemctl restart rspamd

Filtering what comes in does nothing for what your server sends. If mail you send is being refused, check your PTR record and SPF, DKIM and DMARC next.

When a scoring engine is not the whole job

Rspamd is an excellent engine, and on a server with a few mailboxes it may be all you need. Once you host mail for other people, the work shifts to quarantine reviews, per-customer policy and catching a stolen password before it damages your sending reputation. BashEmail for Ubuntu covers that on the server you already run, with no MX or DNS change. It applies server, domain and mailbox policy, lets an admin preview and release held mail, and contains risky senders through watch, hold and sending suspension. It fails open if the filter is unavailable.

Share LinkedIn X
Questions

Frequently asked questions

What is the best spam filter for Postfix on Ubuntu?

Rspamd and SpamAssassin are the two common open-source choices. Rspamd connects to Postfix directly as a milter and has a built-in web interface, which makes it a practical default on a new server. If you already run a well-tuned SpamAssassin setup, there may be little reason to change.

Does Rspamd need Redis?

Basic scoring works without it, including SPF, DKIM, DMARC and RBL checks. Without Redis, Rspamd disables Bayes, greylisting, rate limiting, neural networks, DMARC reporting and Redis-backed message history. Install `redis-server` and add `servers = "127.0.0.1";` to `/etc/rspamd/local.d/redis.conf`.

What are the default Rspamd spam thresholds?

The defaults are 4 for greylist, 6 for add header and 15 for reject. You can override any of them in `/etc/rspamd/local.d/actions.conf` by listing only the values you want to change. Avoid giving two actions the same threshold, because which one wins depends on internal ordering.

What happens to mail if Rspamd goes down?

That depends on Postfix's `milter_default_action`. Its default, `tempfail`, makes senders retry later, so mail is delayed. With `accept`, mail is delivered without being scanned until Rspamd returns. Choose based on whether a short delay or a short gap in filtering is worse for your users.

How do I test Rspamd without blocking real mail?

Set `reject = null;` and `greylist = null;` in `/etc/rspamd/local.d/actions.conf`, and `enabled = false;` in `greylist.conf`. Messages that reach the add header score still arrive, marked `X-Spam: Yes`. Review scores in the History tab for a week or two before removing those settings.

Put your servers on autopilot.

Connect a server in about a minute. The first week is on us.