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

最終更新日: 2012.01.05

<<トップページ <<新着情報 <<質問掲示板 <<アンケート <<リンク集 <<サイト内検索 <<管理人へメール <<Scientific Linuxで自宅サーバー構築 <<Fedoraで自宅サーバー構築

■概要

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

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

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


■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-Auth設定)
smtpd_sasl_auth_enable = yes
smtpd_sasl_local_domain = $myhostname
smtpd_recipient_restrictions =
    permit_mynetworks
    permit_sasl_authenticated
    reject_unauth_destination

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

(2)SMTP-Auth設定
SMTP-Auth用ユーザ名、パスワードにシステムのユーザ名、パスワードを使用する場合)
[root@centos ~]# /etc/rc.d/init.d/saslauthd start ← saslauthd起動
saslauthd を起動中:                                        [  OK  ]

[root@centos ~]# chkconfig saslauthd on ← saslauthd自動起動設定

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

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

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

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

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

■Postfix起動

(1)sendmail停止
デフォルトで起動しているSMTPサーバーであるsendmailを停止する
[root@centos ~]# /etc/rc.d/init.d/sendmail stop ← sendmail停止
sendmailを停止中:                                          [  OK  ]
sm-clientを停止中:                                         [  OK  ]

[root@centos ~]# chkconfig sendmail off ← sendmail自動起動設定解除

(2)メールサーバー切替え
システムで使用するメールサーバー機能をsendmailからPostfixに切替える
[root@centos ~]# alternatives --config mta ← メールサーバー切替え

2 プログラムがあり 'mta'を提供します。

  選択       コマンド
-----------------------------------------------
*+ 1           /usr/sbin/sendmail.sendmail
   2           /usr/sbin/sendmail.postfix

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

(3)Postfix起動
[root@centos ~]# /etc/rc.d/init.d/postfix start ← Postfix起動
Starting postfix:                                          [  OK  ]

[root@centos ~]# chkconfig postfix on ← Postfix自動起動設定

(4)ポート25番のOPEN
ルーター側の設定でポート25番をOPENする。
※ルーターの設定は各ルーターのマニュアルまたはメーカー別ルーターポート開放手順を参照

ポートチェック【外部からポート開放確認】で「host名」にサーバー名(例:centossrv.com)、「port番号」に25と入力して「ポートチェック」ボタン押下し、「ホスト=centossrv.com ポート=25 にアクセスできました。」と表示されることを確認。

■OP25B対策

OP25B(Outbound Port 25 Blocking)対策参照

■Dovecotインストール

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

■Dovecot設定

