LoginSignup
4

More than 3 years have passed since last update.

CentOS8.3にPostfixとDovecotをセットアップする手順

Last updated at Posted at 2020-12-20
../

VPSサーバー(CentOS8.3)にメールサーバーを構築してみた。PostfixとDovecotをセットアップする手順をメモしておく。Postfixはメールの送受信を行い、DovecotはクライアントPCからのメール受信を担うサービスである。以下の例では、mail.kankeri.com というサーバー名とする。

Postfixでは、SMTP認証にSASLを利用する。SASLにはいくつかの実装があるが、一般的なcyrus-saslを利用する。また、SASLはさまざまな認証のメカニズムに対応しているが、初期状態で選択されているものがPAM認証である。PAMは、UNIXのユーザーアカウントとパスワードを利用するものであり、loginやsuコマンドなどでも利用されている。

メールの送信や転送では、SMTP(25ポート)とSMTP-SUBMISSION(587ポート)を使い、STARTTLSで通信する。STARTTLSは、受信側がSTARTTLSに対応していれば暗号化して通信され、対応していなければ平文で通信する方式である。今回は、SMTPS(465ポート)は利用せず、またSSL/TSLは使用しないものとする。メールクライアントからの送信では、OP25B対策として25ポートではなく、587ポートを利用する。

メールの受信では、IMAP(143ポート)とPOP3(110ポート)を使い、STARTTLSで通信する。今回は、IMAPS(993ポート)やPOP3S(995ポート)は利用せず、またSSL/TSLは使用しないものとする。クライアントでサーバーからメールを取得した際に、メール本体をサーバーに残したままにするのがIMAPであり、サーバーに残さないのがPOP3である。Dovecotでは、IMAPとPOP3のどちらでも選択利用できるように設定する。

  • cyrus-saslのセットアップ
  • メールボックスの作成
  • DNSレコードの設定
  • PostfixとDovecotのインストール
  • Postfix:main.cfの設定
  • Postfix:master.cfの設定
  • Dovecotの設定
  • サービス起動とFirewall設定
  • メールクライアント(Thunderbird)からの送受信の確認

cyrus-saslのセットアップ

cyrus-saslをインストールする。SMTP認証にsaslauthdを指定し、メカニズムにPAMを指定する。デフォルトの設定のままである。/etc/passwdに登録されたユーザーとパスワードを使用して認証する。PAM以外には、sasldbという選択肢もあるが、今回は利用しない。sasldbは、/etc/passwdとは別に独自にユーザーとパスワードを登録して管理するものになる。ユーザー数が少ない場合はPAMで十分であり、ユーザー数が多くなるならsasldbを選択することになるのだろう。今回は、デフォルト設定でsaslauthd+PAMとする。

$ yum install cyrus-sasl cyrus-sasl-plain
$ saslauthd -v                          # pamが含まれていること
saslauthd 2.1.27
authentication mechanisms: getpwent kerberos5 pam rimap shadow ldap httpform

$ vi /etc/sasl2/smtpd.conf
pwcheck_method: saslauthd               # saslauthdであること
mech_list: plain login                  # plain loginであること

$ vi /etc/sysconfig/saslauthd 
MECH=pam                                # pamであること

$ systemctl start saslauthd
$ systemctl enable saslauthd

メールボックスの作成

PAM認証を使うので、UNIXのユーザーアカウントごとにメールボックスを作成しておく。既存のユーザーにはホームの配下に規定のディレクトリを作成し、未来のユーザー用にはスケルトンを設定しておく。また、存在しないユーザー宛のメールが届いた場合に不達メールを送信元へ返すことはせず、破棄するようにしたいので、unknown_userを/etc/aliasesに追加しておく。

# 例えば、既存のtaconanaユーザーの場合
$ su - taconana
$ mkdir -p Maildir/{new,cur,tmp}
$ chmod -R 700 Maildir

# 未来のユーザーのためにスケルトンを設定
$ su -
$ mkdir -p /etc/skel/Maildir/{new,cur,tmp}
$ chmod -R 700 /etc/skel/Maildir/

# 存在しないユーザー宛のメール対応
$ vi /etc/aliases
unknown_user: /dev/null

DNSレコードの設定と確認

メール送信(SMTP)も、メール受信(IMAP/POP3)も、今回はバーチャルホストで mail.kankeri.com とする。利用しているDNSプロバイダのWebページで、DNSレコードを以下の感じに設定しおく。

  • MX:kankeri.com
  • A:mail.kankeri.com

そして、DNSが機能していることをpingやnslookupコマンドなどで確認しておく。

$ su -
$ nslookup mail.kankeri.com

PostfixとDovecotのインストール

PostfixとDovecotをインストールする。また、MTA(メール転送エージェント)をpostfixに設定しておく。MTAとしてsendmailが選択されている場合があるので、念のため確認し、変更しておく。

$ yum install postfix
$ postconf | grep version
mail_version = 3.3.1

$ yum install dovecot
$ dovecot --version
2.3.8 (9df20d2db)

$ alternatives --config mta
1 プログラムがあり 'mta' を提供します。
  選択       コマンド
-----------------------------------------------
*+ 1           /usr/sbin/sendmail.postfix

Enter を押して現在の選択 [+] を保持するか、選択番号を入力します: 1

Postfix:main.cfの編集

/etc/postfix/main.cfを以下の感じで編集する。編集後にpostconf -nで確認するとよい。

$ vi /etc/postfix/main.cf
myhostname = mail.kankeri.com
mydomain = kankeri.com
myorigin = $mydomain

inet_interfaces = all
#inet_interfaces = localhost

#inet_protocols = all
inet_protocols = ipv4   # IPv4 のみ

