メールサーバー構築手順(基本)

2025/10/23
まだ執筆途中

メールサーバー構築手順(AlmaLinux 9 + Postfix + Dovecot + TLS + ssl_dh)

AlmaLinux 9 環境で Postfix(MTA)と Dovecot(IMAP/POP)を組み合わせ、Let's Encrypt の TLS 証明書を利用して安全なメールサーバーを構築する。
仮想ドメイン mail.tips456.net を運用し、すべて alma ユーザーの sudo 権限で管理する。micro エディタを使用。
TLS強度を高めるため Dovecot に ssl_dh(4096bit DHパラメータ)を設定する。

後日設定で、バーチャルドメインに追加ドメイン( exmtst.com )を追加する。

サービス構成

* Postfix: SMTP (25), Submission (587), SMTPS (465)
* Dovecot: IMAPS (993), POP3S (995)
* Let's Encrypt: 証明書取得・更新 (80, 443)
* Firewalld: 必要ポートの開放

DNS設定

* mail.tips456.net A → 160.16.103.15
* mail.tips456.net MX → mail.tips456.net (prio 10)
* fbk456.jp A → 160.16.103.15
* 逆引き → fbk456.jp

構築手順

1. リポジトリとパッケージ

sudo dnf config-manager --set-enabled crb
sudo dnf -y update
sudo dnf -y install postfix dovecot certbot s-nail openssl policycoreutils-python-utils
解説

(1) sudo dnf config-manager --set-enabled crb

CRB (CodeReady Builder) リポジトリの有効化

CRBはRHEL/Rocky Linux/AlmaLinuxなどで追加の開発ツールやライブラリを提供するリポジトリ
一部のパッケージの依存関係を満たすために必要
CentOS StreamではPowerToolsという名前の場合もある

(2) sudo dnf -y update

システム全体のパッケージ更新

インストール済みの全パッケージを最新版に更新
  -yオプションで確認プロンプトを自動的に「yes」で応答
セキュリティパッチや機能改善を適用

(3) sudo dnf -y install postfix dovecot certbot s-nail openssl policycoreutils-python-utils

メールサーバー関連パッケージのインストール
各パッケージの役割:

postfix: SMTP(送信)サーバー。メールの送受信を担当
dovecot: IMAP/POP3サーバー。メールボックスへのアクセスを提供
certbot: Let's Encryptの無料SSL証明書を自動取得・更新するツール
s-nail: コマンドラインメールクライアント(mailxの後継)
openssl: SSL/TLS暗号化ライブラリとツール
policycoreutils-python-utils: SELinux管理ツール(semanageコマンド等)

linux側の設定

1. ホスト名と /etc/hosts 設定

sudo hostnamectl set-hostname fbk4546.jp
grep -q fbk4546.jp /etc/hosts || echo "127.0.1.1 fbk4546.jp" | sudo tee -a /etc/hosts
確認:
hostnamectl
sudo cat /etc/hosts

2. vmail ユーザー作成

virtualドメイン運用ですので、その専用ユーザーを決めます。他とかぶらないように5000番代以上がよいでしょう。
ここでは、「vmail」という名前にします。
sudo groupadd -g 5000 vmail
sudo useradd -g vmail -u 5000 -d /var/mail/vhosts -m vmail

# ベースディレクトリのみ作成(ドメイン追加の場合は、この部分を追加)
sudo mkdir -p /var/mail/vhosts/mail.tips456.net

sudo chown -R vmail:vmail /var/mail/vhosts
sudo chmod -R 750 /var/mail/vhosts
メールユーザー・ディレクトリの作成
1. sudo groupadd -g 5000 vmail
仮想メール用グループの作成

vmailという名前のグループを作成
  -g 5000: グループID(GID)を5000に固定
仮想メールボックスを管理する専用グループ
システムユーザーとの競合を避けるため高い番号を使用
2. sudo useradd -g vmail -u 5000 -d /var/mail/vhosts -m vmail
仮想メール用ユーザーの作成

