そういうのがいいブログ

SIerで働く30代サラリーマンSEがインフラエンジニアに憧れてLinux・クラウド・AWSの勉強をするブログ

迷惑メール判定されないようDKIMを設定したメールサーバーを構築

※[PR]当ブログの記事の中にはプロモーションが含まれています。

・迷惑メール判定されないようDKIMを設定したメールサーバーを構築する方法を知りたい。
・具体的な手順を分かりやすく教えてほしい。

こういった疑問に答えます。

本記事の内容

  1. 迷惑メール判定されないようDKIMを設定したメールサーバーを構築

サトナカ (@souiunogaii)

この記事を書いている私は、某SIerに勤務しながら、
WEB系エンジニア・インフラエンジニアに憧れて、プログラミングおよびインフラ技術の勉強をしています。

こういった私が、解説していきます。

私が実機で試したコマンドや画像を載せて書いています。
記事の信頼性担保に繋がると思います。

迷惑メール判定されないようDKIMを設定したメールサーバーを構築

手順① Opendkimのインストール

dnf -y install opendkim
[root@SV-VEGA ~]# dnf -y install opendkim
メタデータの期限切れの最終確認: 1:42:08 前の 20230905103841秒 に実施しました。
依存関係が解決しました。
====================================================================================================
 パッケージ                  アーキテクチャー バージョン                  リポジトリー        サイズ
====================================================================================================
インストール:
 opendkim                    x86_64           2.11.0-0.34.el8             epel                244 k
依存関係のインストール:
 libbsd                      x86_64           0.11.7-2.el8                epel                120 k
 libevent                    x86_64           2.1.8-5.el8                 baseos              252 k
 libmd                       x86_64           1.1.0-1.el8                 epel                 45 k
 libmemcached-libs           x86_64           1.0.18-17.el8               appstream           136 k
 libopendkim                 x86_64           2.11.0-0.34.el8             epel                 84 k
 opendbx                     x86_64           1.4.6-21.el8                epel                 56 k
 sendmail-milter             x86_64           8.15.2-34.el8               appstream            81 k

トランザクションの概要
====================================================================================================
インストール  8 パッケージ

ダウンロードサイズの合計: 1.0 M
インストール後のサイズ: 2.6 M
パッケージのダウンロード:
(1/8): libmemcached-libs-1.0.18-17.el8.x86_64.rpm                   1.3 MB/s | 136 kB     00:00
(2/8): sendmail-milter-8.15.2-34.el8.x86_64.rpm                     654 kB/s |  81 kB     00:00
(3/8): libevent-2.1.8-5.el8.x86_64.rpm                              1.3 MB/s | 252 kB     00:00
(4/8): libmd-1.1.0-1.el8.x86_64.rpm                                 243 kB/s |  45 kB     00:00
(5/8): opendbx-1.4.6-21.el8.x86_64.rpm                              811 kB/s |  56 kB     00:00
(6/8): libbsd-0.11.7-2.el8.x86_64.rpm                               385 kB/s | 120 kB     00:00
(7/8): libopendkim-2.11.0-0.34.el8.x86_64.rpm                       363 kB/s |  84 kB     00:00
(8/8): opendkim-2.11.0-0.34.el8.x86_64.rpm                          830 kB/s | 244 kB     00:00
----------------------------------------------------------------------------------------------------
合計                                                                437 kB/s | 1.0 MB     00:02
トランザクションの確認を実行中
トランザクションの確認に成功しました。
トランザクションのテストを実行中
トランザクションのテストに成功しました。
トランザクションを実行中
  準備             :                                                                            1/1
  インストール中   : opendbx-1.4.6-21.el8.x86_64                                                1/8
  インストール中   : libmd-1.1.0-1.el8.x86_64                                                   2/8
  インストール中   : libbsd-0.11.7-2.el8.x86_64                                                 3/8
  インストール中   : libopendkim-2.11.0-0.34.el8.x86_64                                         4/8
  インストール中   : libevent-2.1.8-5.el8.x86_64                                                5/8
  インストール中   : libmemcached-libs-1.0.18-17.el8.x86_64                                     6/8
  インストール中   : sendmail-milter-8.15.2-34.el8.x86_64                                       7/8
  scriptletの実行中: sendmail-milter-8.15.2-34.el8.x86_64                                       7/8
  scriptletの実行中: opendkim-2.11.0-0.34.el8.x86_64                                            8/8
  インストール中   : opendkim-2.11.0-0.34.el8.x86_64                                            8/8
  scriptletの実行中: opendkim-2.11.0-0.34.el8.x86_64                                            8/8
  検証             : libmemcached-libs-1.0.18-17.el8.x86_64                                     1/8
  検証             : sendmail-milter-8.15.2-34.el8.x86_64                                       2/8
  検証             : libevent-2.1.8-5.el8.x86_64                                                3/8
  検証             : libbsd-0.11.7-2.el8.x86_64                                                 4/8
  検証             : libmd-1.1.0-1.el8.x86_64                                                   5/8
  検証             : libopendkim-2.11.0-0.34.el8.x86_64                                         6/8
  検証             : opendbx-1.4.6-21.el8.x86_64                                                7/8
  検証             : opendkim-2.11.0-0.34.el8.x86_64                                            8/8

