Docs / Email Servers / How to Set Up DKIM with OpenDKIM on Ubuntu

How to Set Up DKIM with OpenDKIM on Ubuntu

By Admin · Feb 25, 2026 · Updated Apr 25, 2026 · 39 views · 2 min read

What Is DKIM?

DomainKeys Identified Mail (DKIM) is an email authentication method that digitally signs outgoing messages. Receiving servers verify the signature against a public key published in DNS, confirming the message was not tampered with in transit.

Install OpenDKIM

sudo apt update && sudo apt install -y opendkim opendkim-tools

Generate a Key Pair

sudo mkdir -p /etc/opendkim/keys/example.com
sudo opendkim-genkey -b 2048 -d example.com -D /etc/opendkim/keys/example.com -s mail -v
sudo chown -R opendkim:opendkim /etc/opendkim/keys/

Configure OpenDKIM

Edit /etc/opendkim.conf:

AutoRestart          Yes
AutoRestartRate      10/1h
Mode                 sv
Canonicalization     relaxed/simple
SignatureAlgorithm   rsa-sha256
KeyTable             /etc/opendkim/key.table
SigningTable         refile:/etc/opendkim/signing.table
InternalHosts        /etc/opendkim/trusted.hosts

Create Mapping Files

Key table (/etc/opendkim/key.table):

mail._domainkey.example.com example.com:mail:/etc/opendkim/keys/example.com/mail.private

Signing table (/etc/opendkim/signing.table):

*@example.com mail._domainkey.example.com

Trusted hosts (/etc/opendkim/trusted.hosts):

127.0.0.1
localhost
example.com

Connect to Postfix

Add to /etc/postfix/main.cf:

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

Publish the DNS Record

Display the public key:

cat /etc/opendkim/keys/example.com/mail.txt

Create a TXT record named mail._domainkey.example.com with the displayed value. After DNS propagation, test with:

opendkim-testkey -d example.com -s mail -vvv

Was this article helpful?