vmailという名前のユーザーを作成
  -g vmail: 所属グループをvmailに設定
  -u 5000: ユーザーID(UID)を5000に固定
  -d /var/mail/vhosts: ホームディレクトリを指定
  -m: ホームディレクトリを自動作成
このユーザーがすべての仮想メールボックスを所有
3. ドメイン用ディレクトリの作成
bashsudo mkdir -p /var/mail/vhosts/mail.tips456.net
各ドメインのメールボックス格納場所を作成

今後、sudo mkdir -p /var/mail/vhosts/exmtst.com と追加予定。

  -p: 親ディレクトリも含めて再帰的に作成
mail.tips456.netとexmtst.comの2ドメイン分を準備
各ドメインのユーザーメールボックスがこの下に作成される
4. sudo chown -R vmail:vmail /var/mail/vhosts
所有者の設定

/var/mail/vhosts以下すべてのファイル・ディレクトリの所有者をvmail:vmailに変更
  -R: 再帰的に適用
PostfixとDovecotがvmailユーザーとしてメールを管理
5. sudo chmod -R 750 /var/mail/vhosts
パーミッションの設定

750の意味:

所有者(vmail): 読み・書き・実行可能(7)
グループ(vmail): 読み・実行可能(5)
その他: アクセス不可(0)

3. Postfix設定

(1) main.cf  postfixの基本設定

sudo edit /etc/postfix/main.cf

(ファイル変更)
# 基本
myhostname = fbk456.jp
mydomain = fbk456.jp
myorigin = $myhostname

inet_interfaces = all
inet_protocols = ipv4

# 振り分け順位1位 mydestinarionの設定
#mydestination には、メール運用ドメインは含めない(このルールは優先適用され、異なるドメインの同じユーザー名についてメールが混同する)

mydestination = localhost.$mydomain, localhost

# 仮想ドメイン ドメイン追加の時には、ここに追加
virtual_mailbox_domains =
 mail.tips456.net

# Dovecotとの通信に使用するunixソケットの指定
virtual_transport = lmtp:unix:private/dovecot-lmtp

# 振り分け順位第2位 virtual_aliasの転送設定 hashで設定
virtual_alias_maps = hash:/etc/postfix/virtual

# バーチャルuidは5000に設定
virtual_minimum_uid = 5000
virtual_uid_maps = static:5000
virtual_gid_maps = static:5000

# TLS設定(証明書は後で certbot 取得を設定する)
smtpd_tls_cert_file = /etc/letsencrypt/live/fbk456.jp/fullchain.pem
smtpd_tls_key_file  = /etc/letsencrypt/live/fbk456.jp/privkey.pem
smtpd_tls_security_level = may

smtpd_tls_loglevel = 1
smtp_tls_security_level = may
smtp_tls_loglevel = 1

smtpd_tls_mandatory_protocols = !SSLv2, !SSLv3, !TLSv1, !TLSv1.1

# SASL(Dovecot) Simple Authentication and Security Layer
# Postfixのユーザー認証(submission用)は、Dovecotの認証機構(Dovecot SASL)を利用することの設定
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = $myhostname
smtpd_sasl_security_options = noanonymous
broken_sasl_auth_clients = yes

# 最小受信ポリシー
smtpd_recipient_restrictions =
    permit_mynetworks,
    permit_sasl_authenticated,
    reject_unauth_destination

(2) master.cf

sudo edit /etc/postfix/master.cf
(ファイル変更)
submission inet n - n - - smtpd
  -o syslog_name=postfix/submission
  -o smtpd_tls_security_level=encrypt
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_client_restrictions=permit_sasl_authenticated,reject
  -o milter_macro_daemon_name=ORIGINATING

smtps inet n - n - - smtpd
  -o syslog_name=postfix/smtps
  -o smtpd_tls_wrapper_mode=yes
  -o smtpd_sasl_auth_enable=yes
  -o smtpd_client_restrictions=permit_sasl_authenticated,reject
  -o milter_macro_daemon_name=ORIGINATING

4. Dovecot設定

(1) パスワードファイル作成

