Secure Email Delivery: Implementing SPF and DKIM with Postfix
  1. Create a directory to hold the OpenDKIM socket file and allow only opendkim user and postfix group to access it.
sudo mkdir /var/spool/postfix/opendkim
sudo chown opendkim:postfix /var/spool/postfix/opendkim
  1. Edit the OpenDKIM main configuration file.
sudo nano /etc/opendkim.conf

Find the following line (Ubuntu 18.04)

Socket    local:/var/run/opendkim/opendkim.sock

or (Ubuntu 22.04/20.04)

Socket local:/run/opendkim/opendkim.sock

Replace it with the following line. (If you can’t find the above line, then add the following line.)

Socket    local:/var/spool/postfix/opendkim/opendkim.sock

Save and close the file.

  1. Edit the OpenDKIM configuration file /etc/default/opendkim:
sudo nano /etc/default/opendkim

Find the following line:

SOCKET="local:/var/run/opendkim/opendkim.sock"

or

SOCKET=local:$RUNDIR/opendkim.sock

Change it to:

SOCKET="local:/var/spool/postfix/opendkim/opendkim.sock"

Save and close the file.

  1. Edit the Postfix main configuration file.
sudo nano /etc/postfix/main.cf

Add the following lines at the end of the file, so Postfix will be able to call OpenDKIM via the milter protocol.

# Milter configuration
milter_default_action = accept
milter_protocol = 6
smtpd_milters = local:opendkim/opendkim.sock
non_smtpd_milters = $smtpd_milters

Save and close the file.

  1. Restart OpenDKIM and Postfix service.
sudo systemctl restart opendkim postfix