Back to tutorials
Tutorial

Set Up DKIM Signing With OpenDKIM on Ubuntu Postfix

Set up OpenDKIM on Postfix with keys, DNS records, and real header tests to fix DKIM fail on your Ubuntu VPS.

By Anurag Singh
Updated on Nov 30, 2026
Category: Tutorial
Share article
Set Up DKIM Signing With OpenDKIM on Ubuntu Postfix

Your mail can “send fine” and still land in spam if DKIM isn’t passing. Often the issue isn’t your app. It’s the server that hands mail to the internet. This walkthrough shows you how to set up OpenDKIM on Postfix on Ubuntu, publish the right DNS records, and confirm it works by reading real message headers.

Scope: Ubuntu 24.04 LTS (works similarly on Ubuntu 22.04).

Assumptions: Postfix is already installed, and your server can send outbound SMTP.

You’ll set up DKIM for one or more domains. Then you’ll verify the signals receivers care about: DKIM + SPF alignment, and a From domain that matches what you sign.

Before you touch OpenDKIM: check your VPS and DNS basics

DKIM won’t rescue a shaky mail setup. Spend a few minutes on the basics first. Otherwise, you’ll end up with “DKIM passes but deliverability still stinks.”

  • Correct reverse DNS (PTR): your VPS IP should resolve back to a hostname you control (example: mail.example.com).
  • A/AAAA record for the mail hostname: mail.example.com points to your server IP.
  • SPF exists: at least one SPF TXT record at the root of your domain.
  • From address matches your domain: From: you@example.com should be the domain you’re signing.

If you’re still stuck at “the contact form doesn’t send,” fix outbound sending first. Then come back to DKIM.

These HostMyCode tutorials focus on getting mail out reliably:

If you need a server that’s actually suited for outbound mail (static IP, controllable rDNS, clean Linux base), start with a HostMyCode VPS. It’s a straightforward place to run Postfix + OpenDKIM without shared-host restrictions.

Step 1: Install OpenDKIM and its tools on Ubuntu

Install the OpenDKIM milter plus the tools used to generate keys and troubleshoot.

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

Confirm the service is present:

systemctl status opendkim --no-pager

You’ll often see it running already, but not connected to Postfix yet. That’s expected.

Step 2: Pick a DKIM selector and create a key for your domain

A selector is a label for a key. It lets you rotate keys later without changing the domain.

Common choices: dkim, mail, s1, 2026a. This tutorial uses s1.

Create a key directory that’s readable and locked down:

sudo install -d -m 0750 -o opendkim -g opendkim /etc/opendkim/keys

Now create a per-domain folder and generate the key pair:

DOMAIN=example.com
SELECTOR=s1
sudo install -d -m 0750 -o opendkim -g opendkim /etc/opendkim/keys/$DOMAIN
cd /etc/opendkim/keys/$DOMAIN
sudo opendkim-genkey -s $SELECTOR -d $DOMAIN
sudo chown opendkim:opendkim $SELECTOR.private
sudo chmod 0600 $SELECTOR.private

You’ll get two files:

  • s1.private (your private signing key; keep it on the server only)
  • s1.txt (a suggested DNS TXT record)

View the DNS record OpenDKIM generated:

sudo cat /etc/opendkim/keys/$DOMAIN/$SELECTOR.txt

Hold off on publishing it for a moment. First, wire the server side so Postfix can sign messages.

Step 3: Configure OpenDKIM: KeyTable, SigningTable, TrustedHosts

On Ubuntu, OpenDKIM reads /etc/opendkim.conf plus table files. You’ll add entries that tell OpenDKIM which key to use.

You’ll also define which sources are allowed to submit mail for signing.

Create the table files:

sudo nano /etc/opendkim/KeyTable

Add a line (single line):

s1._domainkey.example.com example.com:s1:/etc/opendkim/keys/example.com/s1.private
sudo nano /etc/opendkim/SigningTable

Add a rule that signs mail when the From domain matches:

*@example.com s1._domainkey.example.com
sudo nano /etc/opendkim/TrustedHosts

Add the hosts allowed to submit mail to be signed:

127.0.0.1
localhost
::1

If your apps submit via localhost (the usual case), that list is enough.

If other machines submit mail to this Postfix over the network, add their IPs/subnets. Secure SMTP properly (AUTH/TLS) before you do.

Don’t add 0.0.0.0/0. That’s an open relay waiting to happen.

