Debian Mail Server, Part II: SPF and DKIM

TL;DR: check If you don't get 10/10, read this post.

In a previous post, we saw how to configure a Debian mail server with Postfix and Dovecot, but we did not address an important issue: spam. Major e-mail providers are defending their users against spam by requiring two extra credentials: Sender Policy Framework (SPF) and DomainKey Identified Mail (DKIM). We will see how to configure these two services. We will also configure a local defense against incoming spam with SpamAssassin.

Configuring SPF

Sender Policy Framework (SPF) is one of the two services you should configure in order not to be considered as a spammer by major e-mail service providers. Its installation starts with:

sudo apt-get install postfix-policyd-spf-python

To enable the policy service in Postfix, append the following to /etc/postfix/

policy-spf  unix  -       n       n       -       -       spawn
     user=nobody argv=/usr/bin/policyd-spf

Then, add the instruction check_policy_service unix:private/policy-spf to your smtpd_recipient_restrictions in /etc/postfix/ A complete list of recipient restrictions, including SPF, looks like:

smtpd_recipient_restrictions =
    check_policy_service unix:private/policy-spf,
    check_sender_access hash:/etc/postfix/sender_access,
    check_recipient_access hash:/etc/postfix/recipient_access,

A few rules apply: the policy service should always be after reject_unauth_destination, otherwise early responses from SPF can turn your system into an open relay. Also, put the policy service after you permit local senders (permit_sasl_authenticated), as SPF should be applied to inbound e-mail from the Internet, not outbound e-mail from your users.

You should also add the following line to in order to avoid timeouts:

policy-spf_time_limit = 3600s

Finally, you will need to setup an SPF record in your DNS. It is basically a TXT record that looks as follows:

600 IN TXT "v=spf1 a mx ip4:YOUR_IPv4_ADDRESS ip6:YOUR_IPv6_ADDRESS ~all"

Adding your IP addresses is not mandatory but, as we will see later on, GMail will give you a softfail score if it cannot find these.

Configuring DKIM

DomainKeys Identified Mail (DKIM) is the other service you need to configure in order not to be considered as a spammer by big e-mail providers. It ties your e-mail server to your domain name, so that receivers can check that e-mails originating from your domain indeed correspond to your computer. Start the installation with:

apt-get install opendkim opendkim-tools

DKIM is based on asymmetric cryptography. Basically, we will generate a pair of public/private keys on your server, and publish the public key on your DNS records. First, edit /etc/opendkim.conf and make sure it contains the following:

KeyTable           /etc/opendkim/KeyTable
SigningTable       /etc/opendkim/SigningTable
ExternalIgnoreList /etc/opendkim/TrustedHosts
InternalHosts      /etc/opendkim/TrustedHosts
LogWhy yes

Create the directory /etc/opendkim if it does not exist. Then, enter all your domains, hostnames or IP addresses in /etc/opendkim/TrustedHosts:

Now, edit /etc/default/opendkim and uncomment the following line:

SOCKET="inet:12345@localhost" # listen on loopback on port 12345

Connect this to Postfix by appending the following block to /etc/postfix/

milter_default_action = accept
milter_protocol = 6
smtpd_milters = inet:localhost:12345
non_smtpd_milters = inet:localhost:12345

Now, we need to generate the pair of public/private keys for your server. It goes as follows:

mkdir -p /etc/opendkim/keys/
cd /etc/opendkim/keys/
opendkim-genkey -s default -d
chown opendkim:opendkim default.private

Next, add the key to /etc/opendkim/KeyTable:

And to /etc/opendkim/SigningTable:

(In some tutorials, you will see a * instead of just at the beginning of this line. I tried both, but only the latter passed the DKIM checks discussed in the next section.)

Finally, display your DNS key with:

$ cat /etc/opendkim/keys/
default._domainkey IN TXT "v=DKIM1; k=rsa; p=MIGfM...long hash...DAQAB"

