To generate a DKIM key, use your mail provider's built-in generator: Google Workspace creates a 2048-bit key in Admin console > Apps > Google Workspace > Gmail > Authenticate email, and Microsoft 365 generates its keys in the Defender portal under Email authentication settings > DKIM. For your own mail server, run openssl genpkey or opendkim-genkey to create the pair, then publish the public key as a TXT record at selector._domainkey.yourdomain.com.
Key takeaways
- Generate the key where your mail is sent from: Google Admin console, the Microsoft Defender portal, or OpenSSL for self-hosted servers.
- Use a 2048-bit key and a unique selector per sender, and publish the public key as a DNS TXT record before turning signing on.
- Verify with dig and a test message: the Authentication-Results header must show dkim=pass before you enforce DMARC.
DKIM is the signature on your outbound email. Without it, anyone can send mail that claims to come from your domain, and receiving servers have no cryptographic way to tell the difference. Generating the key takes a few minutes — the hard part is knowing where to generate it and what to do with the public half afterward.
What is a DKIM key?
A DKIM key is an RSA key pair: a private key your mail server uses to sign outgoing messages, and a public key you publish in DNS so receiving servers can verify the signature. The private key never leaves your mail system; the public key is visible to the world in a TXT record.
DKIM stands for DomainKeys Identified Mail, defined in RFC 6376. When your server sends a message, it hashes selected headers and the body, signs that hash with the private key, and adds a DKIM-Signature header. The receiving server fetches your public key from DNS, checks the signature, and knows the message was not altered in transit. If you want the background first, read our guide to what DKIM is and how it works.
A key pair has three parts you will keep meeting:
- Private key — held by your mail provider or server, used to sign. Guard it like a password.
- Public key — published in DNS as a TXT record, used by receivers to verify.
- Selector — a short label (for example
googleors1) that tells receivers which public key to look up.
How does DKIM signing work?
Signing happens in four steps: your server hashes the message, signs the hash with the private key, attaches the signature as a header, and the receiver verifies it against the public key in your DNS. If any signed part changes in transit, verification fails.
The signature header itself carries the details a receiver needs: d= is the signing domain, s= is the selector, a=rsa-sha256 is the algorithm, bh= is the hash of the body, and b= is the signature itself. A typical header also names the canonicalization method, usually c=relaxed/relaxed, which tolerates harmless whitespace changes made by mail relays.
DKIM does not say whether a message is wanted or safe. It only proves the message came from the domain in the d= field and was not changed after signing. That proof is what makes DMARC enforcement possible: DMARC passes when DKIM passes and the signing domain aligns with the From address.
How do I generate a DKIM key in Google Workspace?
In Google Workspace, generate the key in the Admin console under Apps, Google Workspace, Gmail, Authenticate email: pick your domain, click Generate New Record, choose 2048-bit and the default selector, then copy the TXT record value to your DNS host.
You must be signed in as a super administrator, and you need the Gmail Settings administrator privilege. Google notes one timing trap: after you turn on Gmail for your organization, wait 24 to 72 hours before generating the key, or you may get an error saying the DKIM record was not created. The full procedure is in Google Workspace Help on setting up DKIM.
- Open the Google Admin console and go to Menu, Apps, Google Workspace, Gmail.
- Click Authenticate email and select the domain in the Selected domain menu.
- Click Generate New Record.
- Choose a key length of 2048 if your domain host supports it, otherwise 1024, and keep the default prefix selector
googleunless you have a reason to change it. - Click Generate. The page shows a TXT record value and the message "DKIM authentication settings updated".
- Copy the DNS host name (
google._domainkey) and the long TXT value, and add them as a TXT record at your domain provider. - Wait for DNS to propagate, return to the same page, and click Start Authentication. Google may keep showing "You must update the DNS records for this domain" for up to 48 hours while it checks.
Google generates the key pair for you and keeps the private key. You never see it, which is the point of a hosted service: there is nothing to store, back up, or leak from your side. If you manage several domains, repeat the process per domain — each one gets its own key and record.
How do I generate a DKIM key in Microsoft 365?
In Microsoft 365, generate the key in the Microsoft Defender portal under Email and collaboration, Policies and rules, Threat policies, Email authentication settings, DKIM: select your custom domain, copy the two CNAME records Microsoft shows, publish them in DNS, then switch signing on.
Your initial onmicrosoft.com domain is already DKIM-signed by Microsoft, so these steps only apply to custom domains you send from. Each subdomain you send from needs its own DKIM configuration.
- Sign in to the Microsoft Defender portal at security.microsoft.com with a Microsoft 365 administrator account.
- Go to Email & collaboration, Policies & rules, Threat policies, Email authentication settings, then open the DKIM tab.
- Select your custom domain. Microsoft displays two CNAME records, normally
selector1._domainkeyandselector2._domainkey, pointing at tenant-specific targets ending indkim.mail.microsoft. - Create both CNAME records at your DNS host, copying the targets exactly as shown. Do not type them from memory or shorten them.
- Wait for DNS propagation, which can take minutes to a few hours, then return to the DKIM page and turn on "Sign messages for this domain with DKIM signatures".
- Send a test message and confirm the header shows
dkim=pass.
Microsoft uses CNAME records instead of TXT records because it holds the keys and rotates them for you. Microsoft Learn documents the full DKIM setup, including the PowerShell route. If you prefer scripting, the same job can be done in Exchange Online PowerShell with New-DkimSigningConfig and Set-DkimSigningConfig; check the current state first with Get-DkimSigningConfig. When creating keys in PowerShell, specify 2048-bit explicitly, since the cmdlet default is the weaker 1024-bit size.
How do I generate a DKIM key with OpenSSL?
For a self-hosted mail server, generate the pair with OpenSSL: run openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 -out dkim-private.pem, then extract the public key with openssl pkey -in dkim-private.pem -pubout -out dkim-public.pem.
That gives you a 2048-bit RSA private key and its matching public key in PEM format. Keep the private key on the mail server with strict permissions — readable only by the mail service account — and never publish it, email it, or commit it to a repository. If you run OpenDKIM, its opendkim-genkey tool does the same job and also prints a ready-to-paste DNS record: opendkim-genkey -b 2048 -d example.com -s s1 writes the private key and a s1.txt file containing the record.
To build the DNS value from the public key, strip the PEM header, footer, and line breaks from the public key file so you have one continuous base64 string, then wrap it as v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3.... The v=DKIM1 tag identifies the record version, k=rsa names the key type, and p= carries the public key itself.
Configure your mail software to sign with the private key and the selector you chose — in OpenDKIM this is the KeyTable and SigningTable; in Postfix with OpenDKIM it is the milter socket. Restart the mail service, send a test message, and verify before you tell anyone the job is done.
How do I publish the DKIM public key in DNS?
Publish the public key as a TXT record whose name is your selector plus ._domainkey plus your domain — for example google._domainkey.example.com — with the value starting v=DKIM1; k=rsa; p= followed by the base64 public key.
Watch out for three common DNS mistakes. First, some registrars split long TXT values into 255-character strings; a 2048-bit key is about 390 characters of base64, so your provider must either accept the full string or let you enter it as two quoted strings, which DNS treats as one value.
Second, some DNS editors add your domain to the host name automatically, so entering google._domainkey.example.com in full can create google._domainkey.example.com.example.com — enter only google._domainkey if your provider appends the domain. Third, never paste the private key into DNS. If the value starts with -----BEGIN PRIVATE KEY-----, stop: you have the wrong file, and that key must be replaced.
After publishing, check the record from the command line with dig TXT google._domainkey.example.com +short, replacing the selector and domain with your own. The answer should be your full v=DKIM1... value. If dig returns nothing, the record is missing, misspelled, or not yet propagated.
How do I verify DKIM is working?
Verify in two places: check the DNS record with dig, then send a test message to a Gmail address and inspect the headers for dkim=pass in the Authentication-Results header. Both checks must pass before you enforce DMARC.
In Gmail, open the test message, click the three dots, and choose Show original. Near the top you will see the SPF, DKIM, and DMARC results. You want all three to read PASS, with the DKIM line showing your domain. Our free DKIM checker can also test your published record without sending mail.
If the test fails, work through the likely causes in order: the DNS record has not propagated yet, the selector in the signature does not match the selector in DNS, the record was truncated when pasted, or something between your server and the receiver modified the message after signing — a common culprit is an email signature or disclaimer added downstream of the signing step. Sign after any content is added, not before.
| Symptom | Likely cause | Fix |
|---|---|---|
dkim=fail (bad signature) | Message changed after signing | Move signing after signatures and disclaimers are added |
dkim=fail (no key) | DNS record missing or wrong selector | Check the record name with dig; fix the selector |
dkim=neutral | No signature on the message | Signing is off for this domain or sender |
| Record not found in dig | DNS not propagated or wrong host name | Wait, then check for a doubled domain suffix |
| Record truncated | 255-character DNS string limit | Split the key into two quoted strings |
How do I rotate a DKIM key?
Rotate by generating a new key under a new selector, publishing it alongside the old one, switching signing to the new selector, and removing the old record only after a few days. Never delete the old record before the new one is live.
The overlap matters because mail already in transit was signed with the old key. A safe rotation looks like this: generate the new key with selector s2 while s1 still signs, publish s2._domainkey, wait for DNS propagation, switch your server to sign with s2, wait at least a week, then delete the s1 record. In Google Workspace and Microsoft 365, the provider handles rotation for you — Microsoft rotates its managed keys automatically — but if you generated keys yourself, rotation is your job.
Rotate at least once a year, and immediately if a private key may have leaked, an employee with access leaves under bad terms, or you migrate mail providers. Treat a leaked private key like a leaked password: anyone holding it can sign mail as your domain until the record is removed.
What mistakes should I avoid?
The most expensive mistake is turning on DMARC enforcement before DKIM passes, which can send your legitimate mail to spam or get it rejected. Verify dkim=pass first, then move DMARC from p=none to quarantine and finally to reject.
- One key for every sender. Third-party services that send mail as you — marketing platforms, help desks, invoicing tools — each need their own DKIM setup for your domain, or their mail will fail DMARC alignment.
- Sharing selectors between providers. If two systems use the same selector, rotating one breaks the other. Give each sender its own selector.
- Forgetting subdomains. Mail from
mail.example.comneeds its own DKIM configuration; the parent domain's record does not cover it. - Publishing the private key. It happens more often than anyone admits. If it does, generate a fresh pair immediately.
- Using 1024-bit keys indefinitely. They still work, but 2048-bit is the current standard and costs nothing extra.
Once DKIM is passing, finish the trio: confirm SPF is published and check your DMARC policy with our DMARC checker.
Our guide to what DMARC is walks through the safe path to p=reject.
The bottom line
Generating a DKIM key is a ten-minute job in Google Workspace or Microsoft 365, and a few commands with OpenSSL on your own server. The key itself is the easy part — the discipline is publishing the record correctly, verifying dkim=pass on a real message, and rotating the key on a schedule.
Do that, and your domain carries a cryptographic signature that receivers can trust, spoofers cannot forge, and DMARC can enforce. Check your setup in seconds with the free DKIM checker.
Frequently asked questions
How do I generate a DKIM key for free?
Use your mail provider's built-in generator, which costs nothing: Google Workspace and Microsoft 365 both generate keys in their admin consoles. For a self-hosted server, OpenSSL is free and installed on most Linux systems. There is no need to buy a key from anyone.
Should I use a 1024-bit or 2048-bit DKIM key?
Use 2048-bit if your DNS host supports it, because 1024-bit RSA is considered weak by modern standards. If your DNS provider cannot store the longer record, use 1024-bit rather than skipping DKIM entirely, and plan to move to a provider that supports 2048-bit.
What is a DKIM selector?
A selector is a short label, such as google or s1, that identifies which key signed a message. It appears in the DKIM-Signature header as s= and in DNS as selector._domainkey.yourdomain.com. Selectors let you run several keys at once, which is how key rotation works.
How long does it take for a DKIM record to work?
DNS changes usually propagate within minutes to a few hours, but Google advises waiting 24 to 72 hours after enabling Gmail before generating a key, and up to 48 hours for the record to be recognized. Send a test message and check for dkim=pass before relying on it.
Do I need DKIM if I already have SPF?
Yes. SPF only checks the envelope sender and breaks when mail is forwarded, while DKIM signs the message itself and survives forwarding. DMARC needs at least one of them to pass with alignment, and DKIM is the more reliable of the two, so set up both.
Can I use the same DKIM key for multiple domains?
No. Each domain needs its own key pair and its own DNS record, because the public key is published under that domain's DNS. If you send from subdomains, each subdomain that sends mail needs its own DKIM configuration too.
How often should I rotate my DKIM keys?
Rotate at least once a year, and immediately if you suspect the private key leaked. Rotation means generating a new key under a new selector, publishing it, switching signing to it, and only then removing the old record after a few days.
Why does my DKIM check fail with a valid record?
The usual causes are a copied record with missing characters, a 2048-bit key split incorrectly across DNS strings, signing the wrong domain, or a mail flow that modifies the message after signing, such as an email signature added downstream. Check the exact record with dig and compare it character by character.
Sources
The engineers and analysts who build Cysoro's domain scanner, email-authentication checks and free security tools. We test what we write about every day — SPF, DKIM, DMARC, certificates, look-alike domains and phishing — and check each guide against the relevant standards, government guidance and vendor documentation.
Checked against RFC 6376, Google Workspace Help and Microsoft Learn DKIM documentation.
How we write and fact-check →