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.
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.
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.
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.
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.
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.
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.
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.
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.
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.