sudo micro /etc/dovecot/passwd
順番フィールド名説明省略可否
1*user**ユーザー名(メールアドレス)user-1@mail.tips456.net*必須**
2*password**パスワードハッシュSHA512-CRYPT$6$...*必須**
3*uid**ユーザーID5000省略可(::)
4*gid**グループID5000省略可(::)
5*gecos**コメント(通常は実名)John Doe省略可(::)
6*home**ホームディレクトリ/var/mail/vhosts/...省略可(::)
7*shell**シェル(メールでは不使用)/sbin/nologin省略可(::)
8*extra_fields**追加フィールドquota=1G:...省略可
# /etc/dovecot/passwd の内容
# passdbが使う部分:ユーザー名とパスワード(最初の2フィールド)
# userdbが使う部分:UID以降のフィールド(3フィールド目以降)

user-1@mail.tips456.net:{SHA512-CRYPT}$6$...::::/var/mail/vhosts/mail.tips456.net/user-1::quota=1G
user-2@mail.tips456.net:{SHA512-CRYPT}$6$...::::/var/mail/vhosts/mail.tips456.net/user-2::quota=1G
#                      ^                    ^
#                      |                    |
#                  passdbが確認         userdbが取得
正式表記
# /etc/dovecot/passwd
# ユーザーごとに異なるquota設定
user-1@mail.tips456.net:{SHA512-CRYPT}$6$...::::::userdb_quota_rule=*:storage=1G
user-2@mail.tips456.net:{SHA512-CRYPT}$6$...::::::userdb_quota_rule=*:storage=2G
user-1@exmtst.com:{SHA512-CRYPT}$6$...::::::userdb_quota_rule=*:storage=500M
user-2@exmtst.com:{SHA512-CRYPT}$6$...::::::userdb_quota_rule=*:storage=10G
test@exmtst.com:{SHA512-CRYPT}$6$...::::::userdb_quota_rule=*:storage=100M
※quota=1G ユーザーのメールボックス容量 1GB (0は無制限)
sudo chmod 640 /etc/dovecot/passwd
sudo chown root:dovecot /etc/dovecot/passwd

(2) 10-auth.conf

sudo micro /etc/dovecot/conf.d/10-auth.conf
(ファイル変更)
disable_plaintext_auth = yes
auth_mechanisms = plain login
!include auth-passwdfile.conf.ext

(3) auth-passwdfile.conf.ext

sudo edit /etc/dovecot/conf.d/auth-passwdfile.conf.ext
# シンプルで管理しやすい構成
passdb {
  driver = passwd-file
  args = scheme=SHA512-CRYPT username_format=%Lu /etc/dovecot/passwd
}

userdb {
  driver = passwd-file
  args = username_format=%Lu /etc/dovecot/passwd

  # passwd-fileに関する全ユーザー共通の設定はここで指定
 # homeは動的に設定、uid/gidは共通
  default_fields = uid=vmail gid=vmail home=/var/mail/vhosts/%d/%n mail=maildir:~/Maildir
}
  • username_format=のパラメータ
変数意味
%uユーザー名全体(そのまま)
%nローカル部分(@より前)
%dドメイン部分(@より後)
%lユーザー名を小文字に変換
%Luユーザー名全体を小文字に
%Lnローカル部分を小文字に
%Ldドメイン部分を小文字に

(4) 10-mail.conf

sudo edit /etc/dovecot/conf.d/10-mail.conf
mail_location = maildir:~/Maildir
mail_uid = vmail
mail_gid = vmail
first_valid_uid = 5000
first_valid_gid = 5000

mail_plugins = $mail_plugins quota

# Maildirの自動作成を有効化(通常デフォルトで有効)
maildir_empty_new = yes
「mail_location = maildir:~/Maildir」は全てのメールのデフォルト(システム全体のデフォルト)として設定。
(4)-1 /etc/dovecot/conf.d/20-imap.conf
protocol imap {
  mail_plugins = $mail_plugins imap_quota
}

