Faruque Ahmed : MCP, MCSA, MCSE, MCTS, MCIT, CCNA, OCA, OCP, GCP
Follow these steps to set up an automated spam filter on your Linux Ubuntu web server using SpamAssassin, Dovecot, and Postfix:
Update the package lists and install Postfix, Dovecot, SpamAssassin, and spamc:
sudo apt install postfix dovecot-imapd dovecot-pop3d spamassassin spamc
Enable the SpamAssassin service to start automatically at boot, and then start the service:
sudo systemctl enable spamassassin
sudo systemctl start spamassassin
Edit the SpamAssassin configuration file /etc/spamassassin/local.cf using a text editor (e.g., nano or vim):
sudo nano /etc/spamassassin/local.cf
Add or modify the following settings in the file:
rewrite_header Subject [SPAM]required_score 5.0use_bayes 1bayes_auto_learn 1These settings will mark emails with a spam score of 5.0 or higher by adding "[SPAM]" to the subject. You can adjust the required_score value as needed. Save the changes and exit the editor.
Edit the Postfix configuration file /etc/postfix/master.cf:
sudo nano /etc/postfix/master.cf
Add the following lines 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}
Save the changes and exit the editor.
Restart the Postfix service to apply the changes:
sudo systemctl restart postfix
Edit the Dovecot configuration file /etc/dovecot/conf.d/90-sieve.conf:
sudo nano /etc/dovecot/conf.d/90-sieve.conf
Uncomment or add the following lines:
plugin {
sieve = ~/.dovecot.sieve
sieve_default = /etc/dovecot/sieve/default.sieve
sieve_dir = ~/sieve
sieve_global_dir = /etc/dovecot/sieve/global/
}
Save the changes and exit the editor.
Create a default Sieve script /etc/dovecot/sieve/default.sieve:
sudo nano /etc/dovecot/sieve/default.sieve
Add the following content to the file:
require ["fileinto"];
if header :contains "X-Spam-Flag" "YES" {
fileinto "Junk";
}
This script will automatically move messages marked as spam to the "Junk" folder. Save the changes and exit the editor.
Compile the Sieve script:
sudo sievec /etc/dovecot/sieve/default.sieve
Step 7: Restart Dovecot
Restart the Dovecot service to apply the changes:
sudo systemctl restart dovecot
SpamAssassin will now automatically process incoming emails on your server and mark them as spam based on your configuration. The Dovecot Sieve script will move the messages marked as spam to the "Junk" folder for each user. You can further refine the configuration by editing /etc/spamassassin/local.cf and adjusting the settings as needed.
Additionally, users can create their own Sieve scripts to manage their spam filters and email organization based on their preferences. To do this, they should place their scripts in their ~/sieve directory and compile them using the sievec command.