CentOS7 メール設定 postfix dovecot

DNS設定

MXレコード、メールサーバのAレコード設定
メールサーバーのサブドメイン「mail」は任意

1
2
3
ホスト名            TYPE  TTL  VALUE              優先
mail.lists111.com A     3600 133.130.68.33
lists111.com      MX    3600 mail.lists111.com  10

ポート番号を使用しているプロセスを確認

1
netstat -tanp

メールサーバが使用するポート開放

  • SMPT(25)
  • SMTPS(465)
  • IMAPS(993)
  • POP3S(995)
01
02
03
04
05
06
07
08
09
10
11
# 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
 
// 確認
# firewall-cmd --list-ports
465/tcp 995/tcp 25/tcp 993/tcp

基本パッケージ、開発ツールインストール

1
2
3
# yum -y groupinstall base
# yum -y groupinstall development
# yum -y groupinstall network-tools

Postfix、Dovecotインストール

01
02
03
04
05
06
07
08
09
10
11
12
# yum -y install postfix
// 確認
# postconf | grep mail_version
// 自動起動設定
# systemctl enable postfix.service
// 起動
# systemctl start postfix.service
 
# yum -y install dovecot
# dovecot --version
# systemctl enable dovecot
# systemctl start dovecot

mail.lists111.comの Let’s EncryptのSSL/TLS証明書取得

※Let’s Encryptの初期設定済 (設定方法

1
2
3
4
# systemctl stop httpd.service
# cd /home/ユーザー名/letsencrypt
# ./letsencrypt-auto certonly -a standalone -d mail.lists111.com
# systemctl start httpd.service

Postfix の設定

ファイルのバックアップ
1
cp /etc/postfix/main.cf /etc/postfix/main.cf.org
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
vi /etc/postfix/main.cf
 
# メールサーバのホスト名(FQDN)指定
#myhostname = host.domain.tld
myhostname = mail.lists111.com
 
# メールサーバのドメイン名指定
#mydomain = domain.tld
mydomain = lists111.com
 
# メールアドレスの形式:ユーザ名@ドメイン名
#myorigin = $mydomain
myorigin = $mydomain
 
# 全てのホストからメール受信
inet_interfaces = localhost
inet_interfaces = all
 
# mydomain で指定したドメイン宛のメールを受信
mydestination = $myhostname, localhost.$mydomain, localhost
#mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
#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 =
 
# メールの格納フォーマット指定
#home_mailbox = Maildir/
home_mailbox = Maildir/
 
# 不要な情報を非公開
#smtpd_banner = $myhostname ESMTP $mail_name
smtpd_banner = $myhostname ESMTP
 
##### 最下部に追加 #####
 
# VRFYコマンド禁止
disable_vrfy_command = yes
 
# MailBoxの最大サイズ
# メールボックスがMaildir形式のためこの設定は無視されるが「message_size_limit」との兼ね合いのため設定
mailbox_size_limit = 204800000
 
# 受信メールサイズの制限「mailbox_size_limit」より少ない値
message_size_limit = 5120000
 
# 接続元の制限(スパムメール対策)
smtpd_client_restrictions =
    check_client_access hash:/etc/postfix/access
    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.lists111.com/fullchain.pem
smtpd_tls_key_file = /etc/letsencrypt/live/mail.lists111.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)有効化

ファイルのバックアップ

1
# cp /etc/postfix/master.cf /etc/postfix/master.cf.org
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# vi /etc/postfix/master.cf
 
#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
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

ルックアップテーブル作成

1
# postmap /etc/postfix/access

設定に誤りがないかチェック

問題ない場合は表示なし

1
# postfix check

Postfix再起動

1
# systemctl restart postfix

Dovecot の設定

受信プロトコル設定

1
2
3
4
5
# vi /etc/dovecot/dovecot.conf
 
#protocols = imap pop3 lmtp
protocols = imap pop3

暗号化を使う imaps と pop3s を有効、
平文で通信する imap と pop3 は「port = 0」を設定し無効に

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# cp /etc/dovecot/conf.d/10-master.conf /etc/dovecot/conf.d/10-master.conf.org
# vi /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
  }
}

Dovecot SASL ライブラリの認証ソケットファイル指定

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
# vi /etc/dovecot/conf.d/10-master.conf
 
service auth {
・・・
  # Postfix smtp-auth
  #unix_listener /var/spool/postfix/private/auth {
  #  mode = 0666
  #}
・・・
}
service auth {
・・・
  # Postfix smtp-auth
  unix_listener /var/spool/postfix/private/auth {
    mode = 0660
    user = postfix
    group = postfix
  }
・・・
}

認証方式設定

(平文パスワードを許可しだが、SSL/TLSで暗号化されますので問題ない)

01
02
03
04
05
06
07
08
09
10
# cp /etc/dovecot/conf.d/10-auth.conf /etc/dovecot/conf.d/10-auth.conf.org
# vi /etc/dovecot/conf.d/10-auth.conf
 
