Skip to content

OpenDKIM signing for Postfix — keys, selectors, and verification

Wire OpenDKIM into Postfix as a milter, generate a 2048-bit key with a selector, publish the DNS record, and verify outbound mail carries a passing DKIM signature.

DKIM signatures turn “this message claims to be from example.com” into something a receiver can cryptographically verify. This article is the OpenDKIM install we apply once Postfix and TLS are in place — generate a 2048-bit key, publish the DNS TXT record with the selector, configure OpenDKIM as a Postfix milter, and confirm an outbound message lands at Gmail with dkim=pass. Without DKIM, your DMARC policy from opendmarc-validation-postfix cannot enforce anything.

How to verify

Audit what is signed today. A Postfix that has never been touched signs nothing — every outbound Authentication-Results from a receiver will read dkim=none.

dpkg -l | grep -E 'opendkim|dkim-milter'
dig +short TXT default._domainkey.example.com
dig +short TXT _dmarc.example.com
echo "test" | mail -s "dkim probe" check-auth@verifier.port25.com
# inspect the bounce reply for dkim=none vs dkim=pass

The port25.com verifier returns a free authentication report by email — DKIM, SPF, DMARC, sender-ID, and the SpamAssassin score. It is the simplest external check on outbound posture.

What’s happening

OpenDKIM is a SMTP milter — a process that hangs off Postfix’s filter API and can read, modify, or reject mail in flight. When Postfix accepts a message via submission, the milter gets a copy, signs the message header with a private key, and Postfix replaces the outbound message with the signed version. The receiver pulls the public key from DNS (<selector>._domainkey.<domain> TXT record), reconstructs the canonical message body, and verifies the signature.

The key terms:

  • Selector — a label that lets you rotate keys without changing the domain. Convention is default, mail, sh2026q2, or a date stamp. Multiple selectors can exist simultaneously; the signer picks one, the verifier follows the s= tag in the signature header.
  • Canonicalization — how the header and body are normalized before signing. relaxed/relaxed is the practical choice — it tolerates whitespace mangling by intermediate gateways. simple/simple rejects almost any in-flight change.
  • Key size — 2048 bits is the floor for new deployments. 1024 is being deprecated by major receivers; 4096 is overkill for most domains and trips DNS UDP packet size on some resolvers.

The milter runs on a UNIX socket Postfix can reach. We bind it to a local TCP port or, better, a UNIX socket in Postfix’s chroot — the chroot path is the same pattern as Dovecot’s SASL socket.