インストール済み:
  libbsd-0.11.7-2.el8.x86_64                     libevent-2.1.8-5.el8.x86_64
  libmd-1.1.0-1.el8.x86_64                       libmemcached-libs-1.0.18-17.el8.x86_64
  libopendkim-2.11.0-0.34.el8.x86_64             opendbx-1.4.6-21.el8.x86_64
  opendkim-2.11.0-0.34.el8.x86_64                sendmail-milter-8.15.2-34.el8.x86_64

完了しました!
[root@SV-VEGA ~]# dnf list installed | grep opendkim
libopendkim.x86_64                   2.11.0-0.34.el8                        @epel
opendkim.x86_64                      2.11.0-0.34.el8                        @epel
dnf install opendkim-tools
[root@SV-VEGA ~]# dnf install opendkim-tools
メタデータの期限切れの最終確認: 1:46:21 前の 20230905103841秒 に実施しました。
依存関係が解決しました。
====================================================================================================
 パッケージ                 アーキテクチャー   バージョン                    リポジトリー     サイズ
====================================================================================================
インストール:
 opendkim-tools             x86_64             2.11.0-0.34.el8               epel              84 k

トランザクションの概要
====================================================================================================
インストール  1 パッケージ

ダウンロードサイズの合計: 84 k
インストール後のサイズ: 201 k
これでよろしいですか? [y/N]: y
パッケージのダウンロード:
[MIRROR] opendkim-tools-2.11.0-0.34.el8.x86_64.rpm: Curl error (35): SSL connect error for https://alma.acidman.thelefty.org/fedora-epel/8/Everything/x86_64/Packages/o/opendkim-tools-2.11.0-0.34.el8.x86_64.rpm [OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to alma.acidman.thelefty.org:443 ]
opendkim-tools-2.11.0-0.34.el8.x86_64.rpm                           177 kB/s |  84 kB     00:00
----------------------------------------------------------------------------------------------------
合計                                                                 68 kB/s |  84 kB     00:01
トランザクションの確認を実行中
トランザクションの確認に成功しました。
トランザクションのテストを実行中
トランザクションのテストに成功しました。
トランザクションを実行中
  準備             :                                                                            1/1
  インストール中   : opendkim-tools-2.11.0-0.34.el8.x86_64                                      1/1
  scriptletの実行中: opendkim-tools-2.11.0-0.34.el8.x86_64                                      1/1
  検証             : opendkim-tools-2.11.0-0.34.el8.x86_64                                      1/1

インストール済み:
  opendkim-tools-2.11.0-0.34.el8.x86_64

完了しました!

手順② Opendkim秘密鍵の作成

mkdir /etc/opendkim/keys/space.com
opendkim-genkey -D /etc/opendkim/keys/space.com/ -d space.com -s 230905(セレクタ名は日付)
[root@SV-VEGA ~]# ls -al /etc/opendkim/keys/space.com/
合計 8
drwxr-xr-x 2 root root      46  95 12:31 .
drwxr-x--- 3 root opendkim  32  95 12:21 ..
-rw------- 1 root root     887  95 12:31 230905.private
-rw------- 1 root root     320  95 12:31 230905.txt

所有者をopendkimに変更

chown -R opendkim:opendkim /etc/opendkim/keys/space.com/
[root@SV-VEGA ~]# chown -R opendkim:opendkim /etc/opendkim/keys/space.com/
[root@SV-VEGA ~]# ls -al /etc/opendkim/keys/space.com/
合計 8
drwxr-xr-x 2 opendkim opendkim  46  95 12:31 .
drwxr-x--- 3 root     opendkim  32  95 12:21 ..
-rw------- 1 opendkim opendkim 887  95 12:31 230905.private
-rw------- 1 opendkim opendkim 320  95 12:31 230905.txt

手順③ DNSに公開鍵の設定

