Postfix

Postfixの設定例。
[最終更新日: 2009/02/04]

1. postfixの操作

| INDEX

サーバ操作の基本的なものを。

メールサーバ起動
> postfix start
設定再読み込み
> postfix reload
メールサーバ停止
> postfix stop
設定のチェック
> postfix check
キューの表示
> mailq
alias設定の更新( /etc/aliases.db )
> newaliases

2. postfixの設定(main.cf)

| | INDEX

単一のサーバでの運用の設定(MXレコードは使わない)

SMTP: 25 のポートが解放が必要。

# main.cf [一部]
[略]
# HOST_CONFIGURATION
mydomain = example.com
myhostname = example.com
myorigin = $mydomain
# mynetworks define.
mynetworks_style = subnet
mynetworks = 192.168.0.0/24, 127.0.0.0/8
inet_interfaces = all
# 自分で受け取るドメイン名の定義
mydestination = $myhostname
# 丸投げ先の指定
#relay_domains = $mydestination
[末尾]
# ユーザが存在しない場合の扱い
unknown_local_recipient_reject_code = 550 #即座に拒否
#unknown_local_recipient_reject_code = 450
smtpd_banner = $myhostname ESMTP unknown #サーバ名を隠蔽
smtpd_helo_required = yes
disable_vrfy_command = yes
strict_rfc821_envelopes = yes
# 以下の2つをyesにしないと、すべての中継を拒否する。
allow_percent_hack = yes
swap_bangpath = yes
#notify_classes = resource,software,policy
allow_untrusted_routing = no
smtpd_helo_restrictions = permit_mynetworks, reject_invalid_hostname, reject_unknown_client, permit
smtpd_client_restrictions = permit_mynetworks, reject_unknown_client, permit
smtpd_recipient_restrictions = permit_mynetworks,reject_unauth_destination

上記設定では mynetworks 以外からのメール送信(リレー)は禁止。

さらに不正中継拒否設定を強化する場合は、正規表現を用いたチェッカを足す。

# 一行で書く
smtpd_recipient_restrictions = 
regexp:/etc/postfix/recipient_checks.reg, permit_mynetworks,reject_unauth_destination

/etc/postfix/recipient_checks.reg を以下の内容で作成。

/[@!%].*[@!%]/ 550 Please use user@domain address forms only.

また、メールボックスをMaildir形式にする場合は、

#home_mailbox = Mailbox
home_mailbox = Maildir/
#mail_spool_directory = /var/mail
#mail_spool_directory = /var/spool/mail #コメントアウトする

メール一通(ヘッダ含む)あたりの容量の制限。

message_size_limit = 1024000 # 1Mb

メールボックスの容量の制限。

#mailbox_size_limit = 0      # 無制限の場合
mailbox_size_limit = 10240000

3. Telnetによるメールサーバテスト

| | INDEX

telnetコマンドを用いてメールサーバと会話してみる。

> telnet 192.168.1.16 25
Trying 192.168.1.16...
Connected to 192.168.1.16.
Escape character is '^]'.
220 example.com ESMTP Postfix
HELO example.com
250 example.com
MAIL FROM: <hoge@example.com>
250 Ok
RCPT TO: <foo@example.com>
250 Ok
DATA
354 Start mail input; end with <CRLF>.<CRLF>
Subject: test mail
From: <hoge@example.com>
Test Mail.
.
250 Ok: queued as DEFFDD010
QUIT
221 Bye
Connection closed by foreign host.

4. メールキューの保存期間

| | INDEX

デフォルト設定だと、キューに残る期間が結構長い気がするので。

以下は試験運用などでの気の短い設定例。

# 1時間に1回づつ試して5回まで再試行
minimal_backoff_time = 1h
maximal_backoff_time = 1h
maximal_queue_lifetime = 5h

5. メールの送信先ドメインを限定する

| INDEX

許可するドメインのリストを作成する。

# /etc/postfix/local_domains
pcnet.local OK
hatari.local OK

postmapコマンドでDBを作成する。

> postmap /etc/postfix/local_domains

/etc/postfix/main.cfを編集する。

smtpd_recipient_restrictions = 
    check_recipient_access hash:/etc/postfix/local_domains,reject

Postfixの設定をリロード。

> /etc/init.d/postfix reload
.