Set sane ownership and permissions on the table files:

sudo chown root:root /etc/opendkim/KeyTable /etc/opendkim/SigningTable /etc/opendkim/TrustedHosts
sudo chmod 0644 /etc/opendkim/KeyTable /etc/opendkim/SigningTable /etc/opendkim/TrustedHosts

Step 4: Edit /etc/opendkim.conf for socket and tables

OpenDKIM needs a socket that Postfix can reach. On Ubuntu, the clean approach is a local Unix socket inside Postfix’s chroot area.

That keeps it off the network and avoids permission surprises.

Edit the OpenDKIM config:

sudo nano /etc/opendkim.conf

Confirm or set these values (add missing lines, and keep only one copy of each setting):

# Identify this host
Syslog                  yes
SyslogSuccess           yes
LogWhy                  yes

# DKIM signing
Canonicalization        relaxed/simple
Mode                    sv
SubDomains              no
AutoRestart             yes
AutoRestartRate         10/1h
UMask                   002

# Tables
KeyTable                refile:/etc/opendkim/KeyTable
SigningTable            refile:/etc/opendkim/SigningTable
ExternalIgnoreList      refile:/etc/opendkim/TrustedHosts
InternalHosts           refile:/etc/opendkim/TrustedHosts

# Socket for Postfix (Unix socket)
Socket                  local:/var/spool/postfix/opendkim/opendkim.sock

Create the socket directory inside the Postfix spool. Set ownership so OpenDKIM can create the socket and Postfix can access it.

sudo install -d -m 0755 /var/spool/postfix/opendkim
sudo chown opendkim:postfix /var/spool/postfix/opendkim

Now update the Ubuntu defaults file so it matches your socket path (/etc/default/opendkim):

sudo nano /etc/default/opendkim

Set (or adjust) the socket option so it matches opendkim.conf:

SOCKET="local:/var/spool/postfix/opendkim/opendkim.sock"

Restart OpenDKIM:

sudo systemctl restart opendkim
sudo systemctl status opendkim --no-pager

Quick check: confirm the socket exists.

sudo ls -la /var/spool/postfix/opendkim/

If you don’t see opendkim.sock, go straight to the logs:

sudo journalctl -u opendkim -n 80 --no-pager

Step 5: Wire OpenDKIM into Postfix (milter settings)

This step is easy to miss. OpenDKIM can be running, keys can be present, and DNS can be correct.

If Postfix never calls the milter, you still get “no DKIM.”

Inspect your active Postfix config:

sudo postconf -n | sed -n '1,160p'

Then edit the file:

sudo nano /etc/postfix/main.cf

Add or update these lines (keep them near any existing milter settings):

# OpenDKIM milter
milter_default_action = accept
milter_protocol = 6
smtpd_milters = unix:/opendkim/opendkim.sock
non_smtpd_milters = unix:/opendkim/opendkim.sock

The socket path matters. Postfix uses a chroot under /var/spool/postfix.

Because of that, the host path /var/spool/postfix/opendkim/opendkim.sock becomes unix:/opendkim/opendkim.sock from Postfix’s point of view.

Reload Postfix:

sudo systemctl reload postfix
sudo systemctl status postfix --no-pager

Check Postfix logs for milter errors:

sudo journalctl -u postfix -n 80 --no-pager

Common mistake: using unix:/var/spool/postfix/... in main.cf. It looks correct, but it’s wrong inside the chroot context.

Step 6: Publish the DKIM DNS TXT record (and avoid formatting traps)

Open the generated record again:

sudo cat /etc/opendkim/keys/example.com/s1.txt

It will look similar to:

s1._domainkey IN TXT ( "v=DKIM1; h=sha256; k=rsa; p=MIIBIjANBgkq..." )

In most DNS panels you’ll enter:

  • Name/Host: s1._domainkey (some panels want the full s1._domainkey.example.com)
  • Type: TXT
  • Value: everything inside the quotes, without the parentheses

Formatting rules that trip people up:

  • Don’t add extra quotes unless your DNS UI requires it. If it does, add one pair only.
  • Don’t introduce spaces or newlines into the key. Some UIs “helpfully” wrap long values and break signatures.
  • Set TTL to 300–3600 seconds while testing; raise it later if you want.

If your DNS is separate from your hosting, keep records organized. HostMyCode’s domains and DNS tools make it easier to keep SPF/DKIM/DMARC next to your other DNS entries.

