メールサーバー構築(Postfix+Dovecot)

最終更新日: 2023.02.23

■概要

メールサーバーを構築する場合、送信メールサーバー(SMTPサーバー)と受信メールサーバー(POP/IMAPサーバー)の構築が必要となる。
ここでは、送信メールサーバーにはPostfix、受信メールサーバーにはDovecotを採用する。

なお、Postfixは迷惑メールの不正中継に利用されないようにするため、初期設定では外部から外部宛(例えば、会社等で自宅サーバーのメールアドレスからプロバイダのメールアドレス宛)に送信できないようになっているが、迷惑メールの不正中継に利用されずに外部から外部宛に送信できるようにするため、SMTP認証機能※を有効にする。
また、DovecotはPOP/IMAPサーバーとして構築し、ユーザーがPOPまたはIMAPを選択できるようにする。※POPとIMAPの違い

※SMTP認証機能とは、メール送信時にユーザー名とパスワードで認証を行なう機能であり、これにより、迷惑メールの不正中継に利用されないようにする。


■Postfixインストール

[root@centos ~]# yum -y install postfix ← Postfixインストール

■Postfix設定

(1)Postfix設定
[root@centos ~]# vi /etc/postfix/main.cf ← Postfix設定ファイル編集
# INTERNET HOST AND DOMAIN NAMES
#
# The myhostname parameter specifies the internet hostname of this
# mail system. The default is to use the fully-qualified domain name
# from gethostname(). $myhostname is used as a default value for many
# other configuration parameters.
#
#myhostname = host.domain.tld
#myhostname = virtual.domain.tld
myhostname = mail.centossrv.com ← 追加(自FQDN名を指定)

# The mydomain parameter specifies the local internet domain name.
# The default is to use $myhostname minus the first component.
# $mydomain is used as a default value for many other configuration
# parameters.
#
#mydomain = domain.tld
mydomain = centossrv.com ← 追加(自ドメイン名を指定)

# SENDING MAIL
#
# The myorigin parameter specifies the domain that locally-posted
# mail appears to come from. The default is to append $myhostname,
# which is fine for small sites.  If you run a domain with multiple
# machines, you should (1) change this to $mydomain and (2) set up
# a domain-wide alias database that aliases each user to
# user@that.users.mailhost.
#
# For the sake of consistency between sender and recipient addresses,
# myorigin also specifies the default domain name that is appended
# to recipient addresses that have no @domain part.
#
#myorigin = $myhostname
#myorigin = $mydomain
myorigin = $mydomain ← 追加(ローカルからのメール送信時の送信元メールアドレス@以降にドメイン名を付加)

# The inet_interfaces parameter specifies the network interface
# addresses that this mail system receives mail on.  By default,
# the software claims all active interfaces on the machine. The
# parameter also controls delivery of mail to user@[ip.address].
#
# See also the proxy_interfaces parameter, for network addresses that
# are forwarded to us via a proxy or network address translator.
#
# Note: you need to stop/start Postfix when this parameter changes.
#
#inet_interfaces = all
#inet_interfaces = $myhostname
#inet_interfaces = $myhostname, localhost
inet_interfaces = localhost
↓
inet_interfaces = all ← 変更(外部からのメール受信を許可)

