メールサーバー構築手順(Postfix / Dovecot)初心者でもできるメモ

CentOS ver.6.4でメールサーバを構築します。
メールサーバーを構築する際、必要となるのが、”SMTPサーバー(送信メールサーバー)”と”POP/IMAPサーバー(受信メールサーバー)”です。

インストールソフトとして

  • 送信メールサーバー = Postfix
  • 受信メールサーバー = Dovecot

を使用して構築。
また、Postfixは不正中継(迷惑メールに利用)されないようにSMTP-Auth機能を持たせるように。※後日追記

SMTP-Auth機能=メール送信時にユーザー名とPassの認証を行う機能。これにより、迷惑メールなどの不正中継に利用されないようにする。

OP25B(Outbound Port 25 Blocking)対策として・・・・・別設定で。

送信メールソフト(Postfix)インストールを実施

手始めにPostfix(送信メールサーバ)のインストールを実施

$ yum -y install postfix

yumインストールすればよほどのことが無い限りエラーになることはない。
FreeBSDでportsインストールしたときはエラーでまくったが。。。

Postfixの設定

$ vi /etc/postfix/main.cf
# 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.bobo.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 = bobo.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		#追加(ローカルのメール送信元メールアドレス@以降にドメイン名付与)

# RECEIVING MAIL

# 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, $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に制限)

SMTP-Auth設定

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

$ service saslauthd start	#saslauthdを起動
saslauthd を起動中:                                        [  OK  ]
$ chkconfig saslauthd on	#saslauthdを自動起動する設定

SMTP-Authにてユーザー名、パスワードを別途使用する場合(今回は実施しない)

$ vi /etc/sasl2/smtpd.conf ← SMTP-Auth認証設定ファイル編集※CentOS6の場合
pwcheck_method: saslauthd
↓
pwcheck_method: auxprop ← 変更

Mailディレクトリ形式のメールボックス作成

Postfixのメール格納形式は共有ディレクトリ形式だがセキュリティ強化の面からメールディレクトリ形式へ移行する

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

【新規ユーザの場合】
新規ユーザ追加時に自動でホームディレクトリにMaildir形式のメールボックスが作成されるようにする

$  mkdir -p /etc/skel/Maildir/{new,cur,tmp}
$
$
$ ll /etc/skel/Maildir/		#新規ユーザ追加時に自動でメールディレクトリ作成
合計 12
drwxr-xr-x 2 root root 4096 12月 19 19:32 2013 cur
drwxr-xr-x 2 root root 4096 12月 19 19:32 2013 new
drwxr-xr-x 2 root root 4096 12月 19 19:32 2013 tmp
$
$
$
$ chmod -R 700 /etc/skel/Maildir/	#パーミッション変更
$
$
$ ll /etc/skel/Maildir/
合計 12
drwx------ 2 root root 4096 12月 19 19:32 2013 cur
drwx------ 2 root root 4096 12月 19 19:32 2013 new
drwx------ 2 root root 4096 12月 19 19:32 2013 tmp
$
$

Postfix起動

CentOSはデフォルトでPostfixが起動しているので再起動を実施

$ service postfix restart
postfix を停止中:                                          [  OK  ]
postfix を起動中:                                          [  OK  ]
$

$ chkconfig postfix on	#Postfix自動起動設定

$ chkconfig | grep postfix
postfix         0:off   1:off   2:on    3:on    4:on    5:on    6:off	#設定が左記のようになっていること

受信メールソフト(Dovecot)インストールを実施

$ yum -y install dovecot
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
 * base: mirrors.yun-idc.com
 * extras: mirrors.yun-idc.com
 * updates: mirrors.yun-idc.com

~~~~~~~~

Installed:
  dovecot.x86_64 1:2.0.9-7.el6

Dependency Updated:
  openssl.x86_64 0:1.0.1e-16.el6_5.4

Complete!		#インストール成功
$

Dovecot設定

バックアップを取っておく