Step 7: Validate the DKIM DNS record from the server

Give DNS a little time to propagate. It’s often minutes, but it can be longer.

Then query it from your VPS.

If you already have dig:

dig +short TXT s1._domainkey.example.com

If you don’t:

sudo apt install -y dnsutils
dig +short TXT s1._domainkey.example.com

You should get a TXT result containing v=DKIM1; and a long p= value. If the result is empty:

  • Check you added the record in the correct DNS zone (example.com vs a delegated subdomain).
  • Make sure your DNS UI didn’t append the domain twice (creating s1._domainkey.example.com.example.com).
  • Confirm the selector s1 matches what you used on the server.

Step 8: Send a test email and inspect real headers (DKIM=pass)

Don’t guess. Prove it by checking headers in a mailbox that shows authentication results (Gmail, Outlook, Fastmail, etc.).

Install a mail CLI if needed:

sudo apt install -y mailutils

Send a test message (replace the address):

echo "DKIM test from $(hostname)" | mail -s "DKIM test $(date -Is)" you@yourmailbox.com

Open the received message and view “Original” / “Message source”. Look for:

  • DKIM-Signature: header exists
  • Authentication-Results: includes dkim=pass for d=example.com

A healthy result looks like:

Authentication-Results: ...;
  dkim=pass header.i=@example.com header.s=s1 header.b=...

If you see dkim=fail or there’s no DKIM signature, don’t start “fixing” DNS at random.

Start with the milter wiring and socket checks. Then move to the troubleshooting list below.

Step 9: Add SPF and DMARC so DKIM actually improves deliverability

DKIM is only one part of the picture. Receivers score mail using multiple signals.

DMARC ties SPF and DKIM together with alignment rules.

At a minimum, run SPF and DMARC alongside DKIM.

SPF: a sane baseline record

If your mail comes only from this VPS IP, a simple SPF record at example.com is:

v=spf1 ip4:YOUR_SERVER_IP -all

If you also send through a third-party platform (newsletters, transactional mail), add their includes. Use the include: values that provider documents.

DMARC: start with “none” while you verify alignment

Create a TXT record at _dmarc.example.com:

v=DMARC1; p=none; rua=mailto:dmarc-reports@example.com; adkim=s; aspf=s; fo=1

Notes:

  • p=none collects reports without rejecting mail. Once you confirm alignment, move to quarantine and then reject.
  • adkim=s and aspf=s enforce strict alignment (great for clean setups, but only after you’ve validated every sender).

Mail authentication usually sits alongside other operational tasks like relays and application senders.

If WordPress is generating the messages, this companion guide helps you validate the app side too: Fix WordPress not sending email on a VPS.

Troubleshooting: the fixes that resolve most DKIM failures

Most DKIM failures come from a small set of wiring or DNS mistakes. Work through these in order.

You’ll usually find the culprit quickly.

1) No DKIM-Signature header appears

  • Postfix isn’t calling the milter. Re-check smtpd_milters and non_smtpd_milters in /etc/postfix/main.cf:
sudo postconf smtpd_milters non_smtpd_milters milter_protocol
  • Socket path mismatch. Ensure OpenDKIM uses local:/var/spool/postfix/opendkim/opendkim.sock and Postfix uses unix:/opendkim/opendkim.sock.
  • Permissions. Confirm the directory ownership:
sudo ls -ld /var/spool/postfix/opendkim
sudo ls -l /var/spool/postfix/opendkim

You want OpenDKIM to create the socket and Postfix to reach it. The opendkim:postfix ownership pattern usually works well.

2) DKIM exists but shows d=hostname instead of your domain

This usually means the signer is using the wrong domain. The header From (or Sender) isn’t what you think it is.

Check what the application is actually sending. Fix it in the app (WordPress SMTP plugin, application mail settings). If you truly need to rewrite addresses, use Postfix canonical maps.

3) DKIM fails with “bad signature”

  • DNS record doesn’t match the private key. Most often you published the wrong selector, or copied the TXT value incorrectly.
  • Line breaks inserted by the DNS UI. Re-enter the TXT value carefully.
  • Multiple TXT records for the same selector. Some panels allow duplicates; receivers may read the wrong one. Ensure only one TXT exists at s1._domainkey.

Confirm what OpenDKIM thinks it should be using by checking your tables:

sudo grep -n "example.com" /etc/opendkim/KeyTable /etc/opendkim/SigningTable