(4)-2 /etc/dovecot/conf.d/20-pop3.conf

protocol pop3 {
  mail_plugins = $mail_plugins
}
(4)-3 /etc/dovecot/conf.d/90-quota.conf
plugin {
  # quota設定(Maildir++形式)
  quota = maildir:User quota
  
  # 警告メール設定(オプション)
  quota_warning = storage=95%% quota-warning 95 %u
  quota_warning2 = storage=80%% quota-warning 80 %u
  
  # 容量超過時のメッセージ
  quota_exceeded_message = メールボックスの容量が一杯です。不要なメールを削除してください。

  # ゴミ箱は10%追加容量を許可
  quota_rule2 = Trash:storage=+10%%
}

(5) 10-master.conf

sudo edit /etc/dovecot/conf.d/10-master.conf
(ファイル変更)
service auth {
  unix_listener /var/spool/postfix/private/auth {
    mode = 0660
    user = postfix
    group = postfix
  }
}
service lmtp {
  unix_listener /var/spool/postfix/private/dovecot-lmtp {
    mode = 0600
    user = postfix
    group = postfix
  }
}

(6) ssl_dh 設定と TLS ポリシー

sudo openssl dhparam -out /etc/dovecot/dh.pem 4096
sudo chmod 600 /etc/dovecot/dh.pem
sudo micro /etc/dovecot/conf.d/10-ssl.conf
(ファイル変更)
ssl = required
ssl_min_protocol = TLSv1.2
ssl_cipher_list = PROFILE=SYSTEM
ssl_curve_list = X25519:P-256
ssl_dh = </etc/dovecot/dh.pem
ssl_cert = </etc/letsencrypt/live/fbk456.jp/fullchain.pem
ssl_key  = </etc/letsencrypt/live/fbk456.jp/privkey.pem
既存のssl_cert = ssl_key = の無効化を忘れずに!

5. Let's Encrypt 証明書

sudo firewall-cmd --add-service=http --permanent
sudo firewall-cmd --reload
sudo systemctl stop postfix dovecot
sudo certbot certonly --standalone -d fbk456.jp --agree-tos -m tech456@example.jp --non-interactive
sudo systemctl start postfix dovecot
sudo systemctl enable --now certbot-renew.timer
sudo certbot renew --dry-run

6. Firewalld設定

sudo firewall-cmd --add-service=ssh --permanent
sudo firewall-cmd --add-service=smtp --permanent
sudo firewall-cmd --add-service=smtps --permanent
sudo firewall-cmd --add-service=smtp-submission --permanent
sudo firewall-cmd --add-service=imaps --permanent
sudo firewall-cmd --add-service=pop3s --permanent
sudo firewall-cmd --add-service=http --permanent
sudo firewall-cmd --add-service=https --permanent
sudo firewall-cmd --reload
sudo firewall-cmd --list-services

7. 動作確認

sudo systemctl start postfix dovecot
sudo ss -lntp | egrep ':25|:465|:587|:993|:995|:80|:443'
sudo doveadm auth test user-1@mail.tips456.net
echo | sudo openssl s_client -starttls smtp -connect fbk456.jp:587 -servername fbk456.jp | head -n 20
echo "hello" | sudo s-nail -s "local test" user-1@mail.tips456.net
sudo tail -n 50 /var/log/maillog

8. クライアント設定

* IMAPS: 993 / SSL/TLS / ユーザー=フルアドレス
* SMTP: 587 / STARTTLS / 認証=通常パスワード
* POP3S: 995 / SSL/TLS
* SPA/NTLM は使用しない

9. 運用と保守

* fail2ban 導入で 22,25,465,587,993,995 を監視
* certbot 自動更新確認: `sudo certbot renew --dry-run`
* Dovecot 冗長ログ解除: auth_debug=no, verbose_ssl=no
* LMTP 配送確認: `sudo tail -f /var/log/maillog | egrep 'lmtp|saved mail'`
* TLS 確認: `echo | openssl s_client -connect fbk456.jp:993 -servername fbk456.jp | grep Protocol`