セレクタ名.txtを開いて公開鍵情報を確認

cat /etc/opendkim/keys/space.com/230905.txt
[root@SV-VEGA ~]# cat /etc/opendkim/keys/space.com/230905.txt
230905._domainkey       IN      TXT     ( "v=DKIM1; k=rsa; "
          "p=XXXXXXXXXXXXXXXXXXXXXX" )  ; ----- DKIM key 230905 for space.com

DNSサーバーにTXTレコードを追加

公開鍵の確認

[root@SV-VEGA ~]# dig 230905._domainkey.space.com txt

; <<>> DiG 9.11.36-RedHat-9.11.36-11.el8_9 <<>> 230905._domainkey.space.com txt
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 56933
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4000
;; QUESTION SECTION:
;230905._domainkey.space.com. IN TXT

;; ANSWER SECTION:
230905._domainkey.space.com. 300 IN TXT "v=DKIM1; k=rsa; p=XXXXXXXXXXXXXXXXXXXXX"

;; Query time: 34 msec
;; SERVER: 192.168.XXX.XXX#53(192.168.XXX.XXX)
;; WHEN: 木  3月 21 18:40:29 JST 2024
;; MSG SIZE  rcvd: 312
[root@SV-VEGA ~]# dig _adsp._domainkey.space.com txt

; <<>> DiG 9.11.36-RedHat-9.11.36-11.el8_9 <<>> _adsp._domainkey.space.com txt
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 31609
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4000
;; QUESTION SECTION:
;_adsp._domainkey.space.com. IN        TXT

;; ANSWER SECTION:
_adsp._domainkey.space.com. 300 IN TXT "dkim=unknown"

;; Query time: 30 msec
;; SERVER: 192.168.XXX.XXX#53(192.168.XXX.XXX)
;; WHEN: 木  3月 21 18:42:08 JST 2024
;; MSG SIZE  rcvd: 89

手順④ opendkim.confの編集

[root@SV-VEGA ~]# cp /etc/opendkim.conf /etc/opendkim.conf.original_bk
vi /etc/opendkim.conf

Mode

     36 ##  Selects operating modes. Valid modes are s (sign) and v (verify). Default is v.
     37 ##  Must be changed to s (sign only) or sv (sign and verify) in order to sign outgoing
     38 ##  messages.
     39 Mode    sv    ←★★変更

Socket

     55 ##  Create a socket through which your MTA can communicate.
     56 Socket  inet:8891@localhost    ←★★変更
     57 #Socket local:/run/opendkim/opendkim.sock

KeyFile

     97 ##  Gives the location of a private key to be used for signing ALL messages. This
     98 ##  directive is ignored if KeyTable is enabled.
     99 #KeyFile        /etc/opendkim/keys/default.private    ←★★コメントで無効化

KeyTable

    101 ##  Gives the location of a file mapping key names to signing keys. In simple terms,
    102 ##  this tells OpenDKIM where to find your keys. If present, overrides any KeyFile
    103 ##  directive in the configuration file. Requires SigningTable be enabled.
    104 KeyTable        /etc/opendkim/KeyTable    ←★★コメント解除して有効化

SigningTable

    106 ##  Defines a table used to select one or more signatures to apply to a message based
    107 ##  on the address found in the From: header field. In simple terms, this tells
    108 ##  OpenDKIM how to use your keys. Requires KeyTable be enabled.
    109 SigningTable    refile:/etc/opendkim/SigningTable    ←★★コメント解除して有効化

ExternalIgnoreList

    111 ##  Identifies a set of "external" hosts that may send mail through the server as one
    112 ##  of the signing domains without credentials as such.
    113 ExternalIgnoreList      refile:/etc/opendkim/TrustedHosts    ←★★コメント解除して有効化

InternalHosts

    115 ##  Identifies a set "internal" hosts whose mail should be signed rather than verified.
    116 InternalHosts   refile:/etc/opendkim/TrustedHosts    ←★★コメント解除して有効化

手順⑤ KeyTableの編集

[root@SV-VEGA ~]# cp -p /etc/opendkim/KeyTable /etc/opendkim/KeyTable.original_bk
vi /etc/opendkim/KeyTable
      1 # OPENDKIM KEY TABLE
      2 # To use this file, uncomment the #KeyTable option in /etc/opendkim.conf,
      3 # then uncomment the following line and replace example.com with your domain
      4 # name, then restart OpenDKIM. Additional keys may be added on separate lines.