#disable_plaintext_auth = yes
disable_plaintext_auth = no
 
auth_mechanisms = plain
auth_mechanisms = plain login

SSL/TLSの有効化とサーバ証明書と秘密鍵指定

01
02
03
04
05
06
07
08
09
10
11
12
# cp /etc/dovecot/conf.d/10-ssl.conf /etc/dovecot/conf.d/10-ssl.conf.org
# vi /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.lists.com/fullchain.pem
ssl_key = </etc/letsencrypt/live/mail.lists.com/privkey.pem

メールボックスの場所指定

1
2
3
4
5
6
# cp /etc/dovecot/conf.d/10-mail.conf /etc/dovecot/conf.d/10-mail.conf.org
# vi /etc/dovecot/conf.d/10-mail.conf
 
#mail_location =
mail_location = maildir:~/Maildir

ログの出力先変更

1
2
3
4
5
6
# cp /etc/dovecot/conf.d/10-logging.conf /etc/dovecot/conf.d/10-logging.conf.org
# vi /etc/dovecot/conf.d/10-logging.conf
 
#log_path = syslog
log_path = /var/log/dovecot/dovecot.log

ログの出力先作成

1
# mkdir /var/log/dovecot

Dovecot 再起動

1
# systemctl restart dovecot

journalctl -u dovecot.service

/var/spool/postfix/private/auth が作成されているか確認

1
2
3
4
5
6
7
# ls -l
・・・
srw-rw-rw- 1 postfix postfix 0  1月  7 23:04 auth
・・・
 
// ファイルが作成されない場合はエラーログを確認
# journalctl -u dovecot.service

ログのローテーション設定

1日ごとにファイル名に日付、保存期間60日。

Postfixログの設定

出力先ディレクトリ作成

1
# mkdir /var/log/mail

出力先の変更

1
2
3
4
5
# vm /etc/rsyslog.conf
 
mail.*        -/var/log/maillog
mail.*        -/var/log/mail/maillog

syslog再起動

1
# systemctl restart rsyslog

不要なログ削除

1
# rm -f /var/log/maillog*

ログローテーション設定

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
# /etc/logrotate.d/syslog
// 下記削除
/etc/logrotate.d/syslog
 
// 新規作成
# vi /etc/logrotate.d/maillog
 
/var/log/mail/maillog {
    daily
    missingok
    dateext
    rotate 60
    sharedscripts
    postrotate
        /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
    endscript
}

確認

1
# logrotate -dv /etc/logrotate.d/maillog

下記のような表示でOK

1
2
rotating pattern: /var/log/mail/maillog after 1 days (60 rotations)
empty log files are rotated, old logs are removed

Dovecotログの設定

01
02
03
04
05
06
07
08
09
10
11
12
13
// 新規作成
# /etc/logrotate.d/dovecot
 
/var/log/dovecot/dovecot.log {
    daily
    missingok
    dateext
    rotate 60
    sharedscripts
    postrotate
        /bin/kill -USR1 `cat /var/run/dovecot/master.pid 2>/dev/null` 2> /dev/null || true
    endscript
}

確認

下記のような表示でOK

1
2
rotating pattern: /var/log/mail/maillog after 1 days (60 rotations)
empty log files are rotated, old logs are removed

メールアカウント作成

contact@lists111.com を作成
シェルログインは必要ないので/sbin/nologinを指定

1
2
# useradd -s /sbin/nologin contact
# passwd contact

メーラー設定

受信サーバー

・ユーザー名
contact
・サーバー
IMAP
・ホスト
mail.lists111.com
・ポート
993
・SSL
SSL/TLS
・認証方式
通常のパスワード認証
・ユーザー名
contact
・サーバー
SMTP
・ホスト
mail.lists111.com
・ポート
465
・SSL
SSL/TLS
・認証方式
通常のパスワード認証

第三者中継チェック

・第三者中継チェック RBL.JP
http://www.rbl.jp/svcheck.php

  • mail.lists111.com 入力
  • 下記表示でOK
  • 第三者中継テストの結果
    全てのテストが行われました, no relays accepted.

SPFレコードの設定

v=spf1 は固定。
ip4: IPアドレス
[/text]

・ホスト名
[空欄].lists111.com
・TYPE
・TTL
300
・VALUE
v=spf1 ip4:133.130.68.33 -all

・確認(時間を置いて)

1
2
3
4
5
# dig lists111.com txt
 
・・・
;; ANSWER SECTION:
lists111.com.  300  IN  TXT  "v=spf1 ip4:133.130.68.33 -all"

・メールのソース
下記があれがOK

1
Received-SPF: pass ・・・

参考にさせて頂いたサイト

あぱーブログ

SSLメールサーバ構築メモ Let’s Encrypt+Postfix+Dovecot

PAGE TOP