Ubuntu 26.04

Mail Server : Set DKIM2026/06/08

 

Configure DKIM (Domain Keys Identified Mail) in Postfix.

In order to register the generated public key in DNS, you will need the DNS server that registers your email domain.

[1] Install and configure OpenDKIM.
root@mail:~#
apt -y install opendkim opendkim-tools
# create a directory for the domain you configure DKIM for

root@mail:~#
mkdir /etc/dkimkeys/srv.world
# generate a key pair
# -D (directory in which to store keys)
# -d (domain name)
# -s (selector name) ⇒ any name is OK

root@mail:~#
opendkim-genkey -D /etc/dkimkeys/srv.world -d srv.world -s $(date "+%Y%m%d")

root@mail:~#
chown -R opendkim:opendkim /etc/dkimkeys/srv.world

root@mail:~#
ll /etc/dkimkeys/srv.world

otal 16
drwxr-xr-x 2 opendkim opendkim 4096 Jun  8 02:06 ./
drwx------ 3 opendkim opendkim 4096 Jun  8 02:06 ../
-rw------- 1 opendkim opendkim 1704 Jun  8 02:06 20260608.private
-rw------- 1 opendkim opendkim  507 Jun  8 02:06 20260608.txt

root@mail:~#
vi /etc/opendkim.conf
# line 14,15 : uncomment
# Mode : s = sign
# Mode : v = verify
Mode                    sv
SubDomains              no

# line 22,23,24 : if you are handling one domain,
# uncomment the following three lines and change the values ​​of each
# if you want to handle multiple domains, set it with different parameters
#Domain                 example.com
#Selector               2020
#KeyFile                /etc/dkimkeys/example.private

# line 37 : comment out
# line 38 : uncomment
#Socket                 local:/run/opendkim/opendkim.sock
Socket                  inet:8891@localhost

# line 50 : comment out if you do not have DNSSEC enabled on DNS server
#TrustAnchorFile        /usr/share/dns/root.key

# line 51 : uncomment and change to the DNS server you will register the DKIM key
Nameservers            10.0.0.30

# add to last line
# if you are handling multiple domains, set the following
# if you have only one domain, set lines 22-24 and the following is not necessary
KeyTable                /etc/dkimkeys/KeyTable
SigningTable            refile:/etc/dkimkeys/SigningTable
ExternalIgnoreList      refile:/etc/dkimkeys/TrustedHosts
InternalHosts           refile:/etc/dkimkeys/TrustedHosts

root@mail:~#
vi /etc/dkimkeys/KeyTable
# create new file
#
# (selector name)._domainkey.(domain name) (domain name):(selector name):(Private Key Path)
#
# if you are handling multiple domains, enter them in the same way

20260608._domainkey.srv.world srv.world:20260608:/etc/dkimkeys/srv.world/20260608.private

root@mail:~#
vi /etc/dkimkeys/SigningTable
# create new file
#
# *@(domain name) (selector name)._domainkey.(domain name)
#
# if you are handling multiple domains, enter them in the same way

*@srv.world 20260608._domainkey.srv.world

root@mail:~#
vi /etc/dkimkeys/TrustedHosts
# create new file
# add trusted hosts
127.0.0.1
::1
localhost

root@mail:~#
chown opendkim:opendkim /etc/dkimkeys/{KeyTable,SigningTable,TrustedHosts}

root@mail:~#
chmod 600 /etc/dkimkeys/{KeyTable,SigningTable,TrustedHosts}

root@mail:~#
systemctl restart opendkim

[2] Configure Postfix.
root@mail:~#
vi /etc/postfix/main.cf
# add to last line
smtpd_milters = inet:127.0.0.1:8891
non_smtpd_milters = $smtpd_milters
milter_default_action = accept

root@mail:~#
systemctl reload postfix

[3] Verify the public key for the DNS server registration.
# public key contents

root@mail:~#
cat /etc/dkimkeys/srv.world/20260608.txt

20260608._domainkey     IN      TXT     ( "v=DKIM1; h=sha256; k=rsa; "
          "p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzrfRtIcAbIPPtBklanol1IpyAuR2eLIOaeNibvku4IaHsbG038IzRjo++BsmG7V7EYCAfHwKze9DRtScVyOKPjM6WgoFMHWVcESdZtQEoMROpGkLbfPg2RQp2bfj85OpgZ79IjuXo/UtwDGIHbzjzII+Gn+OZByppYstT2xQOSe1Qd7bXbjbeWWZMV1klHbnse1LsA/3725HEX"
          "dwsIn8DS6WVF499r5XhoFGPAGnkDDm4R9vtvuYCV+1qj4dvKPCqNwmPhGrygWmu6LUpUtmyYg6SOL/GzWt/ST5QbO1FsdltsC2A6Nr4eyImQdfigxnJtOGZFjyUusMCwA8ugIi5QIDAQAB" )  ; ----- DKIM key 20260608 for srv.world

# the entry in the zone file should be on one line, excluding unnecessary characters

root@mail:~#
sed "s/^\t *//g" /etc/dkimkeys/srv.world/20260608.txt | sed -z "s/\\n//g" | sed "s/( //g" | cut -d')' -f1

20260608._domainkey     IN      TXT     "v=DKIM1; h=sha256; k=rsa; ""p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzrfRtIcAbIPPtBklanol1IpyAuR2eLIOaeNibvku4IaHsbG038IzRjo++BsmG7V7EYCAfHwKze9DRtScVyOKPjM6WgoFMHWVcESdZtQEoMROpGkLbfPg2RQp2bfj85OpgZ79IjuXo/UtwDGIHbzjzII+Gn+OZByppYstT2xQOSe1Qd7bXbjbeWWZMV1klHbnse1LsA/3725HEX""dwsIn8DS6WVF499r5XhoFGPAGnkDDm4R9vtvuYCV+1qj4dvKPCqNwmPhGrygWmu6LUpUtmyYg6SOL/GzWt/ST5QbO1FsdltsC2A6Nr4eyImQdfigxnJtOGZFjyUusMCwA8ugIi5QIDAQAB"
[4] Register the public key on the DNS server.
It will use the example of registering to a BIND zone file.
root@dns:~#
vi /etc/bind/srv.world.wan
.....
.....
# add to last line
20260608._domainkey     IN      TXT     "v=DKIM1; h=sha256; k=rsa; ""p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzrfRtIcAbIPPtBklanol1IpyAuR2eLIOaeNibvku4IaHsbG038IzRjo++BsmG7V7EYCAfHwKze9DRtScVyOKPjM6WgoFMHWVcESdZtQEoMROpGkLbfPg2RQp2bfj85OpgZ79IjuXo/UtwDGIHbzjzII+Gn+OZByppYstT2xQOSe1Qd7bXbjbeWWZMV1klHbnse1LsA/3725HEX""dwsIn8DS6WVF499r5XhoFGPAGnkDDm4R9vtvuYCV+1qj4dvKPCqNwmPhGrygWmu6LUpUtmyYg6SOL/GzWt/ST5QbO1FsdltsC2A6Nr4eyImQdfigxnJtOGZFjyUusMCwA8ugIi5QIDAQAB"

root@dns:~#
rndc reload
[5] Check on the mail server side.
root@mail:~#
dig 20260608._domainkey.srv.world. txt

.....
.....

# if the response matches what you registered, that's OK
;; ANSWER SECTION:
20260608._domainkey.srv.world. 86400 IN TXT     "v=DKIM1; h=sha256; k=rsa; " "p=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzrfRtIcAbIPPtBklanol1IpyAuR2eLIOaeNibvku4IaHsbG038IzRjo++BsmG7V7EYCAfHwKze9DRtScVyOKPjM6WgoFMHWVcESdZtQEoMROpGkLbfPg2RQp2bfj85OpgZ79IjuXo/UtwDGIHbzjzII+Gn+OZByppYstT2xQOSe1Qd7bXbjbeWWZMV1klHbnse1LsA/3725HEX" "dwsIn8DS6WVF499r5XhoFGPAGnkDDm4R9vtvuYCV+1qj4dvKPCqNwmPhGrygWmu6LUpUtmyYg6SOL/GzWt/ST5QbO1FsdltsC2A6Nr4eyImQdfigxnJtOGZFjyUusMCwA8ugIi5QIDAQAB"

.....
.....

root@mail:~#
opendkim-testkey -d srv.world -s 20260608 -vvv

opendkim-testkey: using default configfile /etc/opendkim.conf
opendkim-testkey: checking key '20260608._domainkey.srv.world'
opendkim-testkey: key not secure
opendkim-testkey: key OK
# If [key OK], that's OK
# * [key not secure] is a message about DNSSEC
[6]

Finally, send an email to Gmail and if the header of the received email shows [DKIM: 'PASS' (Domain: srv.world)], then everything is OK.

Matched Content