The procedure

  1. Install OpenDKIM and the tools package. The tools package ships opendkim-genkey, which is what we use to make keys:

    sudo apt update
    sudo apt install -y opendkim opendkim-tools
    sudo systemctl status opendkim --no-pager
  2. Generate a 2048-bit key pair with a selector. Pick a selector — we use a year-quarter convention so rotation is obvious in logs:

    sudo mkdir -p /etc/opendkim/keys/example.com
    cd /etc/opendkim/keys/example.com
    sudo opendkim-genkey -b 2048 -d example.com -s sh2026q2 -v
    sudo chown -R opendkim:opendkim /etc/opendkim/keys
    sudo chmod 600 /etc/opendkim/keys/example.com/sh2026q2.private
    ls -l
    # sh2026q2.private  (the key, keep secret)
    # sh2026q2.txt      (the DNS TXT record to publish)
  3. Publish the public key in DNS. Open sh2026q2.txt — it contains a TXT record split across multiple lines. Publish it as a single concatenated string at sh2026q2._domainkey.example.com. Concatenate carefully:

    sudo cat /etc/opendkim/keys/example.com/sh2026q2.txt
    # the record looks like:
    # sh2026q2._domainkey IN TXT ( "v=DKIM1; k=rsa; "
    #   "p=MIIBIjANBgkqhkiG9w0BAQEFAAOC..." )

    At the DNS provider, paste the entire p= string as one value. Wait for propagation, then verify:

    dig +short TXT sh2026q2._domainkey.example.com
    sudo opendkim-testkey -d example.com -s sh2026q2 -vvv

    opendkim-testkey should print key OK.

  4. Configure OpenDKIM. Edit /etc/opendkim.conf:

    Syslog                  yes
    UMask                   002
    Mode                    sv
    Canonicalization        relaxed/relaxed
    ExternalIgnoreList      refile:/etc/opendkim/TrustedHosts
    InternalHosts           refile:/etc/opendkim/TrustedHosts
    KeyTable                refile:/etc/opendkim/KeyTable
    SigningTable            refile:/etc/opendkim/SigningTable
    SignatureAlgorithm      rsa-sha256
    Socket                  local:/var/spool/postfix/opendkim/opendkim.sock
    PidFile                 /run/opendkim/opendkim.pid
    UserID                  opendkim:opendkim

    Mode sv means sign (s) and verify (v). The socket lives inside Postfix’s chroot.

  5. Populate the key, signing, and trusted-hosts tables. Three small files:

    sudo tee /etc/opendkim/KeyTable >/dev/null <<EOF
    sh2026q2._domainkey.example.com example.com:sh2026q2:/etc/opendkim/keys/example.com/sh2026q2.private
    EOF
    
    sudo tee /etc/opendkim/SigningTable >/dev/null <<EOF
    *@example.com sh2026q2._domainkey.example.com
    EOF
    
    sudo tee /etc/opendkim/TrustedHosts >/dev/null <<EOF
    127.0.0.1
    ::1
    localhost
    mx1.example.com
    10.0.0.0/8
    EOF
    
    sudo chown -R opendkim:opendkim /etc/opendkim
  6. Create the socket directory inside Postfix’s chroot. Postfix runs chrooted; the socket has to be reachable from inside:

    sudo mkdir -p /var/spool/postfix/opendkim
    sudo chown opendkim:postfix /var/spool/postfix/opendkim
    sudo chmod 750 /var/spool/postfix/opendkim

    Then add to /etc/default/opendkim:

    SOCKET="local:/var/spool/postfix/opendkim/opendkim.sock"
  7. Wire the milter into Postfix. Add to /etc/postfix/main.cf:

    milter_default_action = accept
    milter_protocol = 6
    smtpd_milters = unix:opendkim/opendkim.sock
    non_smtpd_milters = $smtpd_milters

    Reload both:

    sudo systemctl restart opendkim
    sudo systemctl reload postfix
    sudo journalctl -u opendkim -n 30 --no-pager
  8. Verify with an external probe. Send a message through the submission port and inspect the receiver’s Authentication-Results:

    swaks --to check-auth@verifier.port25.com \
          --from user@example.com --server mx1.example.com:587 -tls \
          --auth LOGIN --auth-user user@example.com --auth-password 'pw'

    The reply email should contain dkim=pass header.d=example.com. If it reads dkim=neutral (no signature), OpenDKIM is not running or the milter isn’t wired. If it reads dkim=fail, the DNS record doesn’t match the private key.

Common pitfalls

  • The DNS TXT record is pasted with the line continuation quotes intact ("p=AAA" "BBB") — most resolvers will return only the first chunk; you have to concatenate inside the quoted value.
  • The OpenDKIM service starts but Postfix logs connect to Milter service unix:opendkim/opendkim.sock: No such file or directory — the chroot directory wasn’t created with the right owner, so OpenDKIM’s bind silently failed.
  • Outbound mail is signed but receivers still report dkim=none because the milter signs body but Postfix’s non_smtpd_milters is unset — local-pickup mail (cron jobs, app servers via sendmail) skips the milter chain.
  • Key rotation requires a second selector live for the overlap window — never just overwrite sh2026q2.private and republish; messages signed with the old key fail verification until you keep both DNS records up.
  • 2048-bit DKIM TXT records exceed 255 octets per string and must be split into multiple quoted strings inside a single TXT record — some DNS UIs hide this and break the publish without warning.

Stack Harbor stands up DKIM as a single deployable unit — key generated with a dated selector, DNS record published from version-controlled zone data, milter wired into both smtpd_milters and non_smtpd_milters, and external verification proven before declaring the deploy complete. Key rotation runs on a quarterly cadence in the engagements we run; that operating rhythm is part of our Managed Operations practice.