Guide: self-hosted SMTP server with Postfix + OpenDKIM
DKIM cryptographically signs your emails to prove they genuinely come from your domain. This guide details the complete setup on Debian/Ubuntu: Postfix for sending, OpenDKIM for signing, and DNS publication.
1. The DKIM principle
DKIM adds a digital signature in the DKIM-Signature header of every sent email. The receiving server verifies this signature using the public key published in DNS (TXT on selector._domainkey.yourdomain.com). If the signature is valid and aligned with the sender's domain, the email passes DKIM authentication.
2. Install Postfix and OpenDKIM
On Debian/Ubuntu, install the required packages. opendkim-tools provides opendkim-genkey and opendkim-testkey — a classic pitfall: "command not found" without this package.
sudo apt update
sudo apt install postfix opendkim opendkim-tools
# During Postfix installation, choose "Internet Site"
# and enter your fully qualified domain name (FQDN).
3. Generate and install the key
You can use the generator above, or generate directly on the server with opendkim-genkey (the .txt file contains the DNS record to publish, the .private the key to keep secret). Place the key in a dedicated directory and restrict its permissions.
sudo mkdir -p /etc/opendkim/keys/yourdomain.com
sudo opendkim-genkey -b 2048 -d yourdomain.com -D /etc/opendkim/keys/yourdomain.com \
-s default -v
sudo chown -R opendkim:opendkim /etc/opendkim
sudo chmod 600 /etc/opendkim/keys/yourdomain.com/default.private
# The default.txt file contains the DNS record to publish
4. Configure OpenDKIM
Edit /etc/opendkim.conf to enable signing, then create the tables that map domains, selectors and keys.
# /etc/opendkim.conf
Mode sv
Domain yourdomain.com
RequireSafeKeys true
Socket inet:8891@localhost
KeyTable refile:/etc/opendkim/key.table
SigningTable refile:/etc/opendkim/signing.table
ExternalIgnoreList refile:/etc/opendkim/trusted.hosts
InternalHosts refile:/etc/opendkim/trusted.hosts
# /etc/opendkim/signing.table
*@yourdomain.com default._domainkey.yourdomain.com
# /etc/opendkim/key.table
default._domainkey.yourdomain.com yourdomain.com:default:/etc/opendkim/keys/yourdomain.com/default.private
# /etc/opendkim/trusted.hosts
127.0.0.1
localhost
yourdomain.com
5. Hook up Postfix
Add the OpenDKIM milter to Postfix's configuration so every outgoing email is signed automatically, then restart both services.
# /etc/postfix/main.cf — add at the end:
milter_default_action = accept
milter_protocol = 6
smtpd_milters = inet:localhost:8891
non_smtpd_milters = inet:localhost:8891
sudo systemctl restart opendkim postfix
sudo systemctl enable opendkim postfix
6. Publish the DNS record
Publish the contents of the default.txt file (or the one generated above) as a TXT record on default._domainkey.yourdomain.com. A 2048-bit RSA key exceeds 255 characters: split the value into several quoted strings (the DNS standard allows this). Set a low TTL (300 s) during testing, then raise it (3600 s) once validated.
# Example TXT record (value split into 2 strings)
default._domainkey.yourdomain.com. 300 IN TXT ("v=DKIM1; k=rsa; "
"p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKC...")
7. Test
First verify DNS publication with opendkim-testkey (the "key OK" confirms the public key is properly deployed), then send a test email and check that the DKIM-Signature header is present and valid. The site's spam test checks DKIM, SPF, DMARC and the SpamAssassin score in real time.
sudo opendkim-testkey -d yourdomain.com -s default -vvv
# Expected result: « key OK »
# Send a test email and check the headers
echo "DKIM test" | mail -s "DKIM test" you@example.com
# Then check the DKIM-Signature header in the received message
Pitfalls to avoid
- Key too short: Gmail has refused keys < 1024 bits since 2018 and recommends 2048 (RFC 8301). A 512-bit key = near-systematic rejection.
Truncated TXT value: a DNS string cannot exceed 255 characters. The 2048 key must be split into several quoted strings, otherwise it is silently truncated and the signature fails.- TTL during testing: set 300 s while validating, then raise to 3600 s. A high TTL during setup = slow propagation and painful debugging.
- DNS propagation: up to 48 h depending on resolvers, though often just a few minutes. Don't trigger a key rotation in the middle of a campaign.
- Distinct selectors: if multiple services sign (self-hosted Postfix + SendGrid + Google Workspace), use one selector per service (default, s1, google...) so you can revoke one without breaking the others.
- DMARC alignment: DMARC requires the signing domain (d=) to align with the From domain. In strict mode (adkim=s) an exact match is required; in relaxed mode (adkim=r) the organizational domain is enough (news.example.com aligns with example.com).
- Key rotation: publish the new key on a new selector, keep the old one in DNS until secondary DNS servers and caches expire (1-2 weeks), then remove the old one. Never change the key on the same selector in production.
- Exposed private key: if it leaks (public Git repo, ticket, unencrypted backup), revoke it immediately and generate a new one. OpenDKIM's
RequireSafeKeys true refuses to sign if the key is readable by others. - Consistent PTR and HELO: DKIM alone is not enough. The sending IP must have a valid PTR resolving to the same hostname (forward-confirmed reverse DNS), and the SMTP HELO must match. Gmail requires FCrDNS for all senders.
- Outgoing port 25 blocked: many ISPs and clouds block outgoing port 25 by default (residential ISPs, AWS EC2). Use an SMTP relay (smart host) or request unblocking from your host. Without port 25, your emails don't leave.