1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【CentOS Stream 9】Postfix + Dovecot + DKIM/SPF/DMARCでメールサーバー構築【ローカルMTA対応】

Posted at

はじめに

CentOS Stream 9で Postfix + Dovecot + OpenDKIM を用いたメールサーバーを構築しました。
また、SPF/DKIM/DMARCすべてにPASSするように設定も行いました。
この手順はローカル通知用途だけでなく、パブリック送信にも対応しています。

✅ 環境情報

項目 内容
OS CentOS Stream 9
ドメイン sample.jp
ホスト名 mail.sample.jp
MTA Postfix (3.5.25)
IMAP/POP Dovecot (2.3.16)
認証 SASL(Dovecot連携)
DKIM/SPF/DMARC すべてPASS確認済み

構築

以下よりインストールと設定を行っていきます。

1. Postfixのインストールと初期設定

sudo dnf install postfix
sudo systemctl enable --now postfix

IPv6によるトラブルを避けるため、main.cf に以下を追記:

inet_protocols = ipv4

main.cf の代表的設定

myhostname = mail.sample.jp
mydomain = sample.jp
myorigin = $mydomain
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
home_mailbox = Maildir/
message_size_limit = 10485760
mynetworks = 127.0.0.0/8, [あなたのグローバルIP]/32

2. SASL認証(SMTP AUTH)設定【Dovecot連携】

DovecotをSASL認証エージェントとして使用します。

必要パッケージ

sudo dnf install cyrus-sasl cyrus-sasl-plain
sudo systemctl enable --now saslauthd

Postfix設定(main.cf)

smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous

smtpd_relay_restrictions = 
  permit_mynetworks, 
  permit_sasl_authenticated, 
  reject_unauth_destination

Dovecotソケット設定(10-master.conf)

service auth {
  unix_listener /var/spool/postfix/private/auth {
    mode = 0660
    user = postfix
    group = postfix
  }
}

3. Dovecotのインストールと設定

sudo dnf install dovecot

設定ファイル変更

  • /etc/dovecot/conf.d/10-mail.conf
mail_location = maildir:~/Maildir
first_valid_uid = 1000
  • /etc/dovecot/conf.d/10-auth.conf
disable_plaintext_auth = no
auth_mechanisms = plain login
  • /etc/dovecot/conf.d/10-ssl.conf
ssl = no  # 社内利用ならOK。本番公開用途ではTLSを有効化

起動:

sudo systemctl enable --now dovecot

4. ユーザー作成とMaildir構築

sudo useradd -m -s /bin/bash mailtest
echo "mailtest:password" | sudo chpasswd
sudo mkdir -p /home/mailtest/Maildir/{new,cur,tmp}
sudo chown -R mailtest:mailtest /home/mailtest/Maildir
sudo chmod -R 700 /home/mailtest/Maildir

5. メール送信確認(mailx)

sudo dnf install s-nail
echo "これはテストメールです" | mailx -s "テスト件名" mailtest@localhost

6. SPF / DKIM / DMARC の設定と検証

🔹 SPF

DNS設定例:

sample.jp. IN TXT "v=spf1 a mx ip4:xxx.xxx.xxx.xxx ~all"

🔹 DKIM(OpenDKIM)

sudo dnf install opendkim opendkim-tools
sudo mkdir -p /etc/opendkim/keys/sample.jp
cd /etc/opendkim/keys/sample.jp
sudo opendkim-genkey -s mail -d sample.jp
sudo chown opendkim:opendkim mail.private

/etc/opendkim.conf

Mode sv
Socket inet:12301@localhost
KeyTable /etc/opendkim/KeyTable
SigningTable refile:/etc/opendkim/SigningTable
ExternalIgnoreList refile:/etc/opendkim/TrustedHosts
InternalHosts refile:/etc/opendkim/TrustedHosts

KeyTable

mail._domainkey.sample.jp sample.jp:mail:/etc/opendkim/keys/sample.jp/mail.private

SigningTable

*@sample.jp mail._domainkey.sample.jp

Postfix設定(main.cf)

milter_default_action = accept
milter_protocol = 2
smtpd_milters = inet:localhost:12301
non_smtpd_milters = inet:localhost:12301
sudo systemctl restart opendkim
sudo postfix reload

🔹 DMARC

_dmarc.sample.jp. IN TXT "v=DMARC1; p=none; rua=mailto:admin@sample.jp"

7. aliases修正(必要に応じて)

# /etc/aliases
#support: postmaster
sudo newaliases
sudo postfix reload

8. 検証コマンドと外部ツール

nslookup -q=TXT mail._domainkey.sample.jp
nslookup -q=TXT _dmarc.sample.jp

外部サービス:

送信結果

以下送信結果になります。
スクリーンショット 2025-05-30 11.48.13.png


🔍 補足と注意点

項目 説明
Dovecot IMAP/POPの他、Postfixと連携するSASL認証機能も提供
SASL認証 PostfixがDovecot経由でユーザー認証(/etc/shadow)を行う構成
TLS 本記事はssl=noで構築。公開運用にはLet's EncryptなどのTLS設定を推奨
sendmail-milter 不要。OpenDKIMで完結するためインストール不要

📘 参考リンク


✅ まとめ

  • Postfix + Dovecot 構成でローカルMTA構築
  • DKIM/SPF/DMARCを正しく設定し、外部でも高評価
  • TLSやウイルススキャン追加でさらに実用性向上も可能

おわりに

今は AmazonSES や SendGrid といったメールサービスがあるのでそっちをうまく活用した方が運用面などを考えると効率的です。
とはいえ、メールサーバーを構築しなければならない場合もあると思います。
そう言った際に参考になれば幸いです。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?