Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How Do You Sign Outgoing Mail With OpenDKIM and Postfix?

EmailLinux
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedDec 3, 2026
7 min read

To sign outgoing mail with OpenDKIM and Postfix, install OpenDKIM, generate a key pair for your domain, publish the public key as a DNS TXT record, and connect Postfix to OpenDKIM as a milter so every outgoing message gets a DKIM-Signature header. DKIM proves the message came from a server authorised to sign for your domain and that the signed parts were not changed in transit.

This is an OpenDKIM Postfix setup for Debian or Ubuntu, using example.com as the domain and mail as the selector. It follows the Postfix, Dovecot and TLS setup.

Table of Contents

  • How DKIM signing works
  • Installing OpenDKIM and generating keys
  • Configuring OpenDKIM
  • Publishing the DNS record
  • Connecting Postfix to OpenDKIM
  • Verifying signatures
  • Rotating DKIM keys
  • Frequently Asked Questions
    • Do I still need SPF if I have DKIM?
    • Why does DKIM fail only for some messages?
    • What key size and selector should I use?
    • Should OpenDKIM sign every domain?
  • Summary
  • Related Articles

How DKIM signing works

DKIM (DomainKeys Identified Mail) uses a public key signature:

  1. Your server holds a private key and signs selected headers and the body of each message.
  2. The signature goes into a DKIM-Signature header that names the domain (d=) and the selector (s=).
  3. The receiver reads those two values, looks up the TXT record selector._domainkey.domain, and verifies the signature with the public key published there.

The selector lets you keep several keys at once, which makes key rotation safe. DKIM works with SPF and DMARC: DMARC passes when SPF or DKIM passes and the domain aligns with the visible From address. Gmail and Yahoo require DKIM for bulk senders, and signing all mail is good practice for any sender.

Installing OpenDKIM and generating keys

OpenDKIM runs as a separate daemon and talks to Postfix over a socket using the milter protocol.

bash

Use a 2048-bit RSA key. Old 1024-bit keys are weak, though 2048-bit TXT records are long enough that some DNS panels need them split into quoted strings. The command creates mail.private (keep it secret) and mail.txt (the DNS record).

Configuring OpenDKIM

Edit /etc/opendkim.conf.

text

Mode sv makes OpenDKIM both sign outgoing mail and verify incoming mail. relaxed/simple tolerates harmless whitespace changes in headers. Create the three tables.

bash

The key table maps a key name to the domain, selector and private key file. The signing table says which sender addresses use which key, so you can add one line per domain. Postfix must be in the opendkim group to reach the socket.

Publishing the DNS record

Show the record the tool generated.

bash
text

Create a TXT record named mail._domainkey.example.com whose value joins the quoted parts into one string: v=DKIM1; h=sha256; k=rsa; p=.... Many DNS panels want only the part inside the quotes, with no line breaks. Wait for DNS to propagate, then test the record and key together.

bash

key OK means the DNS record matches the private key on the server. If it says key not secure, that only means DNSSEC is not enabled and is not an error.

Connecting Postfix to OpenDKIM

Add the milter to Postfix, then restart both services.

bash

The socket path opendkim/opendkim.sock is relative to Postfix's queue directory, /var/spool/postfix, which matches the Socket line above. Setting milter_default_action = accept keeps mail flowing if OpenDKIM is down, at the cost of sending unsigned mail until you fix it. Use tempfail if you prefer to hold mail instead. non_smtpd_milters makes locally generated mail, such as cron messages, signed as well.

Verifying signatures

Send a message to a mailbox you control and read the original headers.

text

dkim=pass is the result you want. You can also send a message to an address provided by an email-testing service and read its report. Check the server side with sudo journalctl -u opendkim -n 30, which logs lines such as DKIM-Signature field added (s=mail, d=example.com).

Common failures are a socket permission error (check the group membership and the directory owner), a DNS record with stray spaces or a missing p= value, a selector mismatch between the key table and DNS, and a message changed after signing, for example by a footer added by a mailing list.

Rotating DKIM keys

Rotate keys once or twice a year, or at once if a private key could have leaked.

  1. Generate a new key with a new selector, for example 2027a.
  2. Publish its TXT record and wait for propagation.
  3. Update key.table and signing.table to use the new selector, and reload OpenDKIM.
  4. Keep the old DNS record for a few days so mail already in transit can still be verified, then remove it.

Frequently Asked Questions

Do I still need SPF if I have DKIM?

Yes. SPF and DKIM check different things, and DMARC needs at least one of them to pass with a domain that aligns with the From address. Publish both.

Why does DKIM fail only for some messages?

A mailing list, security gateway or forwarding rule may alter the message after signing. Mail that is only forwarded without changes normally still verifies.

What key size and selector should I use?

Use 2048-bit RSA keys, or 1024 bits only if your DNS provider cannot hold longer records. Pick a selector you can change, such as the year or a name like mail.

Should OpenDKIM sign every domain?

Sign each domain you send from, by adding a line per domain to the signing and key tables with its own key.

Summary

Install OpenDKIM, generate a 2048-bit key, publish the TXT record at selector._domainkey.domain, and configure the key and signing tables. Connect Postfix with smtpd_milters and non_smtpd_milters, then confirm dkim=pass in a real message header and with opendkim-testkey. Rotate keys with a new selector so signing never has a gap.

Related Articles

  • How Do You Secure a Mail Server With a Let's Encrypt Certificate?
  • How Do You Configure Postfix to Relay Through a Smarthost?
  • Learn Email Delivery: Complete Study Roadmap, Beginner to Advanced
Share

Similar posts

  • How Do You Configure Postfix to Relay Through a Smarthost?
  • How Do You Secure a Mail Server With a Let's Encrypt Certificate?
  • How Do You Configure Dovecot for IMAP and Mailboxes?
  • How Do You Set Up a Postfix Mail Server on Linux?
  • How Do You Check Email DNS Records Using dig and nslookup?
  • How Do You Test an SMTP Server Using Telnet or OpenSSL?
  • Self-Hosted Email vs Email Service Provider: Which to Choose?
  • How Do You Keep an Email List Clean and Avoid Spam Traps?