お名前.comでVPS+CentOS7+postfix+dovecotでメールサーバーの設定1
お名前.comでVPS+nginx+PHP7+MariaDB+WordPressでサイト構築、お名前.comでVPS+nginx+PHP7+phpMyAdmin+vsftpdの設定、お名前.comのVPS+CentOS7+nginxで、サイトのSSL化(Let's encrypt)に続き、postfixとdovecotでメールサーバーの設定を行いました。 ドメインはムームードメインで取得しています。
事前準備
お名前.comのドメイン管理画面で、CNAMEやMXレコードの設定を行っておきます。
参考:ムームードメインで取得したドメインを、お名前.comで利用する方法
現在、ウェブサーバーが起動している場合には、あらかじめ、email.xxx.comというurlにアクセスされたときに、エラーが返らないように、email.xxx.com用のドキュメントルートを準備し、apacheやnginxの設定を行っておきます。
参考:お名前.comでVPS+nginx+PHP7+MariaDB+WordPressでサイト構築>2-6.WordPressのインストール(?)と設定
メールサーバーが使用するポートの設定
メールの送受信に使用するポートを開けておきます。
SMTP:25
SMTPS:465
IMAPS:993
POP3S:995
# firewall-cmd --add-port=25/tcp --permanent
# firewall-cmd --add-port=465/tcp --permanent
# firewall-cmd --add-port=993/tcp --permanent
# firewall-cmd --add-port=995/tcp --permanent
# firewall-cmd --reload
postfixとdovecotのインストール
postfixとdovecotをインストールします。
# yum -y install postfix
# yum -y install dovecot
Let's Encrypt のSSL/TLSサーバー証明書の取得
基本的な流れは、お名前.comのVPS+CentOS7+nginxで、サイトのSSL化(Let's encrypt)/a>と同じです。
postfixの設定
/etc/postfix/main.cfファイルの設定
オリジナルファイルを別名保存しておきます。
# cp -ip /etc/postfix/main.cf /etc/postfix/main.cf.ori
main.cfファイルを以下のように設定しなおします。
# vim /etc/postfix/main.cf
+++++++++++以下、main.cfの修正部分をピックアップ+++++++++++
#メールサーバのホスト名
#myhostname = host.domain.tld
↓
myhostname = mail.xxx.com
#メールサーバのドメイン名
#mydomain = domain.tld
↓
mydomain = xxx.com
#メールアドレスの形式を、「username@ドメイン名」にする
#myorigin = $mydomain
↓
myorigin = $mydomain
# 全てのホストからメールを受信可能にする
inet_interfaces = localhost
↓
inet_interfaces = all
# xxx.comのドメイン宛のメールも受信可能にする
mydestination = $myhostname, localhost.$mydomain, localhost
↓
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
# 存在しないメールアドレス宛のメールの受信を拒否する
#local_recipient_maps = proxy:unix:passwd.byname $alias_maps
↓
local_recipient_maps = proxy:unix:passwd.byname $alias_maps
# リレー中継を許可する宛先ドメインを指定
#relay_domains = $mydestination
↓
relay_domains = $mydestination
# 転送サーバを使わないようにする
#relayhost = [an.ip.add.ress]
↓
relayhost =
# メールの格納フォーマットをMaildir形式にする
#home_mailbox = Maildir/
↓
home_mailbox = Maildir/
# 不要な情報を非公開
#smtpd_banner = $myhostname ESMTP $mail_name
↓
smtpd_banner = $myhostname ESMTP
+++++++++++以下、main.cfの最終行に追加+++++++++++
# VRFYコマンドの禁止
disable_vrfy_command = yes
# MailBoxの最大サイズの指定
# Maildir形式にしたので、本来は不要だが、message_size_limitのために設定
mailbox_size_limit = 204800000
# 受信メールサイズの制限
# mailbox_size_limitより少ない値
message_size_limit = 10240000
##########スパム対策##########
# スパムメール対策:接続元の制限
smtpd_client_restrictions =
check_client_access hash:/etc/postfix/access
#zen.spamhaus.orgは、ドコモからのアクセスをSPAMにしてしまうことがあるらしいのでとりあえず設定から排除 # reject_rbl_client zen.spamhaus.org
reject_rbl_client all.rbl.jp
reject_non_fqdn_sender
reject_unknown_sender_domain
# スパムメール対策:エンベロープアドレス(MAIL FROM)の制限
smtpd_sender_restrictions =
reject_rhsbl_sender zen.spamhaus.org
reject_unknown_sender_domain
##########SMTP-Auth関連##########
# SASL認証を有効化
smtpd_sasl_auth_enable = yes
# Dovecot SASL ライブラリを指定
smtpd_sasl_type = dovecot
# Dovecot SASL ライブラリの認証ソケットファイル
# /var/spool/postfix/ からの相対パスで記述
smtpd_sasl_path = private/auth
# 古いバージョンの AUTH コマンド (RFC 2554) を利用している
# SMTP クライアントからも使えるようにする
broken_sasl_auth_clients = yes
##########TLS/SSL関連########## # TLSを有効化
smtpd_use_tls = yes
# 宛先のメールサーバがTLSに対応していれば、通信を暗号化する
smtp_tls_security_level = may
# サーバ証明書と秘密鍵を指定
smtpd_tls_cert_file = /etc/letsencrypt/live/mail.xxx.com/fullchain.pem
smtpd_tls_key_file = /etc/letsencrypt/live/mail.xxx.com/privkey.pem
# TLSログレベルの設定
# 0:出力しない
# 1:TLSハンドシェイクと証明書情報
# 2:TLSネゴシエーションの全て
smtpd_tls_loglevel = 1
# 暗号に関する情報を "Received:" メッセージヘッダに含める
smtpd_tls_received_header = yes
# 接続キャッシュファイルの指定
smtpd_tls_session_cache_database = btree:/var/lib/postfix/smtpd_scache
# キャッシュの保持時間の指定
smtpd_tls_session_cache_timeout = 3600s
# 認証を通過したものはリレーを許可する(permit_sasl_authenticated)
smtpd_recipient_restrictions =
permit_mynetworks
permit_sasl_authenticated
reject_unauth_destination
smtps(SMTP-Auth over SSL)の有効化
オリジナルファイルを別名保存しておきます。
# cp -ip /etc/postfix/master.cf /etc/postfix/master.cf.ori
master.cfファイルを以下のように設定しなおします。
修正箇所は、smtpsの部分で、いくつかの項目のコメントアウトを削除するだけです
# vim /etc/postfix/master.cf
+++++++++++以下、master.cfの修正部分+++++++++++
#この上には、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
+++++++++++以下略+++++++++++
※smtpの部分ではなく、smtpsの部分の設定を変更します。
-o smtpd_recipient_restrictions=permit_sasl_authenticated の部分は、最後の「,reject」を削除しないと、送信メールが
Diagnostic-Code: smtp; 554 5.7.1 <xxx@xxx.com>: Recipient address rejected: Access denied
と返されてしまうので、注意
ルックアップテーブルの作成
# postmap /etc/postfix/access
設定チェック
# postfix check
postfixの起動と自動起動設定
# systemctl start postfix
# systemctl enable postfix
dovecotの設定
/etc/dovecot/dovecot.conf の設定
# vim /etc/dovecot/dovecot.conf
############修正部分############
protocols = imap pop3 lmtp
↓
protocols = imap pop3 imap3 pop3s
/etc/dovecot/conf.d/10-master.conf の設定
# vim /etc/dovecot/conf.d/10-master.conf
############修正部分############
service imap-login {
inet_listener imap {
#port = 143
↓
port = 0
}
inet_listener imaps {
#port = 993
#ssl = yes
↓
port = 993
ssl = yes
}
}
service pop3-login {
inet_listener pop3 {
#port = 110
↓
port = 0
}
inet_listener pop3s {
#port = 995
#ssl = yes
↓
port = 995
ssl = yes
}
}
/etc/dovecot/conf.d/10-master.conf の設定
# vim /etc/dovecot/conf.d/10-master.conf
############修正部分############
service auth {
# Postfix smtp-auth
#unix_listener /var/spool/postfix/private/auth {
# mode = 0666
#}
↓
unix_listener /var/spool/postfix/private/auth {
mode = 0660
user = postfix
group = postfix
}
}
/etc/dovecot/conf.d/10-auth.conf の設定
# vim /etc/dovecot/conf.d/10-auth.conf
############修正部分############
#disable_plaintext_auth = yes
↓
disable_plaintext_auth = no
auth_mechanisms = plain
↓
auth_mechanisms = plain login
/etc/dovecot/conf.d/10-ssl.conf の設定
# vim /etc/dovecot/conf.d/10-ssl.conf
############修正部分############
ssl = required
↓
ssl = yes
#ssl_cert = </etc/pki/dovecot/certs/dovecot.pem
#ssl_key = </etc/pki/dovecot/private/dovecot.pem
↓
ssl_cert = </etc/letsencrypt/live/mail.xxx.com/fullchain.pem
ssl_key = </etc/letsencrypt/live/mail.xxx.com/privkey.pem
/etc/dovecot/conf.d/10-mail.conf の設定
# vim /etc/dovecot/conf.d/10-mail.conf
############修正部分############
#mail_location =
↓
mail_location = maildir:~/Maildir
/etc/dovecot/conf.d/10-logging.conf の設定
# vim /etc/dovecot/conf.d/10-logging.conf
############修正部分############
#log_path = syslog
↓
log_path = /var/log/dovecot/dovecot.log
同時にログの出力先を準備しておきます。
# mkdir /var/log/dovecot
dovecotの再起動
dovecotの再起動と自動起動の設定をしておきます。
# systemctl start dovecot
# systemctl enable dovecot
########認証ソケットファイルが作成されているのを確認########
# ls -F /var/spool/postfix/private/auth
########下記表示があれば大丈夫)########
/var/spool/postfix/private/auth =
ログローテーションの設定
【補足】ユーザーについて
ユーザーの追加は以下のように行う。※新規アカウントはログインユーザーではないとする。
# useradd -s /sbin/nologin info
passwd
一部のユーザー名は既にシステムで使用されている。(例:infoなど) そのため、メールを送信しても、目的のメールフォルダに保存されずにrootに移されてしまい、クライアント側でメールの確認ができなくなる。
Oct 21 13:09:58 vxxx-xx-xxx-xx postfix/smtpd[31582]: 353F312DEA: client=mail-it1-f170.google.com[xxx.xx.xxx.xxx]
Oct 21 13:09:58 vxxx-xx-xxx-xx postfix/cleanup[31588]: 353F312DEA: message-id=<CAKaggxC_fZiEXXkj-n6zJrjfoA=WST7DUOWvYAHEf9JW=sRc=g@mail.gmail.com>
Oct 21 13:09:58 vxxx-xx-xxx-xx postfix/qmgr[31278]: 353F312DEA: from=<hogehoge@hogehoge.jp>, size=2354, nrcpt=1 (queue active)
Oct 21 13:09:58 vxxx-xx-xxx-xx postfix/local[31589]: 353F312DEA: to=<root@xxx.com>, orig_to=<info@xxx.com>, relay=local, delay=0.31, delays=0.3/0.01/0/0, dsn=2.0.0, status=sent (delivered to maildir)
Oct 21 13:09:58 vxxx-xx-xxx-xx postfix/qmgr[31278]: 353F312DEA: removed
その場合はaliasのリストから、infoをコメントアウトしてnewaliasesをおこなう。
# vim /etc/aliases
########下記表示があれば大丈夫)########
info: postmaster
↓
#info: postmaster
########保存後########
# newaliases