【CentOS6の場合】
[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を導入している場合のみ



【CentOS5,CentOS4の場合】
[root@centos ~]# vi /etc/dovecot.conf ← Dovecot設定ファイル編集
# Protocols we want to be serving: imap imaps pop3 pop3s
# If you only want to use dovecot-auth, you can set this to "none".
#protocols = imap imaps pop3 pop3s
protocols = imap imaps ← 追加

# Location for users' mailboxes. This is the same as the old default_mail_env
# setting. The default is empty, which means that Dovecot tries to find the
# mailboxes automatically. This won't work if the user doesn't have any mail
# yet, 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
# and where Dovecot can place its index files. 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/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
#
# http://wiki.dovecot.org/MailLocation
#
#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 or auth_chroot variables.
# 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. See doc/configuration.txt for more information.
#valid_chroot_dirs =
valid_chroot_dirs = /home ← 追加※OpenSSHでChrootを設定している場合のみ

■Dovecot起動

(1)Dovecot起動
[root@centos ~]# /etc/rc.d/init.d/dovecot start ← Dovecot起動
Dovecot Imapを起動中:                                      [  OK  ]

[root@centos ~]# chkconfig dovecot on ← Dovecot自動起動設定

(2)ポート110番(POPの場合)または143番(IMAPの場合)のOPEN
ルーター側の設定でポート110番(POPの場合)または143番(IMAPの場合)をOPENする。
※ルーターの設定は各ルーターのマニュアルまたはメーカー別ルーターポート開放手順を参照

ポートチェック【外部からポート開放確認】で「host名」にサーバー名(例:centossrv.com)、「port番号」に110(POPの場合)または143(IMAPの場合)と入力して「ポートチェック」ボタン押下し、「ホスト=centossrv.com ポート=110(POPの場合)または143(IMAPの場合) にアクセスできました。」と表示されることを確認。


■メールユーザ追加

(1)メールユーザ追加(SSHによるリモート接続はできないようにする場合)
※例としてユーザ名をcentosとする
[root@centos ~]# useradd -s /sbin/nologin centos ← ユーザ追加

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

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

[root@centos ~]# sasldblistusers2 ← SMTP-Auth用ユーザ名、パスワード確認
centos@linux.centossrv.com: userPassword

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

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

(2)メールユーザ追加(SSHによるリモート接続もできるようにする場合)
※例としてユーザ名をcentosとする
[root@centos ~]# useradd centos ← ユーザ追加

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

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

[root@centos ~]# sasldblistusers2 ← SMTP-Auth用ユーザ名、パスワード確認
centos@linux.centossrv.com: userPassword

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

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

(3)既存ユーザをメールユーザとする場合
※例としてユーザ名をcentosとする
以下はSMTP-Auth用ユーザ名、パスワードとシステムのユーザ名、パスワードを別々にする場合のみ
[root@centos ~]# echo "パスワード" | saslpasswd2 -p -u linux.centossrv.com -c centos ← SMTP-Auth用ユーザ/パスワード登録

[root@centos ~]# sasldblistusers2 ← SMTP-Auth用ユーザ名、パスワード確認
centos@linux.centossrv.com: userPassword

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

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

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

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

Outlook Expressを起動し、メニューの「ツール」⇒「アカウント」⇒「メール」タブ⇒「追加」ボタン⇒「メール」としてメール設定を行う



「表示名」に送信者の名前(任意)を入力して「次へ」




「電子メールアドレス」に送信者のメールアドレス(例:centos@centossrv.com)を入力して「次へ」




「受信メールサーバー」にメールサーバー名(例:mail.centossrv.com)を入力
「送信メールサーバー」にメールサーバー名(例:mail.centossrv.com)を入力して「次へ」




「パスワード」にパスワードを入力して「次へ」




「完了」




追加したアカウントをダブルクリックしてプロパティを開く




「サーバー」タブの「このサーバーは認証が必要」をチェック
SMTP-Auth用ユーザ名、パスワードとシステムのユーザ名、パスワードを別々にする場合のみ「設定」ボタンをクリックして次の設定を行う




※SMTP-Auth用ユーザ名、パスワードとシステムのユーザ名、パスワードを別々にする場合のみ
「次のアカウントとパスワードでログオンする」をチェック
「アカウント名」にSMTP-Auth用ユーザ名を入力
「パスワード」にSMTP-Auth用パスワードを入力して「OK」




「OK」



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

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

Outlook Expressを起動し、メニューの「ツール」⇒「アカウント」⇒「メール」タブ⇒「追加」ボタン⇒「メール」としてメール設定を行う



「表示名」に送信者の名前(任意)を入力して「次へ」




「電子メールアドレス」に送信者のメールアドレス(例:centos@centossrv.com)を入力して「次へ」




「受信メールサーバーの種類」で「IMAP」を選択
「受信メールサーバー」にメールサーバー名(例:mail.centossrv.com)を入力
「送信メールサーバー」にメールサーバー名(例:mail.centossrv.com)を入力して「次へ」




「パスワード」にパスワードを入力して「次へ」




「完了」




追加したアカウントをダブルクリックしてプロパティを開く




「サーバー」タブの「このサーバーは認証が必要」をチェック
SMTP-Auth用ユーザ名、パスワードとシステムのユーザ名、パスワードを別々にする場合のみ「設定」ボタンをクリックして次の設定を行う




※SMTP-Auth用ユーザ名、パスワードとシステムのユーザ名、パスワードを別々にする場合のみ
「次のアカウントとパスワードでログオンする」をチェック
「アカウント名」にSMTP-Auth用ユーザ名を入力
「パスワード」にSMTP-Auth用パスワードを入力して「OK」




「IMAP」タブの「送信済みアイテムのパス」に「Sent」と入力
「IMAP」タブの「下書きのパス」に「Drafts」と入力して「OK」




「はい」




「OK」



■メールサーバー確認

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

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

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

RBL.JPで「ホスト名」に自宅サーバーのホスト名(例:centossrv.com)を入力して「Check」ボタンを押下する。
19種類のテストが行われ、ページ最後尾にno relays accepted.と表示されればOK。

Tweet Check



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

LPIロゴ Copyright© 2011 fallenangels, All rights reserved.
ご自由にリンクしてください(連絡は不要です)
本ページへのご意見・ご要望、誤字・脱字・リンク切れ等のご連絡はこちらからお願いします