$ ll /etc/dovecot/conf.d/10-mail.conf
-rw-r--r-- 1 root root 14449 11月 23 00:09 2013 /etc/dovecot/conf.d/10-mail.conf
$
$ cp -p /etc/dovecot/conf.d/10-mail.conf /etc/dovecot/conf.d/10-mail.conf.original
$
$
$ ll /etc/dovecot/conf.d/10-mail.conf*
-rw-r--r-- 1 root root 14449 11月 23 00:09 2013 /etc/dovecot/conf.d/10-mail.conf
-rw-r--r-- 1 root root 14449 11月 23 00:09 2013 /etc/dovecot/conf.d/10-mail.conf.original	#バックアップをとておく
$

対象ファイルを設定(10-mail.conf)

$ less /etc/dovecot/conf.d/10-mail.conf

##
## Mailbox locations and namespaces
##

# 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
#
# <doc/wiki/MailLocation.txt>
#
#mail_location =
mail_location = 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. <doc/wiki/Chrooting.txt>
#valid_chroot_dirs =
valid_chroot_dirs = /home		#OpenSSH + Chrootを導入時に設定

対象ファイルを設定(10-auth.conf)

バックアップを取っておく

$ ll /etc/dovecot/conf.d/10-auth.conf
-rw-r--r-- 1 root root 5157  6月 26 10:11 2010 /etc/dovecot/conf.d/10-auth.conf
$
$ cp -p /etc/dovecot/conf.d/10-auth.conf /etc/dovecot/conf.d/10-auth.conf.original
$
$ ll /etc/dovecot/conf.d/10-auth.conf*
-rw-r--r-- 1 root root 5157  6月 26 10:11 2010 /etc/dovecot/conf.d/10-auth.conf
-rw-r--r-- 1 root root 5157  6月 26 10:11 2010 /etc/dovecot/conf.d/10-auth.conf.original
$
$ vi /etc/dovecot/conf.d/10-auth.conf

##
## Authentication processes
##

# 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		#プレーンテキスト認証を許可する

Dovecot起動

$ service dovecot start
Dovecot Imap を起動中:                                     [  OK  ]
$

$ chkconfig | grep dovecot
dovecot         0:off   1:off   2:on    3:on    4:on    5:on    6:off
$

メールユーザ追加

メールユーザ追加設定の際、SSHリモート接続も許可できるように設定する。

メールユーザ追加設定手順

$ useradd bobo
$ passwd bobo
ユーザー bobo のパスワードを変更。
新しいパスワード:
新しいパスワードを再入力してください:
passwd: 全ての認証トークンが正しく更新できました。
$

メールサーバ動作確認

送信メール確認

$ telnet localhost 25  #SMTPでアクセス
Trying ::1...
Connected to localhost.
Escape character is '^]'.
220 mail.bobo.com ESMTP unknown
HELO bobo.com              #アクセス成功
502 5.5.2 Error: command not recognized
MAIL FROM: bobo@bobo.com         #送り元メールアドレス
250 2.1.0 Ok
RCPT TO: bobo@bobo.com          #RCPT TO検証
250 2.1.5 Ok
DATA                   #ここからメールの内容を記載
354 End data with .
From: bobo@bobo.com            #宛先メールアドレス
Subject: test               #件名
Hello wrold.               #ここからメールの内容↓
test
test
test
test

.                     #終了(ピリオド)
250 2.0.0 Ok: queued as 576F6245A
QUIT                   #メール終了(送信)
221 2.0.0 Bye
Connection closed by foreign host.
$

ログで正常にメールを送信できたことを確認。