4) OpenDKIM logs show “not internal” or “not in signing table”

These errors point to TrustedHosts or SigningTable.

  • If your app should be submitting through 127.0.0.1, verify that it really is. Some setups submit from a container network or another host.
  • If mail originates from Docker, it may come from a bridge IP. Prefer submitting via localhost by routing mail through Postfix locally, or add the bridge subnet to TrustedHosts only if you understand the exposure.

Tail OpenDKIM logs while sending a test message:

sudo journalctl -u opendkim -f

5) DKIM passes but inbox placement is still poor

That can be normal for a new sending IP/domain. It can also happen when other trust signals are missing.

  • Make sure SPF is correct and not “+all”.
  • Add DMARC and review reports for alignment problems.
  • Send from a consistent From: and Return-Path.
  • Check rDNS and HELO name. Your server should introduce itself as the PTR hostname.

Protect the box from abuse, too. If an account gets brute-forced and starts blasting spam, your IP reputation will crater fast.

CrowdSec is a practical layer on a VPS: Install CrowdSec on Ubuntu VPS to block bruteforce.

Key rotation and multi-domain setups (optional, but realistic)

Once one domain is signing cleanly, two follow-ups usually matter. You’ll want to rotate keys on a schedule.

You may also need to sign more than one domain on the same host.

Rotate keys without downtime

  1. Generate a new selector (example: s2) and publish its DNS record.
  2. Update KeyTable and SigningTable to point to s2.
  3. Restart OpenDKIM and send tests.
  4. After 3–7 days, remove the old selector DNS record.

Sign multiple domains

Generate a key per domain, then add entries like:

# /etc/opendkim/KeyTable
s1._domainkey.example.com example.com:s1:/etc/opendkim/keys/example.com/s1.private
s1._domainkey.example.net example.net:s1:/etc/opendkim/keys/example.net/s1.private

# /etc/opendkim/SigningTable
*@example.com s1._domainkey.example.com
*@example.net s1._domainkey.example.net

Keep each domain’s keys in its own folder. That makes audits, rotations, and cleanup much less error-prone.

Quick checklist: verify “DKIM is actually working”

  • OpenDKIM is running: systemctl status opendkim
  • Socket exists: /var/spool/postfix/opendkim/opendkim.sock
  • Postfix milters set: postconf smtpd_milters non_smtpd_milters
  • DNS TXT resolves: dig +short TXT s1._domainkey.example.com
  • Real received mail shows: dkim=pass in Authentication-Results

Summary: stable DKIM signing on a real Ubuntu mail server

You installed OpenDKIM, generated a selector and keys, connected the milter to Postfix, published DNS, and verified the outcome in real headers.

That’s the clean path from “DKIM fail” to consistent dkim=pass.

If you want this setup to stay boring (static IP, predictable networking, easy snapshots, full root access), run it on a HostMyCode VPS.

If you don’t want to carry the full load for patching and deliverability tuning, managed VPS hosting is the safer choice for production mail and business sites.

Outbound mail on a VPS comes down to two things: clean DNS records and a correctly wired mail stack. HostMyCode gives you the control you need to run Postfix + DKIM on a clean Linux image. Start with a HostMyCode VPS, or choose managed VPS hosting if you want help keeping it secure, patched, and stable.

FAQ: OpenDKIM on Postfix

Do I need both DKIM and SPF?

Yes. Many receivers evaluate DKIM and SPF together, and DMARC relies on them for alignment. DKIM helps on its own, but it’s not the whole authentication picture.

Should I use RSA or ed25519 keys in 2026?

RSA 2048-bit is still the safest compatibility choice. ed25519 works with many providers now, but not all. If you need maximum interoperability, use RSA 2048 and rotate keys on a schedule.

Why does Postfix use unix:/opendkim/opendkim.sock instead of the full path?

On Ubuntu, Postfix commonly runs SMTP handling in a chroot under /var/spool/postfix. Inside that chroot, /opendkim/... maps to /var/spool/postfix/opendkim/... on the host.

Can I run OpenDKIM on the same server as WordPress?

Yes. It’s common to run Postfix/OpenDKIM on the same Ubuntu VPS as WordPress. Keep the server patched, restrict SMTP, and watch logs so the box doesn’t get abused.

What’s the fastest way to confirm DKIM is passing?

Send a single message to a mailbox that shows raw headers, then check Authentication-Results for dkim=pass and confirm the signing domain (d=) matches your From domain.