CentOS7

Postfix+Dovecotインストール

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

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

また、DovecotはPOP/IMAPサーバーとして構築し、ユーザーがPOPまたはIMAPを選択できるようにする。

Postfixインストール

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

Postfix設定

[root@localhost ~]# 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.hoge.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 = hoge.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@localhost ~]# 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認証有効化)

SMTP認証設定

SMTP認証にシステムのユーザー名、パスワードを使用する場合

[root@localhost ~]# yum -y install cyrus-sasl ← cyrus-saslインストール
[root@localhost ~]# systemctl start saslauthd ← saslauthd起動
[root@localhost ~]# systemctl enable saslauthd ← saslauthd自動起動設定

※SMTP認証に個別のユーザー名、パスワードを使用する場合

[root@localhost ~]# vi /etc/sasl2/smtpd.conf
pwcheck_method: auxprop ← 変更

Maildir形式メールボックス作成

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

【新規ユーザー対処】

[root@localhost ~]# mkdir -p /etc/skel/Maildir/{new,cur,tmp}
[root@localhost ~]# chmod -R 700 /etc/skel/Maildir/

【既存ユーザー対処】

※既に共有ディレクトリ形式でメールを受信しているユーザーがいる場合のみ

既存ユーザーのホームディレクトリにMaildir形式のメールボックスを作成して、蓄積済のメールデータを当該メールボックスへ移行する。

※メールデータ移行中にメールを受け付けないようにするため、メールサーバーは停止しておくこと

Maildir変換ツール入手

[root@localhost ~]# wget http://perfectmaildir.home-dn.net/perfect_maildir/perfect_maildir.pl -O /usr/local/bin/perfect_maildir.pl ← Maildir変換ツールダウンロード
[root@localhost ~]# chmod +x /usr/local/bin/perfect_maildir.pl ← Maildir変換ツールへ実行権限付加

Maildir変換に必要なPerlのTimeDateモジュールインストール

[root@localhost ~]# yum -y install perl-TimeDate ← perl-TimeDateモジュールインストール

Maildir一括変換スクリプト作成

[root@localhost ~]# vi migrate-maildir ← Maildir一括変換スクリプト作成
#!/bin/bash

#http://perfectmaildir.home-dn.net/
FOLDERCONVERT=/usr/local/bin/perfect_maildir.pl

USERLIST=`ls /home/`

MIGRATELOG=/tmp/migrate-maildir.log
rm -f $MIGRATELOG

if [ "$1" != "mbox" ] && [ "$1" != "Mailbox" ]
then
    echo "Usage: migrate-maildir {mbox|Mailbox}"
    exit
fi

for user in $USERLIST;
do
    if [ "$1" = "mbox" ]; then
        inbox="/var/spool/mail/${user}"
    else
        inbox="/home/${user}/Mailbox"
    fi
        if [ -f "${inbox}" ]
    then
        newdir="/home/${user}/Maildir/"
        mkdir -p "$newdir"
        mkdir -p "$newdir"/cur
        mkdir -p "$newdir"/new
        mkdir -p "$newdir"/tmp
        chmod -R 700 "${newdir}"
        $FOLDERCONVERT "$newdir" < "${inbox}" >> $MIGRATELOG 2>&1
        chown -R "${user}":"${user}" "$newdir"
        find "$newdir" -type f  -exec chmod 600 {} \;
    fi
done

user="root"
if [ "$1" = "mbox" ]; then
    inbox="/var/spool/mail/${user}"
else
    inbox="/${user}/Mailbox"
fi
if [ -f "${inbox}" ]
then
    newdir="/${user}/Maildir/"
    mkdir -p "$newdir"
    mkdir -p "$newdir"/cur
    mkdir -p "$newdir"/new
    mkdir -p "$newdir"/tmp
    chmod -R 700 "${newdir}"
    $FOLDERCONVERT "$newdir" < "${inbox}" >> $MIGRATELOG 2>&1
    chown -R "${user}":"${user}" "$newdir"
    find "$newdir" -type f  -exec chmod 600 {} \;
fi

メールデータ移行(共有ディレクトリ形式⇒Maildir形式)

現在のメールボックス形式がデフォルトの「/var/spool/mail/ユーザー名」の場合

[root@localhost ~]# sh migrate-maildir mbox ← Maildir一括変換スクリプト実行

Maildir一括変換スクリプト実行結果は/tmp/migrate-maildir.logに保存される

