What you'll learn
- • What DKIM is and how digital signatures protect your emails
- • How to generate DKIM public/private key pairs
- • Understanding DKIM selectors and rotation
- • Step-by-step DNS configuration for DKIM
- • How to verify DKIM is working correctly
What is DKIM?
DKIM (DomainKeys Identified Mail) is an email authentication method that uses cryptographic signatures to verify that an email hasn't been altered in transit and was actually sent from your domain.
When you send an email, your mail server adds a digital signature to the email header using a private key. Receiving servers verify this signature using your public key published in DNS. If the signature is valid and the content hasn't changed, the email passes DKIM authentication.
How DKIM Works
Email is sent from your server
Your mail server signs the email using your private DKIM key
Signature is added to email header
A DKIM-Signature header is added containing the encrypted hash
Receiving server retrieves public key
The receiving server looks up your DKIM public key from DNS
Signature is verified
If the signature matches and content is unchanged, DKIM passes
Understanding DKIM Selectors
A DKIM selector is a unique identifier for your DKIM key pair. It allows you to have multiple DKIM keys for different purposes or to rotate keys without disrupting email flow.
Example DKIM DNS record format:
[selector]._domainkey.yourdomain.comCommon selectors:
- •
default._domainkey.yourdomain.com - •
google._domainkey.yourdomain.com(Google Workspace) - •
s1._domainkey.yourdomain.com(SendGrid) - •
k1._domainkey.yourdomain.com(Mailgun)
Step 1: Generate DKIM Keys
The method for generating DKIM keys depends on your email provider or mail server:
Google Workspace
- Sign in to Google Admin console
- Go to Apps → Google Workspace → Gmail → Authenticate email
- Click "Generate new record" under DKIM authentication
- Choose prefix length (2048-bit recommended)
- Copy the DNS TXT record values provided
Microsoft 365
- Sign in to Microsoft 365 Defender portal
- Go to Email & collaboration → Policies & rules → Threat policies
- Click on DKIM
- Select your domain and click "Create DKIM keys"
- Copy the two CNAME records provided (selector1 and selector2)
Using OpenSSL (Self-Hosted)
For self-hosted mail servers, generate keys using OpenSSL:
# Generate private key
openssl genrsa -out dkim_private.pem 2048
# Generate public key
openssl rsa -in dkim_private.pem -pubout -out dkim_public.pem
# Format public key for DNS (remove headers and newlines)
tr -d '\n' < dkim_public.pem | sed 's/-----BEGIN PUBLIC KEY-----//' | sed 's/-----END PUBLIC KEY-----//'Security Warning
NEVER share your private key. The private key should only be stored on your mail server with restricted file permissions (chmod 600). Only the public key is published in DNS.
Step 2: Add DKIM Record to DNS
The format of your DKIM DNS record varies by provider:
TXT Record Format
Hostname:
default._domainkey.yourdomain.comRecord Type:
TXTValue:
v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA...Breaking it down:
- •
v=DKIM1- DKIM version (required) - •
k=rsa- Key type (RSA) - •
p=...- Your public key (base64 encoded)
Common Provider-Specific Examples
Google Workspace
Hostname:
google._domainkey.yourdomain.comValue:
v=DKIM1; k=rsa; p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A...Microsoft 365
Two CNAME records required:
selector1._domainkey.yourdomain.com→ CNAME to selector1-yourdomain-com._domainkey.yourdomain.onmicrosoft.com
selector2._domainkey.yourdomain.com→ CNAME to selector2-yourdomain-com._domainkey.yourdomain.onmicrosoft.com
SendGrid
Three CNAME records:
s1._domainkey.yourdomain.coms2._domainkey.yourdomain.comem[number].yourdomain.comDNS Propagation
DKIM DNS changes can take up to 48 hours to propagate globally, though most providers update within 15-30 minutes. You can verify propagation using DNS lookup tools.
Step 3: Configure Your Mail Server
After publishing your DKIM public key in DNS, configure your mail server to sign outgoing emails:
Google Workspace
- After adding DNS records, return to Google Admin console
- Click "Start authentication" button
- Google will verify your DNS records automatically
- DKIM signing will begin within 24-48 hours
Microsoft 365
- After adding CNAME records, return to DKIM settings
- Toggle "Sign messages for this domain with DKIM signatures" to On
- Microsoft will verify DNS records automatically
- DKIM signing will start immediately after verification
Postfix (Self-Hosted)
Install and configure OpenDKIM:
# Install OpenDKIM
apt-get install opendkim opendkim-tools
# Configure in /etc/opendkim.conf
Domain yourdomain.com
KeyFile /etc/opendkim/keys/dkim_private.pem
Selector default
Socket inet:8891@localhostStep 4: Verify DKIM is Working
After configuration, verify that DKIM is properly signing your emails:
Send a Test Email
- Send a test email to yourself or [email protected]
- View the email's raw source/headers
- Look for a
DKIM-Signatureheader - Verify the signature shows
d=yourdomain.com
Best Practices
1. Use 2048-bit Keys
While 1024-bit keys are still supported, 2048-bit keys provide better security and are recommended by most email providers.
2. Rotate DKIM Keys Regularly
Rotate your DKIM keys every 6-12 months. Use multiple selectors to enable smooth key rotation without disrupting email flow.
3. Sign All Outgoing Email
Configure your mail server to sign all outgoing email, not just marketing or transactional emails. This provides maximum protection.
4. Monitor DKIM Failures
Enable DMARC reporting to monitor DKIM authentication failures and identify configuration issues or unauthorized sending sources.
Troubleshooting
DKIM Record Not Found
- Verify the selector name matches your mail server configuration
- Check that the hostname includes
._domainkey. - Wait 15-30 minutes for DNS propagation
- Use
dig TXT selector._domainkey.yourdomain.comto verify
DKIM Signature Verification Failed
- Ensure your public key in DNS matches the private key on your server
- Check that the key is properly formatted (no line breaks or extra spaces)
- Verify the selector in your mail server config matches DNS
- Confirm your mail server's DKIM signing service is running
DNS Record Too Long
- Some DNS providers don't support long TXT records
- Split the public key into multiple strings in the TXT record
- Format:
v=DKIM1; k=rsa; p="string1" "string2" - Alternatively, use a 1024-bit key (less secure but shorter)
Next Steps
Once DKIM is configured, complete your email authentication by setting up SPF (if not done) and DMARC to tie everything together.