# The mydestination parameter specifies the list of domains that this# machine considers itself the final destination for.
#
# These domains are routed to the delivery agent specified with the
# local_transport parameter setting. By default, that is the UNIX
# compatible delivery agent that lookups all recipients in /etc/passwd
# and /etc/aliases or their equivalent.
#
# The default is $myhostname + localhost.$mydomain.  On a mail domain
# gateway, you should also include $mydomain.
#
# Do not specify the names of virtual domains - those domains are
# specified elsewhere (see VIRTUAL_README).
#
# Do not specify the names of domains that this machine is backup MX
# host for. Specify those names via the relay_domains settings for
# the SMTP server, or use permit_mx_backup if you are lazy (see
# STANDARD_CONFIGURATION_README).
#
# The local machine is always the final destination for mail addressed
# to user@[the.net.work.address] of an interface that the mail system
# receives mail on (see the inet_interfaces parameter).
#
# Specify a list of host or domain names, /file/name or type:table
# patterns, separated by commas and/or whitespace. A /file/name
# pattern is replaced by its contents; a type:table is matched when
# a name matches a lookup key (the right-hand side is ignored).
# Continue long lines by starting the next line with whitespace.
#
# See also below, section "REJECTING MAIL FOR UNKNOWN LOCAL USERS".
#
mydestination = $myhostname, localhost.$mydomain, localhost
↓
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
# mailbox file relative to a user's home directory. The default
# mailbox file is /var/spool/mail/user or /var/mail/user.  Specify
# "Maildir/" for qmail-style delivery (the / is required).
#
#home_mailbox = Mailbox
#home_mailbox = Maildir/
home_mailbox = Maildir/ ← 追加(メール格納形式をMaildir形式にする)

# SHOW SOFTWARE VERSION OR NOT
#
# The smtpd_banner parameter specifies the text that follows the 220
# code in the SMTP server's greeting banner. Some people like to see
# the mail version advertised. By default, Postfix shows no version.
#
# You MUST specify $myhostname at the start of the text. That is an
# RFC requirement. Postfix itself does not care.
#
#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_recipient_restrictions =
    permit_mynetworks
    permit_sasl_authenticated
    reject_unauth_destination

以下を最終行へ追加(受信メールサイズ制限)
message_size_limit = 10485760 ← 追加(受信メールサイズを10MB=10*1024*1024に制限)

[root@centos ~]# vi /etc/postfix/master.cf ← master.cf編集
submission inet n       -       n       -       -       smtpd ← 行頭の#を削除してコメント解除(SUBMISSIONポート有効化)
#  -o syslog_name=postfix/submission
#  -o smtpd_tls_security_level=encrypt
  -o smtpd_sasl_auth_enable=yes ← 行頭の#を削除してコメント解除(SUBMISSIONポートでSMTP認証有効化)

(2)SMTP認証設定
SMTP認証にシステムのユーザー名、パスワードを使用する場合
[root@centos ~]# yum -y install cyrus-sasl ← cyrus-saslインストール

[root@centos ~]# systemctl start saslauthd ← saslauthd起動

[root@centos ~]# systemctl enable saslauthd ← saslauthd自動起動設定



SMTP認証に個別のユーザー名、パスワードを使用する場合
[root@centos ~]# vi /etc/sasl2/smtpd.conf ← SMTP認証設定ファイル編集
pwcheck_method: saslauthd
↓
pwcheck_method: auxprop ← 変更

(3)Maildir形式メールボックス作成
Postfixのメール格納形式は共有ディレクトリ形式(「/var/spool/mail/ユーザー名」というファイルに全てのメールが蓄積されていく形式)だが、アクセス性能改善及びセキュリティ強化の観点からMaildir形式へ移行する。

【新規ユーザー対処】
新規ユーザー追加時に自動でホームディレクトリにMaildir形式のメールボックスが作成されるようにする
[root@centos ~]# mkdir -p /etc/skel/Maildir/{new,cur,tmp} ← 新規ユーザー追加時に自動でMaildir形式メールボックス作成

[root@centos ~]# chmod -R 700 /etc/skel/Maildir/ ← メールボックスパーミッション設定

【既存ユーザー対処】※既に共有ディレクトリ形式でメールを受信しているユーザーがいる場合のみ
既存ユーザーのホームディレクトリにMaildir形式のメールボックスを作成して、蓄積済のメールデータを当該メールボックスへ移行する⇒メールデータ移行を参照

