You migrated your mail server to new hardware or a new provider, DNS records look fine, SPF passes, but DKIM validation fails on every outbound message. Gmail shows dkim=fail in the Authentication-Results header. Nothing changed in your DNS zone — and that is exactly the problem. The new server is signing with a private key that does not match the public key still sitting in your DNS TXT record, or it is using a different selector entirely.
This guide walks through the CLI checks to find the mismatch, confirm what your new server is actually signing with, and verify the fix.
How DKIM Signing and Validation Work Together
Quick refresher. When your mail server sends a message, it adds a DKIM-Signature header. That header contains (among other things):
d=— the signing domain (e.g.,example.com)s=— the selector (e.g.,default,mail,s1,20230601)b=— the actual cryptographic signaturebh=— a hash of the message body
The receiving server takes the selector and domain, queries DNS for selector._domainkey.example.com, retrieves the public key from the TXT record, and uses it to verify the signature. If the public key in DNS does not match the private key that signed the message, validation fails. Period.
During a migration, three things commonly go wrong:
- The new server generates a new key pair, but nobody updated DNS with the new public key.
- The new server uses a different selector name, and DNS still only has the old selector.
- DNS was updated, but the TXT record got truncated or mangled (especially with long 2048-bit keys split across multiple strings).
Step 1: Read the DKIM-Signature Header From a Real Message
Send a test email from the new server to an account you control (Gmail works well for this). Open the raw message source — in Gmail, click the three dots next to the reply button and choose "Show original."
Look for the DKIM-Signature header. You need two values from it:
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=example.com; s=mail2024;
h=from:to:subject:date:message-id;
bh=abcdef1234...;
b=GHIJKL5678...
Note the s= value (mail2024 in this illustrative example) and the d= value (example.com). These tell you exactly which DNS record the receiving server will query: mail2024._domainkey.example.com.
Also check the Authentication-Results header added by the receiving server. It typically says something like dkim=fail (body hash did not verify) or dkim=fail (no key for signature). The reason in parentheses matters — "no key" means DNS lookup returned nothing; "body hash did not verify" means the key was found but the signature did not match.
For more on reading these headers, see Reading Authentication-Results Headers to Fix DMARC Alignment.
Step 2: Query DNS for the Selector Your Server Is Using
Now query DNS for the exact selector from Step 1:
dig TXT mail2024._domainkey.example.com +short
Expected result: a TXT record containing v=DKIM1; k=rsa; p=MIIBIjANBgkq... (the public key).
Possible bad outcomes:
- Empty response or NXDOMAIN. DNS has no record for this selector. The new server is signing with a selector that was never published. This is the most common post-migration failure.
- Record exists but uses the old key. The selector name happens to match, but the public key belongs to the old server's private key. Signature verification will fail.
- Record is truncated. Some DNS control panels silently cut TXT records longer than 255 characters. A 2048-bit RSA public key is roughly 400 characters and must be split into multiple quoted strings within a single TXT record. If the panel dropped the second half, the key is invalid.
If your provider uses CNAME-based DKIM (common with hosted email platforms), the selector record might be a CNAME pointing to the provider's infrastructure. Check with:
dig CNAME mail2024._domainkey.example.com +short
If a CNAME exists, follow it and verify the target record actually resolves to a valid DKIM key.
Step 3: Confirm the Private Key on the New Server Matches the DNS Public Key
This step requires SSH access to the mail server. The goal: extract the public key from the private key file on disk, then compare it to what DNS returns.
Where the private key lives depends on your MTA (mail transfer agent):
- Exim with cPanel/WHM: typically under
/var/cpanel/domain_keys/with one file per domain. - Postfix with OpenDKIM: the key path is defined in the
KeyTablefile, often under/etc/opendkim/keys/. - Exim with manual DKIM: check the Exim configuration for
dkim_private_key.
Once you find the private key file, extract its public key:
openssl rsa -in /path/to/private.key -pubout -outform PEM 2>/dev/null
This outputs the public key in PEM format. The base64-encoded portion (between the BEGIN and END lines) should match the p= value in your DNS TXT record, ignoring whitespace and line breaks.
If they do not match, you have a key mismatch. Either:
- Update DNS with the public key that corresponds to the private key the new server is using, or
- Replace the private key on the server with one that matches what DNS already publishes (less common, since you typically do not have the old private key after migration).
Step 4: Check for Leftover Old Selectors
If the old server used a selector like default and the new server uses mail2024, both records might exist in DNS. That is fine — old selectors do not cause failures on their own. But if the new server was accidentally configured to sign with the old selector name while using a new private key, validation breaks.
Query both selectors and compare:
dig TXT default._domainkey.example.com +shortdig TXT mail2024._domainkey.example.com +short
Make sure the selector in the outbound DKIM-Signature header matches a DNS record whose public key corresponds to the signing private key. That is the entire chain.
Once the old server is fully decommissioned and no messages signed with the old selector are still in transit (give it a few days), you can remove the old selector record from DNS. No rush — an unused selector record is harmless.
Step 5: Verify DMARC Alignment
DKIM passing is not enough if DMARC alignment fails. DMARC requires that the d= domain in the DKIM signature matches (or is a subdomain of) the From: header domain. If your new server signs as d=mail.example.com but your From address is user@example.com, DKIM passes but DMARC alignment fails unless your DMARC record uses adkim=r (relaxed mode, which allows subdomain matching).
Check your DMARC record:
dig TXT _dmarc.example.com +short
Look at the adkim= tag. If it says adkim=s (strict), the DKIM d= domain must exactly match the From domain. If it says adkim=r or is absent (relaxed is the default), subdomain matching is allowed.
If you are also sending through third-party services (marketing platforms, helpdesks), each one needs its own DKIM selector published in your DNS. Otherwise those messages will fail DKIM or fail DMARC alignment. See Added a New CRM or Helpdesk? Verify It Signs DKIM With Your Own Domain for that scenario.
Step 6: Send a Test and Confirm the Full Chain
After updating DNS, wait for TTL expiry (check the TTL value in your zone — typically anywhere from 300 to 86400 seconds). Then send another test message and re-examine the Authentication-Results header. You want to see:
dkim=passwith the correct selector and domaindmarc=passwith alignment confirmedspf=pass(separate from DKIM, but part of the overall authentication picture)
You can also use external tools like MxToolbox DKIM Lookup to query your selector record from outside your network, which rules out local DNS caching issues.
The Short Version
Every DKIM failure after migration comes down to one question: does the private key on the new server have its matching public key published in DNS under the correct selector name? Send a message, read the s= and d= values from the DKIM-Signature header, query that exact selector in DNS, and compare the public key to what the server's private key produces. Fix whichever side is wrong, wait for DNS propagation, and test again. The whole check takes five minutes once you know what to look for.