#local_recipient_maps = unix:passwd.byname $alias_maps
local_recipient_maps =

mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
#mynetworks = 168.100.189.0/28, 127.0.0.0/8
mynetworks = 127.0.0.0/8, 10.0.0.0/24

home_mailbox = Maildir/

luser_relay = unknown_user@localhost

smtpd_banner = $myhostname ESMTP

# TLS CONFIGURATION
#smtpd_tls_cert_file = /etc/pki/tls/certs/postfix.pem
smtpd_tls_cert_file = /etc/letsencrypt/live/kankeri.com/fullchain.pem
#smtpd_tls_key_file = /etc/pki/tls/private/postfix.key
smtpd_tls_key_file = /etc/letsencrypt/live/kankeri.com/privkey.pem
smtpd_tls_security_level = may
#smtp_tls_CApath = /etc/pki/tls/certs
smtp_tls_CApath = 
#smtp_tls_CAfile = /etc/pki/tls/certs/ca-bundle.crt
smtp_tls_CAfile = 
smtp_tls_security_level = may

# 以下を末尾に追加
message_size_limit = 10485760
mailbox_size_limit = 1073741824

# SMTP-AUTHの設定
smtpd_sasl_auth_enable = yes
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_security_options = noanonymous
smtpd_sasl_local_domain = $myhostname
smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
#smtpd_recipient_restrictions = permit_mynetworks, permit_auth_destination, permit_sasl_authenticated, reject

# SSL/TLSの設定
smtpd_use_tls = yes
smtp_tls_protocols = !SSLv2, !SSLv3
smtp_tls_mandatory_protocols = !SSLv2, !SSLv3
smtpd_tls_protocols = !SSLv2, !SSLv3
smtpd_tls_mandatory_protocols = !SSLv2, !SSLv3
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache

# 変更された変数の確認
$ postconf -n

Postfix:master.cfの編集

/etc/postfix/master.cfは、以下の感じで編集する。SUBMISSIONを有効化して、SASL認証(smtpd_sasl_auth_enable=yes)を設定する。SMTPも活かしておくこと。SMTPSは、今回は使用しないのでコメントアウトのままにしておく。

# vi /etc/postfix/master.cf
smtp       inet n       -       n       -       -       smtpd
submission inet n       -       n       -       -       smtpd
  -o syslog_name=postfix/submission
# -o smtpd_tls_security_level=encrypt
  -o smtpd_sasl_auth_enable=yes
#smtps     inet n       -       n       -       -       smtpd
# -o syslog_name=postfix/smtps

Dovecotの設定

Dovecot の設定ファイルを編集する。

$ vi /etc/dovecot/dovecot.conf
#listen = *, :: 
listen = *         # IPv4 のみ

$ vi /etc/dovecot/conf.d/10-auth.conf
#disable_plaintext_auth = yes
disable_plaintext_auth = no
auth_mechanisms = plain login

$ vi /etc/dovecot/conf.d/10-mail.conf
mail_location = maildir:~/Maildir

$ vi /etc/dovecot/conf.d/10-master.conf
# Postfix smtp-auth
unix_listener /var/spool/postfix/private/auth {
  mode = 0666
  user = postfix
  group = postfix
}

$ vi /etc/dovecot/conf.d/10-ssl.conf
ssl = required
#ssl_cert = </etc/pki/dovecot/certs/dovecot.pem
#ssl_key = </etc/pki/dovecot/private/dovecot.pem
ssl_cert = </etc/letsencrypt/live/kankeri.com/fullchain.pem
ssl_key = </etc/letsencrypt/live/kankeri.com/privkey.pem
ssl_protocols = !SSLv2 !SSLv3

サービス起動とFirewall設定

PostfixとDovecotのサービスを起動し、自動実行可能に設定しておく。また、Firewallにsmtp(25ポート、smtp-submission(587ポート)、pop3(110ポート)、imap(143ポート)も設定しておく。

# サービス登録
$ systemctl start postfix
$ systemctl enable postfix
$ systemctl start dovecot
$ systemctl enable dovecot

# Firewall設定
$ firewall-cmd --add-service={smtp,smtp-submission,pop3,imap} --permanent
$ firewall-cmd --reload
$ firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0
  sources:
  services: dhcpv6-client http https imap mysql pop3 smtp smtp-submission ssh vnc-server
  ports: 8080/tcp 8443/tcp 8009/tcp
  protocols:

メールクライアント(Thunderbird)からの送受信の確認

例えば、WindowsPCのメールクライアントとしてThunderbirdを利用して確認してみる。アカウント作成時にメールアドレス(例えば、taconana@kankeri.com)を指定して進めると、自動でメールサーバー(mail.kankeri.com)を検出し、IMAP/POP3、ポート番号、STARTTLSかSSL/TLSか、パスワード認証かどうかを自動で判別してくれる。

  • 受信サーバー:mail.kankeri.com、POP3、110ポート、STARTTLS、通常のパスワード認証
  • 送信サーバー:mail.kankeri.com、SMTP、587ポート、STARTTLS、通常のパスワード認証

送信、受信のテストをする。STARTTLSだと、初回のメール送信時には「メッセージを送信できませんでした。Unable to communicate securely with peer: requested domain name does not match the server’s certificate. mail.kankeri.com に関する設定を見直してください」が出る。送信サーバーの「587ポート、STARTTLS」を確認して、再度試行する。「セキュリティ例外の追加」の警告が出たら「次回以降にもこの例外を有効にする」にチェックを入れて「セキュリティ例外を承認」をクリックする。パスワード入力画面に進んだら、入力して保存しておく。

以上

../

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4