$ less /var/log/maillog
Jan 10 17:51:19 t-bobo011 postfix/smtpd[24686]: connect from localhost[::1]
Jan 10 17:51:55 t-bobo011 postfix/smtpd[24686]: 576F6245A: client=localhost[::1]
Jan 10 17:52:50 t-bobo011 postfix/cleanup[24691]: 576F6245A: message-id=<20140110085155.576F6245A@mail.bobo.com>
Jan 10 17:52:50 t-bobo011 postfix/qmgr[12693]: 576F6245A: from=<bobo@bobo.com>, size=369, nrcpt=1 (queue active)
Jan 10 17:52:50 t-bobo011 postfix/local[24694]: 576F6245A: to=<bobo@bobo.com>, relay=local, delay=69, delays=69/0.01/0/0.01, dsn=2.0.0, status=sent (delivered to maildir)
Jan 10 17:52:50 t-bobo011 postfix/qmgr[12693]: 576F6245A: removed

受信メール確認

$ telnet localhost 110    #POP3でアクセス
Trying ::1...
Connected to localhost.
Escape character is '^]'.
+OK Dovecot ready.
USER bobo       #ユーザー入力
+OK
PASS cocobat5555    #ユーザーパスワード入力
+OK Logged in.
LIST          #メッセージリスト確認
+OK 5 messages:
1 422
2 455
3 429
4 322
5 458
.
RETR 5         #5番目のメッセージを確認
+OK 458 octets
Return-Path: <bobo@bobo.com>
X-Original-To: bobo@bobo.com
Delivered-To: bobo@bobo.com
Received: from localhost (localhost [IPv6:::1])
        by mail.bobo.com (Postfix) with SMTP id 576F6245A
        for <bobo@bobo.com>; Fri, 10 Jan 2014 17:51:41 +0900 (JST)
From: bobo@bobo.com
Subject: test
Message-Id: <20140110085155.576F6245A@mail.bobo.com>
Date: Fri, 10 Jan 2014 17:51:41 +0900 (JST)
To: undisclosed-recipients:;

Hello wrold.
test
test
test
test

.
QUIT         #終了
+OK Logging out.
Connection closed by foreign host.
$

ログで正常にメールを受信できたことを確認。

$ less /var/log/maillog
Jan 10 17:58:29 t-bobo011 dovecot: pop3-login: Login: user=, method=PLAIN, rip=::1, lip=::1, mpid=24714, secured
Jan 10 17:59:05 t-bobo011 dovecot: pop3(bobo): Disconnected: Logged out top=0/0, retr=1/474, del=0/5, size=2086

まとめ

難しそうに見えますが、実際に設定してみると、メールサーバーが一番簡単だったと思います。
ほんと初期動作までの設定なので、あとはメールソフトを使用するなどして動作確認をしてください。

それでは!




エンジニアのオンライン学習

ITエンジニアにおすすめの教材、オンラインスクールです。
無料からエンジニアの学習ができる教材などまとめているので参考にしてください。

おすすめオンライン教材
自宅で学習ができるオンラインスクール

ITエンジニアの開発・検証・学習としてインターネット上で専用のサーバ(VPS)を利用しましょう!
実務経験はVPSで学べます。



6件のコメント

送信メール確認部分で”HELLO bobo.com”
-> “HELO bobo.com”ではないでしょうか?

502 5.5.2 Error: command not recognized
となってしまっています

ありがとうございます!
メールサーバの勉強をしているので参考にさせて頂いております

参考にさせていただきました!

対象ファイルを設定(10-auth.conf)で
disable_planetext_auth = no #プレーンテキスト認証を許可する
の綴りが間違っています。
planetextではなく、plaintextで起動することができました。

ご指摘ありがとうございます!「plaintext」に変更しました!

また、参考にしていただきありがとうございます!

maromaro0013 へ返信する コメントをキャンセル

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)

ABOUT US
げんき☆ひろき
インターネット関連のSEをやっています。 ネットワーク、サーバー、ストレージ、仮想基盤まで幅広く手を出しており、MVNOの構築経験もあります。 現在は、Pythonを使ったプログラミングの開発をしネットワークの自動化ツールを作成しています! Pythonの入門書も作成しているので、ぜひ参考にしてください!