Postfix+Dovecotでメールサーバを構築する方法を書いた記事その2です.

送信用メールサーバをPostfixで構築していきます.

親記事

この記事は,以下の記事の一部です.

Postfix+Dovecotでメールサーバを構築する

手順

以下,ドメイン名はyourdomain.comとしていますので,適宜変えてください.

1. Postfixのインストール

以下のコマンドでインストールします.

$ sudo yum -y install postfix

2. 設定ファイルmain.cfの書き換え

設定ファイル/etc/postfix/main.cfを以下のように書き換えます.

()
# INTERNET HOST AND DOMAIN NAMES
()
#
#myhostname = virtual.domain.tld
myhostname = mail.yourdomain.com  # <- ホスト名の設定を追加

# The mydomain parameter specifies the local internet domain name.
()
#
mydomain = yourdomain.com           # <- ドメイン名の設定を追加

# SENDING MAIL
#
# The myorigin parameter specifies the domain that locally-posted
()
#
#myorigin = $myhostname
myorigin = $mydomain                # <- メールアドレスの@以降を設定.

# RECEIVING MAIL

# The inet_interfaces parameter specifies the network interface
()
#
inet_interfaces = all               # <- allのコメントアウトを削除
#inet_interfaces = $myhostname
#inet_interfaces = $myhostname, localhost
#inet_interfaces = localhost

# The mydestination parameter specifies the list of domains that this
()
#
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain # <- 変更
#mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
#mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain,
#       mail.$mydomain, www.$mydomain, ftp.$mydomain
()
# DELIVERY TO MAILBOX
#
# The home_mailbox parameter specifies the optional pathname of a
()
#
#home_mailbox = Mailbox
home_mailbox = Maildir/             # <- コメントアウトを削除
()
# SHOW SOFTWARE VERSION OR NOT
#
# The smtpd_banner parameter specifies the text that follows the 220
()
#
#smtpd_banner = $myhostname ESMTP $mail_name
#smtpd_banner = $myhostname ESMTP $mail_name ($mail_version)
smtpd_banner = $myhostname ESMTP unknown # <- 追加
()
# 以下最終行の後に追加
# SMTP 認証設定
smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = $myhostname
# 認証にDovecotを使用
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth

smtpd_recipient_restrictions =
        permit_mynetworks
        permit_sasl_authenticated
        reject_unauth_destination

# 受信メールサイズ制限
message_size_limit = 10485760

# 存在しないユーザー宛メールをunknown_userへ配送
local_recipient_maps =
luser_relay = unknown_user@localhost

# smtpセキュリティ設定(SSL暗号化)
smtp_tls_security_level = may
smtpd_tls_security_level = may
smtpd_use_tls = yes
smtpd_tls_cert_file = /etc/letsencrypt/live/mail.yourdomain.com/fullchain.pem # <- Let's Encryptで取得したfullchain
smtpd_tls_key_file = /etc/letsencrypt/live/mail.yourdomain.com/privkey.pem    # <- Let's Encryptで取得したprivate key
smtpd_tls_session_cache_database = btree:/var/lib/postfix/smtpd_scache
smtpd_tls_received_header = yes

3. 設定ファイルmaster.cfの書き換え

次に,設定ファイル/etc/postfix/mastrer.cfで,以下の2行を有効化(先頭の#を消す)します.

# ==========================================================================
# service type  private unpriv  chroot  wakeup  maxproc command + args
#               (yes)   (yes)   (yes)   (never) (100)
# ==========================================================================
#smtp      inet  n       -       n       -       -       smtpd
#smtp      inet  n       -       n       -       1       postscreen
#smtpd     pass  -       -       n       -       -       smtpd
#dnsblog   unix  -       -       n       -       0       dnsblog
#tlsproxy  unix  -       -       n       -       0       tlsproxy
#submission inet n       -       n       -       -       smtpd
#  -o syslog_name=postfix/submission
#  -o smtpd_tls_security_level=encrypt
#  -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
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

4. SMTP認証の設定

次に,SMTP認証の設定をします./etc/sasl2/smtpd.confの1行目を以下のように書き換えます.

pwcheck_method: auxprop

5. Maildir形式でのメール受信設定

Maildir形式でメールを受信するように設定します.新規ユーザを作ったときに/home/new_user/Maildir/直下にnewcurtmpという3つのフォルダができるようにします.

$ sudo mkdir -p /etc/skel/Maildir/{new,cur,tmp}
$ chmod -R 700 /etc/skel/Maildir

6. 存在しないメールアドレス宛メール破棄設定

最後に,存在しないメールアドレス宛のメールを破棄するようにします.

$sudo echo unknown_user: /dev/null >> /etc/aliases
$sudo newaliases

以上で完了です.お疲れ様でした.

次の記事

執筆中です.