master.cf-Postfixの設定
claude作
Postfix master.cf :メールサーバーのプロセス管理を理解する
はじめに
Postfixメールサーバーを運用する上で、main.cfと並んで重要な設定ファイルが/etc/postfix/master.cfです。このファイルは、Postfixのマスタープロセスが管理する各種サービスプロセスの動作を定義します。
本記事では、master.cfの構造から実践的な設定例まで、体系的に解説していきます。メールサーバー管理者の方や、Postfixの内部動作を深く理解したい方に向けた内容となっています。
master.cfとは何か
master.cfは、Postfixのマスターデーモン(master)が読み込む設定ファイルで、以下の役割を持ちます:
- 各サービスプロセスの起動方法の定義
- ネットワークポートのリスニング設定
- プロセスの並列度とリソース制限
- セキュリティ設定(chroot、権限など)
- サービス間の連携設定
main.cfが「何をするか」を定義するのに対し、master.cfは「どのように実行するか」を定義すると理解できます。
ファイル構造の基本
8つのフィールド
master.cfの各行は、タブまたはスペースで区切られた8つのフィールドで構成されています:
# service type private unpriv chroot wakeup maxproc command + args
# (yes) (yes) (no) (never) (100)
# ==========================================================================
smtp inet n - n - - smtpd
各フィールドの詳細を見ていきましょう。
1. Service(サービス名)フィールド
サービスの識別子またはリスニングアドレスを指定します。
ネットワークサービスの例:
smtp- 標準SMTPサービス(ポート25)submission- メール投稿用サービス(ポート587)smtps- SMTP over SSL/TLS(ポート465)10.0.0.1:smtp- 特定IPアドレスでリスニング[::1]:smtp- IPv6アドレスでリスニング
内部サービスの例:
pickup- ローカルメール投入cleanup- メールヘッダー処理qmgr- キューマネージャーrewrite- アドレス書き換えbounce- バウンスメール処理
2. Type(タイプ)フィールド
サービスの接続方式を指定します。
inet - TCP/IPネットワーク接続
smtp inet n - n - - smtpd
外部からの接続を受け付けるサービスで使用
unix - UNIXドメインソケット
cleanup unix n - n - 0 cleanup
内部プロセス間通信で使用
fifo - 名前付きパイプ(先入れ先出し)
pickup fifo n - n 60 1 pickup
ローカルからのメール投入で使用
- pass - パススルーサービス
外部プログラムへのパススルーで使用
policy unix - n n - - spawn
3. Private(プライベート)フィールド
サービスへのアクセス制限を設定します。
- y(yes) - Postfixサブシステムのみアクセス可能
- n(no) - 外部からもアクセス可能
- -(ハイフン) - デフォルト値(通常はyes)を使用
# プライベートサービス(内部処理用)
cleanup unix y - n - 0 cleanup
# パブリックサービス(外部接続受付)
smtp inet n - n - - smtpd
4. Unpriv(非特権)フィールド
非特権ユーザーでの実行を制御します。
- y(yes) - postfixユーザーで実行(推奨)
- n(no) - root権限で実行
- -(ハイフン) - デフォルト値(通常はyes)を使用
セキュリティの観点から、可能な限りyに設定することが推奨されます。
5. Chroot(chroot)フィールド
chroot jail環境での実行を制御します。
- y(yes) - $queue_directory内でchroot実行
- n(no) - 通常環境で実行
- -(ハイフン) - デフォルト値を使用
# chroot環境で実行(セキュリティ向上)
smtp inet n - y - - smtpd
# 通常環境で実行(一部機能に必要)
virtual unix - n n - - virtual
注意点:
- chroot環境では、必要なライブラリやファイルへのアクセスが制限される
- DNS解決、SSL証明書アクセスなどで問題が発生する可能性がある
6. Wakeup(起動間隔)フィールド
定期的なプロセス起動の間隔を秒単位で指定します。
- 0 - ウェイクアップタイマーを使用しない
- 数値 - 指定秒数ごとに起動
- 数値? - 条件付き起動(メールがある場合のみ)
- -(ハイフン) - ウェイクアップなし
# 60秒ごとにローカルメールをチェック
pickup unix n - n 60 1 pickup
# 300秒ごとにキューを処理
qmgr unix n n n 300 1 qmgr
7. Maxproc(最大プロセス数)フィールド
同時実行可能な最大プロセス数を制限します。
- 数値 - 最大プロセス数
- 0 - 無制限
- -(ハイフン) - default_process_limit値を使用
# SMTP接続は最大100プロセスまで
smtp inet n - n - 100 smtpd
# クリーンアップは無制限
cleanup unix n - n - 0 cleanup
8. Command + Args(コマンドと引数)フィールド
実行するプログラムとオプション引数を指定します。
smtp inet n - n - - smtpd
-o content_filter=
-o receive_override_options=no_header_body_checks
主要サービスの詳細解説
SMTP受信サービス(ポート25)
標準的なメール受信サービスの設定:
smtp inet n - n - - smtpd
-o content_filter=
-o receive_override_options=no_header_body_checks
-o smtpd_client_restrictions=
-o smtpd_helo_restrictions=
-o smtpd_sender_restrictions=
-o smtpd_recipient_restrictions=permit_mynetworks,reject
設定のポイント:
- 外部からのメール受信を処理
- スパムフィルターなどのcontent_filterを適用可能
- 各種制限(restrictions)でアクセス制御
Submissionサービス(ポート587)
認証付きメール送信用サービス:
submission inet n - n - - smtpd
-o syslog_name=postfix/submission
-o smtpd_tls_security_level=encrypt
-o smtpd_sasl_auth_enable=yes
-o smtpd_tls_auth_only=yes
-o smtpd_reject_unlisted_recipient=no
-o smtpd_client_restrictions=$mua_client_restrictions
-o smtpd_helo_restrictions=$mua_helo_restrictions
-o smtpd_sender_restrictions=$mua_sender_restrictions
-o smtpd_recipient_restrictions=permit_sasl_authenticated,reject
-o milter_macro_daemon_name=ORIGINATING
重要な設定:
- TLS暗号化を強制(
smtpd_tls_security_level=encrypt) - SASL認証を有効化(
smtpd_sasl_auth_enable=yes) - 認証済みユーザーのみ許可
SMTPSサービス(ポート465)
SSL/TLS暗号化SMTP(レガシー):
smtps inet n - n - - smtpd
-o syslog_name=postfix/smtps
-o smtpd_tls_wrappermode=yes
-o smtpd_sasl_auth_enable=yes
-o smtpd_reject_unlisted_recipient=no
-o smtpd_client_restrictions=$mua_client_restrictions
-o smtpd_helo_restrictions=$mua_helo_restrictions
-o smtpd_sender_restrictions=$mua_sender_restrictions
-o smtpd_recipient_restrictions=permit_sasl_authenticated,reject
-o milter_macro_daemon_name=ORIGINATING
内部処理サービス
Postfixの内部処理を担当するサービス群:
# ローカルメール投入
pickup unix n - n 60 1 pickup
-o receive_override_options=no_header_body_checks
# メールクリーンアップ
cleanup unix n - n - 0 cleanup
# キューマネージャー
qmgr unix n n n 300 1 qmgr
# 代替キューマネージャー(oqmgr)
#qmgr unix n n n 300 1 oqmgr
# TLSセッションキャッシュ管理
tlsmgr unix - - n 1000? 1 tlsmgr
# アドレス書き換え
rewrite unix - - n - - trivial-rewrite
# バウンス処理
bounce unix - - n - 0 bounce
defer unix - - n - 0 bounce
trace unix - - n - 0 bounce
# 検証サービス
verify unix - - n - 1 verify
# フラッシュサービス
flush unix n - n 1000? 0 flush
# プロキシマップ
proxymap unix - - n - - proxymap
# プロキシライト
proxywrite unix - - n - 1 proxymap
配送エージェントの設定
SMTP配送
外部サーバーへのメール配送:
smtp unix - - n - - smtp
# 高速配送用(並列数増加)
relay unix - - n - - smtp
-o smtp_fallback_relay=
ローカル配送
システムユーザーへの配送:
local unix - n n - - local
仮想配送(Dovecot連携)
仮想ユーザーへの配送(Dovecot LDA使用):
dovecot unix - n n - - pipe
flags=DRhu user=vmail:vmail argv=/usr/lib/dovecot/deliver -f ${sender} -d ${user}@${nexthop}
仮想配送(Postfix内蔵)
virtual unix - n n - - virtual
コンテンツフィルターの統合
SpamAssassin統合
# マスターフィルター設定
smtp inet n - n - - smtpd
-o content_filter=spamassassin
# SpamAssassinフィルター定義
spamassassin unix - n n - - pipe
flags=R user=spamd argv=/usr/bin/spamc -e /usr/sbin/sendmail -oi -f ${sender} ${recipient}
Amavisd-new統合
# コンテンツフィルター設定
smtp inet n - n - - smtpd
-o content_filter=smtp-amavis:[127.0.0.1]:10024
# Amavisからの戻り
127.0.0.1:10025 inet n - n - - smtpd
-o content_filter=
-o local_recipient_maps=
-o relay_recipient_maps=
-o smtpd_restriction_classes=
-o smtpd_delay_reject=no
-o smtpd_client_restrictions=permit_mynetworks,reject
-o smtpd_helo_restrictions=
-o smtpd_sender_restrictions=
-o smtpd_recipient_restrictions=permit_mynetworks,reject
-o smtpd_data_restrictions=reject_unauth_pipelining
-o smtpd_end_of_data_restrictions=
-o mynetworks=127.0.0.0/8
-o smtpd_error_sleep_time=0
-o smtpd_soft_error_limit=1001
-o smtpd_hard_error_limit=1000
-o smtpd_client_connection_count_limit=0
-o smtpd_client_connection_rate_limit=0
-o receive_override_options=no_header_body_checks,no_unknown_recipient_checks,no_milters
Milterインターフェース
# OpenDKIM統合
smtpd pass - - n - - smtpd
-o smtpd_milters=inet:127.0.0.1:8891
-o non_smtpd_milters=inet:127.0.0.1:8891
ポリシーサービスの実装
Postfix Policy Daemon
# ポリシーサービス定義
policy unix - n n - - spawn
user=nobody argv=/usr/bin/perl /usr/local/lib/policyd-weight
# SMTPDでの利用
smtpd_recipient_restrictions =
...
check_policy_service unix:private/policy
...
Postgrey(グレイリスティング)
# Postgreyサービス
postgrey unix - n n - - spawn
user=postgrey argv=/usr/sbin/postgrey --unix=/var/spool/postfix/private/postgrey
パフォーマンスチューニング
並列処理の最適化
# 高トラフィックサイト向け設定
smtp inet n - n - 200 smtpd
-o smtpd_client_connection_count_limit=10
-o smtpd_client_connection_rate_limit=30
# 配送プロセスの並列化
smtp unix - - n - 20 smtp
-o smtp_destination_concurrency_limit=2
-o smtp_destination_rate_delay=1s
メモリ使用量の制御
# クリーンアッププロセスの制限
cleanup unix n - n - 5 cleanup
-o message_size_limit=10240000
# キューマネージャーの調整
qmgr unix n n n 300 1 qmgr
-o qmgr_message_active_limit=20000
-o qmgr_message_recipient_limit=20000
接続制限の実装
# レート制限付きSMTPD
smtp inet n - n - - smtpd
-o smtpd_client_connection_count_limit=10
-o smtpd_client_connection_rate_limit=30
-o smtpd_client_message_rate_limit=100
-o smtpd_client_recipient_rate_limit=200
-o anvil_rate_time_unit=60s
セキュリティベストプラクティス
1. Chroot環境の活用
# セキュアな設定例
smtp inet n - y - - smtpd
submission inet n - y - - smtpd
メリット:
- ファイルシステムアクセスの制限
- 攻撃時の被害範囲限定
デメリット:
- 設定の複雑化
- 一部機能の制限
2. 非特権実行の徹底
# 可能な限り非特権で実行
cleanup unix n y n - 0 cleanup
bounce unix - y n - 0 bounce
3. TLS/SSL暗号化の強制
# Submissionポートでの暗号化強制
submission inet n - n - - smtpd
-o smtpd_tls_security_level=encrypt
-o smtpd_tls_mandatory_protocols=!SSLv2,!SSLv3,!TLSv1,!TLSv1.1
-o smtpd_tls_mandatory_ciphers=high
-o tls_high_cipherlist=ECDHE+AESGCM:ECDHE+AES256:ECDHE+AES128:DHE+AESGCM:DHE+AES256:DHE+AES128:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4
4. 認証の実装
# SASL認証の設定
submission inet n - n - - smtpd
-o smtpd_sasl_auth_enable=yes
-o smtpd_sasl_type=dovecot
-o smtpd_sasl_path=private/auth
-o smtpd_sasl_security_options=noanonymous
-o smtpd_sasl_local_domain=$myhostname
トラブルシューティング
設定の検証
# 文法チェック
postfix check
# 設定の確認
postconf -n # main.cfの非デフォルト値
postconf -M # master.cfの設定
# 特定サービスの確認
postconf -M smtp/inet
設定の適用
# 設定の再読み込み(サービス継続)
postfix reload
# サービスの再起動(一時停止あり)
systemctl restart postfix
# 特定サービスのみ再起動
postfix reload smtp
ログの確認
# リアルタイムログ監視
tail -f /var/log/mail.log
# エラーログの抽出
grep "error\|warning\|fatal\|panic" /var/log/mail.log
# 特定サービスのログ
grep "postfix/smtpd" /var/log/mail.log
デバッグモードの活用
# デバッグ出力を有効化
smtp inet n - n - - smtpd
-o debug_peer_list=127.0.0.1
-o debug_peer_level=3
よくある問題と対処
1. "Connection refused"エラー
# ポートのリスニング確認
netstat -tlnp | grep :25
ss -tlnp | grep :25
# サービスの起動確認
postfix status
2. Chroot関連の問題
# chroot環境に必要なファイルをコピー
mkdir -p /var/spool/postfix/etc
cp /etc/resolv.conf /var/spool/postfix/etc/
cp /etc/services /var/spool/postfix/etc/
3. 権限関連の問題
# 権限の確認と修正
postfix set-permissions
chown -R postfix:postfix /var/spool/postfix
実践的な設定例
小規模オフィス向け設定
# 基本的なメール送受信設定
smtp inet n - y - 20 smtpd
submission inet n - y - 20 smtpd
-o syslog_name=postfix/submission
-o smtpd_tls_security_level=encrypt
-o smtpd_sasl_auth_enable=yes
-o smtpd_recipient_restrictions=permit_sasl_authenticated,reject
pickup unix n - n 60 1 pickup
cleanup unix n - n - 0 cleanup
qmgr unix n n n 300 1 qmgr
tlsmgr unix - - n 1000? 1 tlsmgr
rewrite unix - - n - - trivial-rewrite
bounce unix - - n - 0 bounce
defer unix - - n - 0 bounce
trace unix - - n - 0 bounce
verify unix - - n - 1 verify
flush unix n - n 1000? 0 flush
proxymap unix - - n - - proxymap
smtp unix - - n - 5 smtp
relay unix - - n - - smtp
showq unix n - n - - showq
error unix - - n - - error
retry unix - - n - - error
discard unix - - n - - discard
local unix - n n - - local
virtual unix - n n - - virtual
lmtp unix - - n - - lmtp
anvil unix - - n - 1 anvil
scache unix - - n - 1 scache
高可用性設定
# 複数IPでのリスニング
10.0.0.1:smtp inet n - n - 100 smtpd
10.0.0.2:smtp inet n - n - 100 smtpd
# バックアップMX設定
smtp inet n - n - - smtpd
-o smtpd_recipient_restrictions=permit_mynetworks,reject_unauth_destination
-o smtpd_relay_restrictions=permit_mynetworks,defer_unauth_destination
マルチインスタンス設定
# インスタンス1(社内メール)
smtp-internal inet n - n - 50 smtpd
-o myhostname=internal.example.com
-o smtpd_banner=$myhostname ESMTP Internal
# インスタンス2(外部メール)
smtp-external inet n - n - 100 smtpd
-o myhostname=mx.example.com
-o smtpd_banner=$myhostname ESMTP
-o content_filter=amavis:[127.0.0.1]:10024
master.cfカスタマイズのヒント
1. サービス固有の設定オーバーライド
-oオプションを使用して、main.cfの設定を上書きできます:
submission inet n - n - - smtpd
-o smtpd_enforce_tls=yes # TLS強制
-o smtpd_sasl_auth_enable=yes # SASL認証有効
-o smtpd_client_restrictions=permit_sasl_authenticated,reject
-o smtpd_sender_restrictions= # 送信者制限を無効化
-o smtpd_recipient_restrictions=permit_sasl_authenticated,reject
-o smtpd_relay_restrictions=permit_sasl_authenticated,reject
-o milter_macro_daemon_name=ORIGINATING # Milter用マクロ
2. カスタムサービスの追加
独自のサービスを追加する例:
# カスタムポリシーサーバー
mypolicy unix - n n - 0 spawn
user=nobody argv=/usr/local/bin/mypolicy-server
# カスタムフィルター
myfilter unix - n n - 10 pipe
flags=Rq user=filter argv=/usr/local/bin/myfilter -f ${sender} -- ${recipient}
3. 条件付き設定の実装
環境変数やmain.cfの変数を参照:
# main.cfで定義された変数を使用
submission inet n - n - - smtpd
-o smtpd_client_restrictions=$submission_client_restrictions
-o smtpd_sender_restrictions=$submission_sender_restrictions
-o smtpd_recipient_restrictions=$submission_recipient_restrictions
まとめ
master.cfは、Postfixの心臓部とも言える重要な設定ファイルです。適切に設定することで、以下のメリットが得られます:
- セキュリティの向上 - chroot、非特権実行、TLS強制など
- パフォーマンスの最適化 - 並列処理、リソース制限の調整
- 柔軟な機能拡張 - フィルター統合、カスタムサービスの追加
- 細かい制御 - サービスごとの個別設定
設定変更時は必ず以下を実行しましょう:
# 1. 設定の文法チェック
postfix check
# 2. 設定の適用
postfix reload
# 3. ログの確認
tail -f /var/log/mail.log
# 4. 動作テスト
echo "Test mail" | mail -s "Test" user@example.com
メールサーバーの運用は複雑ですが、master.cfの仕組みを理解することで、トラブルシューティングが容易になり、より安全で効率的なメールシステムを構築できます。
参考リンク
本記事は定期的に更新されます。最新のPostfixバージョンでは、一部設定が異なる場合があります。