Set Up DKIM Email Authentication on Ubuntu VPS: Complete Tutorial

By Raman Kumar

Share:

Updated on May 10, 2026

Set Up DKIM Email Authentication on Ubuntu VPS: Complete Tutorial

Understanding DKIM and Why You Need It

DKIM (DomainKeys Identified Mail) adds a cryptographic signature to your outgoing emails, proving they originated from your domain. Without proper DKIM configuration, your emails land in spam folders or get rejected entirely.

This tutorial walks you through installing and configuring OpenDKIM on Ubuntu VPS. You'll generate keys, update DNS records, and test the complete setup.

Your Hostperl VPS gives you full control over email authentication. Let's get DKIM working properly.

Prerequisites and Server Requirements

Before you set up DKIM email authentication, ensure your VPS meets these requirements:

  • Ubuntu 20.04 or later with root access
  • Working Postfix mail server installation
  • Valid domain with DNS management access
  • At least 1GB RAM for smooth operation

Check your Postfix status first:

sudo systemctl status postfix

If Postfix isn't running, install it using our complete Postfix setup guide.

Installing OpenDKIM on Ubuntu VPS

Update your package list and install OpenDKIM with its tools:

sudo apt update
sudo apt install opendkim opendkim-tools

Create the OpenDKIM directory structure:

sudo mkdir /etc/opendkim
sudo mkdir /etc/opendkim/keys

Set proper ownership and permissions:

sudo chown -R opendkim:opendkim /etc/opendkim
sudo chmod -R 700 /etc/opendkim

Generating DKIM Keys for Your Domain

Create a directory for your domain's keys. Replace 'yourdomain.com' with your actual domain:

sudo mkdir /etc/opendkim/keys/yourdomain.com

Generate the DKIM key pair:

sudo opendkim-genkey -t -s mail -d yourdomain.com
sudo mv mail.private /etc/opendkim/keys/yourdomain.com/
sudo mv mail.txt /etc/opendkim/keys/yourdomain.com/

The '-s mail' parameter sets 'mail' as your selector. The '-t' flag enables test mode initially.

Set correct ownership:

sudo chown opendkim:opendkim /etc/opendkim/keys/yourdomain.com/*

Configuring OpenDKIM Settings

Edit the main OpenDKIM configuration file:

sudo nano /etc/opendkim.conf

Add these configuration lines:

AutoRestart             Yes
AutoRestartRate         10/1h
UMask                   002
Syslog                  yes
SyslogSuccess           Yes
LogWhy                  Yes

Canonicalization        relaxed/simple

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

Mode                    sv
PidFile                 /var/run/opendkim/opendkim.pid
SignatureAlgorithm      rsa-sha256

UserID                  opendkim:opendkim

Socket                  inet:12301@localhost

Create the TrustedHosts file:

sudo nano /etc/opendkim/TrustedHosts

Add these trusted hosts:

127.0.0.1
::1
localhost
yourdomain.com
*.yourdomain.com

Setting Up DKIM Tables

Create the KeyTable file that maps selectors to key files:

sudo nano /etc/opendkim/KeyTable

Add this entry:

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

Create the SigningTable that determines which key signs which emails:

sudo nano /etc/opendkim/SigningTable

Add this pattern:

*@yourdomain.com mail._domainkey.yourdomain.com

This configuration signs all emails from your domain with the 'mail' selector key.

Integrating OpenDKIM with Postfix

Connect Postfix to OpenDKIM by editing the Postfix main configuration:

sudo nano /etc/postfix/main.cf

Add these milter settings at the end:

milter_protocol = 2
milter_default_action = accept
smtpd_milters = inet:localhost:12301
non_smtpd_milters = inet:localhost:12301

Start and enable OpenDKIM:

sudo systemctl start opendkim
sudo systemctl enable opendkim

Restart Postfix to apply the milter configuration:

sudo systemctl restart postfix

Adding DKIM DNS Records

Extract your public key from the generated file:

sudo cat /etc/opendkim/keys/yourdomain.com/mail.txt

You'll see output like this:

mail._domainkey IN TXT "v=DKIM1; h=sha256; k=rsa; t=y; p=MIIBIjANBg...very-long-key...QIDAQAB"

In your DNS management panel, create a TXT record:

  • Name: mail._domainkey
  • Type: TXT
  • Value: v=DKIM1; h=sha256; k=rsa; t=y; p=MIIBIjANBg...QIDAQAB

The 't=y' parameter keeps test mode active. Remove it once testing confirms everything works.

DNS propagation takes 15-60 minutes typically. Check propagation status with:

dig TXT mail._domainkey.yourdomain.com

Email authentication directly impacts deliverability. Our managed VPS hosting includes email support to help you configure DKIM, SPF, and DMARC correctly.

Testing Your DKIM Configuration

Test the OpenDKIM service status:

sudo systemctl status opendkim
sudo journalctl -u opendkim -n 50

Check if OpenDKIM is listening on the correct port:

sudo netstat -tulnp | grep 12301

Send a test email from your server:

echo "DKIM test email" | mail -s "Testing DKIM" test@gmail.com

Check Gmail's message source for the DKIM-Signature header. Look for authentication results showing 'dkim=pass'.

Use online DKIM validators like MXToolbox or Mail-tester.com for comprehensive testing.

Common DKIM Setup Issues and Solutions

Permission errors are the most frequent problem. Verify ownership:

sudo ls -la /etc/opendkim/keys/yourdomain.com/
sudo chown -R opendkim:opendkim /etc/opendkim

If OpenDKIM won't start, check the configuration syntax:

sudo opendkim -t

DNS record formatting causes signature failures. Ensure no extra spaces or line breaks in your TXT record value.

Socket connection issues between Postfix and OpenDKIM show in logs. Verify the socket setting matches in both configurations.

For comprehensive email authentication, implement our complete SPF, DKIM, and DMARC setup guide.

Securing and Maintaining Your DKIM Setup

Remove test mode once verification succeeds. Edit your DNS TXT record to remove 't=y':

v=DKIM1; h=sha256; k=rsa; p=MIIBIjANBg...QIDAQAB

Set up log rotation for OpenDKIM logs:

sudo nano /etc/logrotate.d/opendkim

Add this configuration:

/var/log/mail.log {
    daily
    missingok
    rotate 52
    compress
    delaycompress
    notifempty
    postrotate
        systemctl reload opendkim
    endscript
}

Monitor DKIM signing regularly through your mail logs:

sudo grep "DKIM-Signature" /var/log/mail.log

Regular monitoring keeps your email authentication healthy and maintains good deliverability rates.

Frequently Asked Questions

How long does DKIM DNS propagation take?

DKIM DNS records typically propagate within 15-60 minutes, but can take up to 24 hours globally. Test with dig commands from different locations to verify propagation.

Can I use multiple DKIM selectors for one domain?

Yes, you can create multiple selectors for different services or key rotation. Add additional entries to your KeyTable and SigningTable, then create corresponding DNS records.

What happens if my DKIM private key is compromised?

Generate new keys immediately, update your DNS records, and restart OpenDKIM. The old selector should be removed from DNS after confirming the new key works correctly.

Why are my emails still going to spam with DKIM enabled?

DKIM alone doesn't guarantee inbox delivery. Implement SPF and DMARC records, maintain good sending reputation, and avoid spam-trigger content in your emails.

How do I troubleshoot DKIM signature failures?

Check OpenDKIM logs for errors, verify DNS record formatting, confirm file permissions on key files, and test the milter connection between Postfix and OpenDKIM.