(4)存在しないユーザー宛メールの破棄
メールサーバーは、存在しないユーザー宛メールを受信すると、宛先不明による不達メールを送信元へ返そうとするが、送信元を偽装した迷惑メールが存在しないユーザー宛にわざと送られてくる場合があり、この場合、送信元が偽装されているため、不達メール自体が不達となってしまい、メールがキューに滞留してしまう(mailqコマンドで確認可能)。最終的に不達メールの送信を一定期間繰り返して破棄するが、このような迷惑メールが大量に送られてくると、キューにメールが大量にたまり、たまった大量の不達メールの再送処理により、サーバー自体が高負荷状態に陥って機能しなくなる恐れがあるため、存在しないユーザー宛のメールは破棄するようにする。
なお、正当なユーザーが宛先を間違えてメールを送った場合に、不達メールが返ってこずに、宛先を間違えたことに気づかない懸念はあるが、現在のセキュリティ事情では、送達実績のない宛先へのメールは送達確認をするようにすることが一般的であることから、本懸念は無視するものとする。
[root@centos ~]# vi /etc/postfix/main.cf ← Postfix設定ファイル編集
以下を最終行へ追加(存在しないユーザー宛メールをunknown_userへ配送)
local_recipient_maps =
luser_relay = unknown_user@localhost

[root@centos ~]# echo unknown_user: /dev/null >> /etc/aliases ← unknown_user宛メールを破棄

[root@centos ~]# newaliases ← /etc/aliases変更反映

■Postfix起動

(1)Postfix起動
[root@centos ~]# systemctl restart postfix ← Postfix再起動

[root@centos ~]# systemctl enable postfix ← Postfix自動起動設定

(2)TCP25番、TCP587番ポート開放
【ルーター】
ルーター側の設定で、TCP25番、TCP587番ポートへのアクセスをサーバーに転送するようにする。
※ルーターの設定は各ルーターのマニュアルまたはメーカー別ルーターポート開放手順を参照

【ファイアウォール】ファイアウォール導入している場合のみ
サーバー側のファイアウォール設定で、TCP25番、TCP587番ポートへのアクセスを許可するようにする。
ファイアウォール設定はこちらを参照

Portチェックテスト【外部からのPort開放確認】で「ホスト名(FQDN) または グローバルIPアドレス」にサーバー名(例:centossrv.com)、「チェックポート番号」に25または587と入力、「ご注意・制約事項」を確認チェックして「Portチェック実行」ボタンを押下し、
ホスト:centossrv.com
ポート:25または587
にアクセスできました
と表示されることを確認。


■OP25B対策

OP25B(Outbound Port 25 Blocking)対策参照

■Dovecotインストール

[root@centos ~]# yum -y install dovecot ← Dovecotインストール

■Dovecot設定

[root@centos ~]# vi /etc/dovecot/conf.d/10-mail.conf ← 10-mail.conf編集
# Location for users' mailboxes. The default is empty, which means that Dovecot
# tries to find the mailboxes automatically. This won't work if the user
# doesn't yet have any mail, so you should explicitly tell Dovecot the full
# location.
#
# If you're using mbox, giving a path to the INBOX file (eg. /var/mail/%u)
# isn't enough. You'll also need to tell Dovecot where the other mailboxes are
# kept. This is called the "root mail directory", and it must be the first
# path given in the mail_location setting.
#
# There are a few special variables you can use, eg.:
#
#   %u - username
#   %n - user part in user@domain, same as %u if there's no domain
#   %d - domain part in user@domain, empty if there's no domain
#   %h - home directory
#
# See doc/wiki/Variables.txt for full list. Some examples:
#
#   mail_location = maildir:~/Maildir
#   mail_location = mbox:~/mail:INBOX=/var/mail/%u
#   mail_location = mbox:/var/mail/%d/%1n/%n:INDEX=/var/indexes/%d/%1n/%n
#
# 
#
#mail_location =
mail_location = maildir:~/Maildir ← 追加(メール格納形式をMaildir形式にする)

# ':' separated list of directories under which chrooting is allowed for mail
# processes (ie. /var/mail will allow chrooting to /var/mail/foo/bar too).
# This setting doesn't affect login_chroot, mail_chroot or auth chroot
# settings. If this setting is empty, "/./" in home dirs are ignored.
# WARNING: Never add directories here which local users can modify, that
# may lead to root exploit. Usually this should be done only if you don't
# allow shell access for users. 
#valid_chroot_dirs =
valid_chroot_dirs = /home ← 追加※OpenSSH+Chrootを導入している場合のみ

[root@centos ~]# vi /etc/dovecot/conf.d/10-auth.conf ← 10-auth.conf編集
# Disable LOGIN command and all other plaintext authentications unless
# SSL/TLS is used (LOGINDISABLED capability). Note that if the remote IP
# matches the local IP (ie. you're connecting from the same computer), the
# connection is considered secure and plaintext authentication is allowed.
#disable_plaintext_auth = yes
disable_plaintext_auth = no ← 追加(プレインテキスト認証を許可)
メールサーバー間通信内容暗号化導入推奨

[root@centos ~]# vi /etc/dovecot/conf.d/10-ssl.conf ← 10-ssl.conf編集
# SSL/TLS support: yes, no, required. 
# disable plain pop3 and imap, allowed are only pop3+TLS, pop3s, imap+TLS and imaps
# plain imap and pop3 are still allowed for local connections
ssl = no ← SSL接続無効
メールサーバー間通信内容暗号化導入推奨

■Dovecot起動

(1)Dovecot起動
[root@centos ~]# systemctl start dovecot ← Dovecot起動

[root@centos ~]# systemctl enable dovecot ← Dovecot自動起動設定

(2)TCP110番(POPの場合)またはTCP143番(IMAPの場合)ポート開放
【ルーター】
ルーター側の設定で、TCP110番(POPの場合)またはTCP143番(IMAPの場合)ポートへのアクセスをサーバーに転送するようにする。
※ルーターの設定は各ルーターのマニュアルまたはメーカー別ルーターポート開放手順を参照

【ファイアウォール】ファイアウォール導入している場合のみ
サーバー側のファイアウォール設定で、TCP110番(POPの場合)またはTCP143番(IMAPの場合)ポートへのアクセスを許可するようにする。
ファイアウォール設定はこちらを参照

Portチェックテスト【外部からのPort開放確認】で「ホスト名(FQDN) または グローバルIPアドレス」にサーバー名(例:centossrv.com)、「チェックポート番号」に110または143と入力、「ご注意・制約事項」を確認チェックして「Portチェック実行」ボタンを押下し、
ホスト:centossrv.com
ポート:110または143
にアクセスできました
と表示されることを確認。


■メールユーザー追加

例としてメールユーザーcentos(メールアドレス:centos@centossrv.com)を追加する。
[root@centos ~]# useradd centos ← ユーザー追加※SSHによるリモート接続もできるようにする場合
[root@centos ~]# useradd -s /sbin/nologin centos ← ユーザー追加※SSHによるリモート接続はできないようにする場合

[root@centos ~]# passwd centos ← パスワード設定
Changing password for user centos.
New UNIX password:  ← パスワード応答
Retype new UNIX password:  ← パスワード応答(確認)
passwd: all authentication tokens updated successfully.

以下はSMTP認証に個別のユーザー名、パスワードを使用する場合のみ
[root@centos ~]# echo "パスワード" | saslpasswd2 -p -u centos.centossrv.com -c centos ← SMTP認証用ユーザー/パスワード登録

[root@centos ~]# sasldblistusers2 ← SMTP認証用ユーザー/パスワード確認
centos@centos.centossrv.com: userPassword

[root@centos ~]# chgrp postfix /etc/sasldb2 ← /etc/sasldb2所有グループをpostfixに変更※最初の1回のみ

※SMTP認証用ユーザー/パスワードを削除する場合
[root@centos ~]# saslpasswd2 -d centos -u centos.centossrv.com ← SMTP認証用ユーザー/パスワード削除

■メールクライアント設定(受信メールサーバーをPOPにする場合)

自宅や会社等の複数拠点でメールを使用する場合で、メールボックスを同期しなくてもいい場合(例:会社で受信したメールは自宅で受信できなくてもいい場合)、受信メールサーバーにPOPを使用する。
ここでは、メールソフトとしてThunderbirdを使用する。