メールデータ移行後始末

[root@localhost ~]# rm -f migrate-maildir ← Maildir一括変換スクリプト削除
[root@localhost ~]# rm -f /usr/local/bin/perfect_maildir.pl ← Maildir変換ツール削除

※メールデータ移行後は、メールサーバーのメール格納形式をMaildir形式へ移行すること

存在しないユーザー宛メールの破棄

メールサーバーは、存在しないユーザー宛メールを受信すると、宛先不明による不達メールを送信元へ返そうとするが、送信元を偽装した迷惑メールが存在しないユーザー宛にわざと送られてくる場合があり、この場合、送信元が偽装されているため、不達メール自体が不達となってしまい、メールがキューに滞留してしまう(mailqコマンドで確認可能)。最終的に不達メールの送信を一定期間繰り返して破棄するが、このような迷惑メールが大量に送られてくると、キューにメールが大量にたまり、たまった大量の不達メールの再送処理により、サーバー自体が高負荷状態に陥って機能しなくなる恐れがあるため、存在しないユーザー宛のメールは破棄するようにする。

なお、正当なユーザーが宛先を間違えてメールを送った場合に、不達メールが返ってこずに、宛先を間違えたことに気づかない懸念はあるが、現在のセキュリティ事情では、送達実績のない宛先へのメールは送達確認をするようにすることが一般的であることから、本懸念は無視するものとする。

[root@localhost ~]# vi /etc/postfix/main.cf ← Postfix設定ファイル編集
以下を最終行へ追加(存在しないユーザー宛メールをunknown_userへ配送)
local_recipient_maps =
luser_relay = unknown_user@localhost
[root@localhost ~]# echo unknown_user: /dev/null >> /etc/aliases ← unknown_user宛メールを破棄
[root@localhost ~]# newaliases ← /etc/aliases変更反映

Postfix起動

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

TCP25番、TCP587番ポート開放

【ルーター】

ルーター側の設定で、TCP25番、TCP587番ポートへのアクセスをサーバーに転送するようにする。

※ルーターの設定は各ルーターのマニュアルまたはメーカー別ルーターポート開放手順を参照

【ファイアウォール】※ファイアウォール導入している場合のみ

サーバー側のファイアウォール設定で、TCP25番、TCP587番ポートへのアクセスを許可するようにする。

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

にアクセスできましたと表示されることを確認。

Dovecotインストール

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

Dovecot設定

[root@localhost ~]# 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@localhost ~]# 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@localhost ~]# 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起動

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

TCP110番(POPの場合)またはTCP143番(IMAPの場合)ポート開放

【ルーター】

ルーター側の設定で、TCP110番(POPの場合)またはTCP143番(IMAPの場合)ポートへのアクセスをサーバーに転送するようにする。

【ファイアウォール】※ファイアウォール導入している場合のみ

サーバー側のファイアウォール設定で、TCP110番(POPの場合)またはTCP143番(IMAPの場合)ポートへのアクセスを許可するようにする。

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

にアクセスできましたと表示されることを確認。

メールユーザー追加

例としてメールユーザー centos (メールアドレス:centos@hoge.com)を追加する。

[root@localhost ~]# useradd centos ← ユーザー追加※SSHによるリモート接続もできるようにする場合
[root@localhost ~]# useradd -s /sbin/nologin centos ← ユーザー追加※SSHによるリモート接続はできないようにする場合
[root@localhost ~]# passwd centos ← パスワード設定
Changing password for user centos.
New UNIX password: ← パスワード応答
Retype new UNIX password: ← パスワード応答(確認)
passwd: all authentication tokens updated successfully.

以下はSMTP認証に個別のユーザー名、パスワードを使用する場合のみ

[root@localhost ~]# echo "パスワード" | saslpasswd2 -p -u centos.hoge.com -c centos ← SMTP認証用ユーザー/パスワード登録
[root@localhost ~]# sasldblistusers2 ← SMTP認証用ユーザー/パスワード確認
centos@hoge.com: userPassword
[root@localhost ~]# chgrp postfix /etc/sasldb2 ← /etc/sasldb2所有グループをpostfixに変更※最初の1回のみ
※SMTP認証用ユーザー/パスワードを削除する場合
[root@localhost ~]# saslpasswd2 -d centos -u centos.hoge.com ← SMTP認証用ユーザー/パスワード削除

トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2018-09-14 (金) 09:47:02