5
      6 #default._domainkey.example.com example.com:default:/etc/opendkim/keys/default.private
      7 230905._domainkey.space.com space.com:230905:/etc/opendkim/keys/space.com/230905.privatevate    ←★★追加

手順⑥ SigningTableの編集

[2023/12/01訂正]コマンドの記載に誤りがありましたので訂正いたします。
(まこ様、ご指摘コメントありがとうございます。)

[root@SV-VEGA ~]# cp -p /etc/opendkim/KeyTable /etc/opendkim/KeyTable.original_bk

[root@SV-VEGA ~]# cp -p /etc/opendkim/SigningTable /etc/opendkim/SigningTable.original_bk

vi /etc/opendkim/KeyTable

vi /etc/opendkim/SigningTable
     25 #example.com default._domainkey.example.com
     26 *@space.com 230905._domainkey.space.com    ←★★追加

手順⑦ opendkimを再起動して変更を反映

systemctl start opendkim
systemctl status opendkim
[root@SV-VEGA ~]# systemctl start opendkim
[root@SV-VEGA ~]# systemctl status opendkim
● opendkim.service - DomainKeys Identified Mail (DKIM) Milter
   Loaded: loaded (/usr/lib/systemd/system/opendkim.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2023-09-05 12:58:55 JST; 7s ago
     Docs: man:opendkim(8)
           man:opendkim.conf(5)
           man:opendkim-genkey(8)
           man:opendkim-genzone(8)
           man:opendkim-testadsp(8)
           man:opendkim-testkey
           http://www.opendkim.org/docs.html
 Main PID: 12293 (opendkim)
    Tasks: 3 (limit: 24822)
   Memory: 1.4M
   CGroup: /system.slice/opendkim.service
           mq12293 /usr/sbin/opendkim -f -x /etc/opendkim.conf

 905 12:58:55 SV-VEGA systemd[1]: Started DomainKeys Identified Mail (DKIM) Milter.
 905 12:58:55 SV-VEGA opendkim[12293]: OpenDKIM Filter v2.11.0 starting (args: -f -x /etc/opendk>

自動起動の設定

systemctl enable opendkim
systemctl is-enabled opendkim
[root@SV-VEGA ~]# systemctl enable opendkim
Created symlink /etc/systemd/system/multi-user.target.wants/opendkim.service → /usr/lib/systemd/system/opendkim.service.
[root@SV-VEGA ~]# systemctl is-enabled opendkim
enabled

手順⑧ Postfixのmain.cfの編集

vi /etc/postfix/main.cf
    761 ### DKIM Settings    ←★★追加
    762 smtpd_milters = inet:127.0.0.1:8891    ←★★追加
    763 non_smtpd_milters = $smtpd_milters    ←★★追加
    764 milter_default_action = accept    ←★★追加

手順⑨ Postfixを再起動して変更を反映

[root@SV-VEGA ~]# postconf -n
[root@SV-VEGA ~]# postfix check
systemctl restart postfix
systemctl status postfix
[root@SV-VEGA ~]# systemctl restart postfix
[root@SV-VEGA ~]# systemctl status postfix
● postfix.service - Postfix Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2023-09-05 13:02:24 JST; 5s ago
  Process: 12407 ExecStop=/usr/sbin/postfix stop (code=exited, status=0/SUCCESS)
  Process: 12429 ExecStart=/usr/sbin/postfix start (code=exited, status=0/SUCCESS)
  Process: 12427 ExecStartPre=/usr/libexec/postfix/chroot-update (code=exited, status=0/SUCCESS)
  Process: 12423 ExecStartPre=/usr/libexec/postfix/aliasesdb (code=exited, status=0/SUCCESS)
  Process: 12422 ExecStartPre=/usr/sbin/restorecon -R /var/spool/postfix/pid/master.pid (code=exite>
 Main PID: 12497 (master)
    Tasks: 3 (limit: 24822)
   Memory: 4.5M
   CGroup: /system.slice/postfix.service
           tq12497 /usr/libexec/postfix/master -w
           tq12498 pickup -l -t unix -u
           mq12499 qmgr -l -t unix -u

 905 13:02:23 SV-VEGA systemd[1]: postfix.service: Succeeded.
 905 13:02:23 SV-VEGA systemd[1]: Stopped Postfix Mail Transport Agent.
 905 13:02:23 SV-VEGA systemd[1]: Starting Postfix Mail Transport Agent...
 905 13:02:24 SV-VEGA postfix/master[12497]: daemon started -- version 3.5.8, configuration /etc>
 905 13:02:24 SV-VEGA systemd[1]: Started Postfix Mail Transport Agent.