Thunderbirdを起動し、メニューの「ファイル」⇒「新規作成」⇒「既存のメールアカウント」でメールアカウント設定を行う



「あなたのお名前」⇒表示名(例:webmaster@centossrv.com)
「メールアドレス」⇒メールアドレス(例:webmaster@centossrv.com)
「パスワード」⇒パスワード
「続ける」




「受信サーバー」⇒「POP3」を選択
「受信サーバー」−「サーバーのホスト名」⇒メールサーバー名(例:mail.centossrv.com)
「受信サーバー」−「ポート番号」⇒「110」を選択
「受信サーバー」−「SSL」⇒「接続の保護なし」を選択
「受信サーバー」−「認証方式」⇒「通常のパスワード認証」を選択
「送信サーバー」−「サーバーのホスト名」⇒メールサーバー名(例:mail.centossrv.com)
「送信サーバー」−「ポート番号」⇒「587」を選択
「送信サーバー」−「SSL」⇒「接続の保護なし」を選択
「送信サーバー」−「認証方式」⇒「通常のパスワード認証」を選択
「再テスト」
「次のアカウント設定が、指定されたサーバーを調べることにより見つかりました。」と表示されることを確認して「完了」




「接続する上での危険性を理解しました」をチェックして「完了」



■メールクライアント設定(受信メールサーバーをIMAPにする場合)

自宅や会社等の複数拠点でメールを使用する場合で、メールボックスを同期したい場合(例:会社で受信したメールを自宅でも受信したい場合)、受信メールサーバーにIMAPを使用する。
ここでは、メールソフトとしてThunderbirdを使用する。

Thunderbirdを起動し、メニューの「ファイル」⇒「新規作成」⇒「既存のメールアカウント」でメールアカウント設定を行う



「あなたのお名前」⇒表示名(例:webmaster@centossrv.com)
「メールアドレス」⇒メールアドレス(例:webmaster@centossrv.com)
「パスワード」⇒パスワード
「続ける」




「受信サーバー」⇒「IMAP」を選択
「受信サーバー」−「サーバーのホスト名」⇒メールサーバー名(例:mail.centossrv.com)
「受信サーバー」−「ポート番号」⇒「143」を選択
「受信サーバー」−「SSL」⇒「接続の保護なし」を選択
「受信サーバー」−「認証方式」⇒「通常のパスワード認証」を選択
「送信サーバー」−「サーバーのホスト名」⇒メールサーバー名(例:mail.centossrv.com)
「送信サーバー」−「ポート番号」⇒「587」を選択
「送信サーバー」−「SSL」⇒「接続の保護なし」を選択
「送信サーバー」−「認証方式」⇒「通常のパスワード認証」を選択
「再テスト」
「次のアカウント設定が、指定されたサーバーを調べることにより見つかりました。」と表示されることを確認して「完了」




「接続する上での危険性を理解しました」をチェックして「完了」



■メールサーバー確認

□内部で同一ユーザー同士でメールの送受信
□内部で他ユーザー間でメールの送受信
□内部で外部(プロバイダのメールアドレス等)との送受信
□内部で携帯との送受信※
□外部(会社等)で同一ユーザー同士でメールの送受信
□外部(会社等)で他ユーザー間でメールの送受信
□外部(会社等)で外部(プロバイダのメールアドレス等)との送受信
□外部(会社等)で携帯との送受信※

※携帯はドメイン指定受信等でメールサーバーからのメールが拒否されないようにしておくこと


■メール不正中継拒否テスト

Open Relay Testで「Mail Server」に自宅サーバーのホスト名(例:centossrv.com)を入力して「Check for Open Relay」ボタンを押下する。
18種類のテストが行われ、すべてのTestについてRelay NOT Acceptedと表示されればOK。


■関連コンテンツ




▲このページのトップへ戻る

プライバシーポリシー
centossrv.com