You need to enter the full line as a free text (TXT) record of your domain's DNS (how to do so depends on your provider). If you cannot edit your DNS record directly but instead need to go through a GUI, just remember that the field name starts with "default._domainkey". (This was the case of my provider's interface, and I left the default value there, which was my domain name; subsequent experiences showed that DKIM was not working and a major e-mail provider identified me as a spammer.) Once you have configured your DNS, you can check it with:

dig TXT

The output should contain an "ANSWER" section with the same content as your default.txt file.

Testing your credentials

There are a few testing services available today to check the spammyness of your e-mails. A simple one is provided by Port 25: send an empty e-mail to and you will get an SPF/DKIM report by return mail that looks like:

SPF check:          pass
DomainKeys check:   neutral
DKIM check:         pass
DKIM check:         pass
Sender-ID check:    pass
SpamAssassin check: ham

(Don't worry about the DomainKeys check, which is for a version of the protocol anterior to DKIM.) The second, and in my opinion most useful service, is It performs similar checks, and also gives you hints on how to improve your configuration. Thanks to this service, I could identify and fix the DKIM mis-configuration mentioned above, also learned about the following two improvements.

Configuring DMARC

Once both SPF and DKIM are configured, it is easy, yet appreciated by other e-mail providers, to add a DMARC record to your DNS. Just create a new TXT field with the default values:

_dmarc IN TXT "v=DMARC1; p=none"

Reverse DNS

Some major e-mail providers check the domain name in your e-mails by a reverse DNS lookup. Consequently, you should make sure that your reverse DNS (dig -x +short <your_IP_address>) returns the domain name used in your e-mails. If it is not the case, you will need to configure a pointer (PTR type) record in your DNS. This option was available in the web interface of my registrar, but some registrars may require you to contact customer support to do this.

The GMail test

GMail is probably the most thorough spam fighter among public e-mail providers today. If you have a GMail account (you can just create an empty one for the task), you can send it a mail and look for the Authentication-Results field in the source code of the received message. On my first trial, it looked like:

       spf=softfail ( domain of transitioning
           does not designate IP_ADDRESS_OF_THE_SERVER as permitted sender)
       dmarc=pass (p=NONE dis=NONE)

We see that GMail indeed checks for SPF, DKIM and DMARC. Here, it noticed something that both previous tests had missed: my SPF record did not include my server's IP address, which it considered suspicious ("domain of transitioning email_address does not designate ip_address as permitted sender"). After adding my IP to the SPF record, the softfail became a pass.

Configuring SpamAssassin

Sooner rather than later, your e-mail server will receive spam. Good e-mail clients have junk filters, but the fight against it starts at the server level. SpamAssassin is a renowned e-mail filter that does this job; plus, it is easy to install and configure. Start the setup with:

apt-get install spamassassin spamc

Let us first configure Postfix's we need to add the filter -o content_filter=spamassassin to the smtp, smtps and submission services:

smtp      inet  n       -       -       -       -       smtpd
  -o content_filter=spamassassin
submission inet n       -       -       -       -       smtpd
  -o syslog_name=postfix/submission
  -o smtpd_tls_security_level=encrypt
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_client_restrictions=permit_sasl_authenticated,reject
  -o milter_macro_daemon_name=ORIGINATING
  -o content_filter=spamassassin
smtps     inet  n       -       -       -       -       smtpd
  -o syslog_name=postfix/smtps
  -o smtpd_tls_wrappermode=yes
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_client_restrictions=permit_sasl_authenticated,reject
  -o milter_macro_daemon_name=ORIGINATING
  -o content_filter=spamassassin

Then, add the following at the end of the file:

spamassassin unix -     n       n       -       -       pipe
  user=debian-spamd argv=/usr/bin/spamc -f -e /usr/sbin/sendmail -oi -f ${sender} ${recipient}

Finally, you can uncomment the following line in /etc/spamassassin/ to label spam mails:

rewrite_header Subject *****SPAM*****


At the time of writing this post, I learned from articles of the Debian Wiki and the Ubuntu Community Wiki, as well as from the following tutorials: