Crmgo Blog

DKIM + Postfix + CentOS 7 (digital signature for email)

DKIM + Postfix + CentOS 7 (digital signature for email)

server:/ # apt-get install opendkim opendkim-tools Create a separate folder for OpenDKIM and move the configuration file to this folder. For compatibility, leave a link in the previous location. 1 2 3 4 server:/ # mkdir /etc/opendkim server:/ # chown opendkim:opendkim /etc/opendkim server:/ #…

DKIM + Postfix + CentOS 7 (digital signature for email)
server:/# apt-get install opendkim opendkim-tools

Create a separate folder for OpenDKIM and move the configuration file to this folder. For compatibility, leave a link in the previous location.

1
2
3
4
server:/# mkdir /etc/opendkim
server:/# chown opendkim:opendkim /etc/opendkim
server:/# mv /etc/opendkim.conf /etc/opendkim/opendkim.conf
server:/# ln -s /etc/opendkim/opendkim.conf /etc/opendkim.conf

Edit the configuration and add the necessary parameters:

PidFile /var/run/opendkim/opendkim.pid
Syslog yes
UserID opendkim:opendkim
Socket inet:8891@localhost
Umask 002
SendReports yes
SoftwareHeader yes
MinimumKeyBits 1024
SyslogSuccess yes
LogWhy yes
X-Header yes
OversignHeaders From
Selector mail
Canonicalization relaxed/relaxed
Mode sv
SubDomains no
KeyTable file:/etc/opendkim/KeyTable
SigningTable file:/etc/opendkim/SigningTable
AutoRestart yes
DNSTimeout 5

ExternalIgnoreList file:/etc/opendkim/TrustedHosts
InternalHosts file:/etc/opendkim/TrustedHosts
#Statistics /var/log/dkim-stats

Create the necessary files and folders (inventory) for our mail server, personally... In the future, each new client will be added in a similar manner.

1
2
server:/# mkdir /etc/opendkim/demon.of.by
server:/# chown opendkim:opendkim /etc/opendkim/demon.of.by

Create keys directly for the domain demon.of.by and also change the owner of the keys to the opendkim user:

1
2
3
server:/# cd /etc/opendkim/demon.of.by
server:/# opendkim-genkey -s mail -d demon.of.by
server:/# chown opendkim:opendkim *

Update DNS - configure DKIM. This part is probably the most laborious, as you must understand how and where to change DNS records for your domain.

1
2
3
server:/# cat /etc/opendkim/demon.of.by/mail.txt
mail._domainkey IN TXT "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDH5t3M0b3DQEBAQUAA4EWRL+S4/yWauDq6C2srD9UptvXdI40YS0aUywyj2XT1TbeNdrLjGZ764kTUwshuqKToa9pUjwT/TPOx939VA8FWV32bC1hE97dgK3mhFpNurp7K3ZH4sDPTz4Qnlpa8kWZ/iThkTPpNYf5LTwIDAQAB" ; ----- DKIM key mail for demon.of.by

Take the content in quotes from here and place it in the TXT record of our domain in DNS:

1
mail._domainkey.demon.of.by. IN TXT "v=DKIM1; k=rsa; p=MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDH5t3M0b3DQEBAQUAA4EWRL+S4/yWauDq6C2srD9UptvXdI40YS0aUywyj2XT1TbeNdrLjGZ764kTUwshuqKToa9pUjwT/TPOx939VA8FWV32bC1hE97dgK3mhFpNurp7K3ZH4sDPTz4Qnlpa8kWZ/iThkTPpNYf5LTwIDAQAB"

Additionally, we add a record for servers that do not have DKIM configured (for all clients, this directive is the same).

1
_adsp._domainkey.demon.of.by. IN TXT "dkim=unknown"
Note: In this example, you must replace the domain demon.of.by with your own, for example, example.com.

We configure the OpenDKIM service to handle the domain demon.of.by. We add a new key to the keys file:

1
2
3
4
5
6
7
server:/# vim /etc/opendkim/KeyTable
# To use this file, uncomment the #KeyTable option in /etc/opendkim.conf,
# then uncomment the following line and replace example.com with your domain
# name, then restart OpenDKIM. Additional keys may be added on separate lines.
mail._domainkey.demon.of.by demon.of.by:mail:/etc/opendkim/demon.of.by/mail.private

We specify which key the service should use to sign mail for which domain.

1
2
3
server:/# vim /etc/opendkim/SigningTable
demon.of.by mail._domainkey.demon.of.by

We specify our trusted hosts for the service.

1
2
3
4
5
6
7
8
server:/# vim /etc/opendkim/TrustedHosts
# To use this file, uncomment the #ExternalIgnoreList and/or the #InternalHosts
# option in /etc/opendkim.conf then restart OpenDKIM. Additional hosts
# may be added on separate lines (IP addresses, hostnames, or CIDR ranges).
# The localhost IP (127.0.0.1) should be the first entry in this file.
127.0.0.1
demon.of.by

Don't forget to specify the port on which our OpenDKIM should listen. Edit /etc/default/opendkim:

1
2
3
4
5
6
7
8
9
10
11
12
13
server:/# vim /etc/default/opendkim
# Command-line options specified here will override the contents of
# /etc/dkim-filter.conf. See dkim-filter(8) for a complete list of options.
#DAEMON_OPTS=""
#
# Uncomment to specify an alternate socket
# Note that setting this will override any Socket value in dkim-filter.conf
#SOCKET="local:/var/run/dkim-filter/dkim-filter.sock" # Debian default
#SOCKET="inet:54321" # listen on all interfaces on port 54321
#SOCKET="inet:12345@192.0.2.1" # listen on 192.0.2.1 on port 12345
SOCKET="inet:8891@localhost" # By default - listen on loopback on port 8891

In our case, this is port 8891.

Restart OpenDKIM

1
2
3
server:/# /etc/init.d/opendkim restart
Mar  3 15:01:26 ip-10-71-153-106 opendkim[8303]: OpenDKIM Filter v2.6.8 starting (args: -x /etc/opendkim.conf -u opendkim -P /var/run/opendkim/opendkim.pid -p inet:8891@localhost)

The final touch: adding OpenDKIM to Postfix. To do this, edit /etc/postfix/main.cf and add the following lines:

1
2
3
4
5
6
7
server:/# vim /etc/postfix/main.cf
# DKIM
milter_default_action = accept
milter_protocol = 2
smtpd_milters = inet:localhost:8891
non_smtpd_milters = inet:localhost:8891
Note: If you are using multiple handlers (e.g., Postfix/DomainKeys), you can add several simultaneously, as shown below:
1
2
3
4
milter_default_action = accept
milter_protocol = 2
smtpd_milters = inet:localhost:8891,inet:localhost:8892
non_smtpd_milters = inet:localhost:8891,inet:localhost:8892

Restart Postfix

1
server:/# /etc/init.d/postfix restart

Send test emails to the following addresses:

  • check-auth@verifier.port25.com
  • sa-test@sendmail.net
  • autorespond+dkim@dk.elandsys.com

Carefully analyze the responses.

Note: This post may be AI-assisted and then human-edited. Editorial responsibility: Dmytro Lomaka, Kaltenkirchen.