LoginSignup
15
25

More than 5 years have passed since last update.

メールサーバー構築

Last updated at Posted at 2017-02-11

環境

  • OS: CentOS 6.7

DNS設定

ドメイン業者でのDNS設定

  • Aレコード設定(mail.{my domain})
  • MXレコード設定追加

VPSサーバー業者でのDNS逆引き設定

  • PTRレコード設定

MXレコードの確認

以下の手順でMXレコードが表示されれば正しく設定されている。

$ nslookup

> server <DNSサーバーのIPアドレス>
> set q=MX
> {my domain}
{結果}
> exit

SMTPサーバー(Postfix)

インストール

# Postfixがインストールされているか確認
$ yum list installed | grep postfix

# Postfixをインストール
$ sudo yum install postfix

# Postfixがインストールされたことを確認
$ yum list installed | grep postfix
postfix.x86_64                    2:2.6.6-6.el6_7.1                    @base

ユーザーとグループの確認

Postfixをインストールすると、以下のユーザーとグループが作成される。

  • ユーザー:postfix
  • グループ:postfix, postdrop
$ cat /etc/passwd | grep postfix
postfix:x:89:89::/var/spool/postfix:/sbin/nologin

$ cat /etc/group | grep post
mail:x:12:mail,postfix
postdrop:x:90:
postfix:x:89:

基本設定

/etc/postfix/main.cf
#メールサーバーのホスト名
myhostname = mail.{my domain}
#ドメイン名
mydomain = {my domain}
#Fromアドレスに付加されるドメイン
myorigin = $mydomain
#メールの配送を許可する宛先ドメイン名
mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
#メールの配送を許可するクライアントの指定(ローカルマシンのみ)
mynetworks_style = host
#メールを受け取るネットワークI/Fの指定
inet_interfaces = all

以下は、いずれかひとつだけを指定する。
- mynetworks_style
- mynetworks

配送制限

/etc/postfix/main.cf
# postmasterへの通知設定
notify_classes = resource, software, policy
# %が含まれるアドレスを@に変換する
allow_percent_hack = yes
# !が含まれるアドレスを@に変換する
swap_bangpath = yes
# VRFYコマンドの使用禁止
disable_vrfy_command = yes
# 受信者制限
smtpd_recipient_restrictions = permit_mynetworks, check_relay_domains

起動

# Postfix設定の確認
$ sudo postfix check
$ sudo /etc/rc.d/init.d/postfix restart

$ ps aux | grep postfix
root      1492  0.2  0.3  81288  3424 ?        Ss   00:01   0:00 /usr/libexec/postfix/master
postfix   1494  0.0  0.3  81368  3376 ?        S    00:01   0:00 pickup -l -t fifo -u
postfix   1495  0.0  0.3  81436  3416 ?        S    00:01   0:00 qmgr -l -t fifo -u

$ sudo lsof -i:25
COMMAND  PID USER   FD   TYPE     DEVICE SIZE/OFF NODE NAME
master  1492 root   12u  IPv4 1200760344      0t0  TCP localhost.localdomain:smtp (LISTEN)
master  1492 root   13u  IPv6 1200760346      0t0  TCP localhost.localdomain:smtp (LISTEN)
  • masterプロセスが25ポートで待ち受け、各種プロセスを起動する。
  • pickupとqmgrは常駐する。

SMTP AUTHによるユーザー認証

SASLの設定

SASLの設定については、下記リンクを参照。
http://qiita.com/nkiw/items/cbddeaba18f8c010fb5e

SMTP AUTHを使用する設定

/etc/postfix/main.cf
# 受信者制限
smtpd_recipient_restrictions = permit_auth_destination, permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
# SMTP AUTHを有効化
smtpd_sasl_auth_enable = yes
# ドメイン名設定
smtpd_sasl_local_domain = $myhostname
# 匿名認証を許可しない
smtpd_sasl_security_options = noanonymous
# SASL認証に対応していないクライアントを拒否
broken_sasl_auth_clients = yes
  • smtpd_recipient_restriction
    • permit_sasl_authenticated・・・SASL認証済みアクセスを許可
    • permit_auth_destination・・・このサーバーで配送終了となるドメイン宛メールを許可
    • reject_unauth_destination・・・このサーバーで配送終了とならないドメイン宛メールを拒否

SSL/TLSアクセス設定

サーバー証明書と秘密鍵の準備

認証局(CA)の構築とサーバー証明書の作成については、以下のリンクを参照。
http://qiita.com/nkiw/items/94191fcfdc6a56d56b5d

smtpsポート待ち受け設定

/etc/postfix/master.cf
# smtps(465)ポートのListen
smtps     inet  n       -       n       -       -       smtpd
  -o smtpd_tls_wrappermode=yes
  -o smtpd_sasl_auth_enable=yes
# TLSセッションキャッシュの管理プログラム起動
tlsmgr    unix  -       -       n       1000?   1       tlsmgr

サーバー証明書と鍵の設定

  • /etc/postfix/cert/にサーバー証明書と鍵を置く。
  • main.cfでパスを設定する。
/etc/postfix/main.cf
# SSL/TLSの設定
smtpd_tls_cert_file = /etc/postfix/cert/cert.crt
smtpd_tls_key_file = /etc/postfix/cert/cert.key
smtpd_use_tls = yes

サブミッションポート(587)待ち受け設定

メールクライアントから平文でSMTPアクセスするために必要。

/etc/postfix/master.cf
submission inet n       -       n       -       -       smtpd
  -o smtpd_sasl_auth_enable=yes

トラブルシューティング

ログ

/var/log/maillog

POP/IMAPサーバー(Dovecot)

インストール

$ sudo yum install dovecot

$ yum list installed | grep dovecot
dovecot.x86_64                    1:2.0.9-22.el6                       @base

Dovecotの設定ファイル

メールボックス設定

  • メールボックスの形式(mox)と場所を設定する。
/etc/dovecot/conf.d/10-mail.conf
mail_location = mbox:/home/%u/mail:INBOX=/var/mail/%u

ユーザー認証設定

  • Dovecot用に作成するパスワードファイルで認証を行うように設定する。
/etc/dovecot/conf.d/10-auth.conf
disable_plaintext_auth = no
#!include auth-system.conf.ext
!include auth-passwdfile.conf.ext
  • パスワードファイルのパスを指定する。
/etc/dovecot/conf.d/auth-passwdfile.conf.ext
passdb {
  driver = passwd-file
  args = /etc/dovecot/passwd
}

userdb {
  driver = passwd-file
  args = /etc/dovecot/passwd
}

SSL設定

  • 指定したパスにSSL証明書と秘密鍵を設置する。
/etc/dovecot/conf.d/10-ssl.conf
ssl = yes
ssl_cert = </etc/dovecot/cert/cert.crt
ssl_key = </etc/dovecot/cert/cert.key

ログ出力設定

  • デバッグできるようにとりあえず全てログを出す。
/etc/dovecot/conf.d/10-logging.conf
log_path = /var/log/dovecot.log
auth_verbose = yes
auth_debug = yes
auth_debug_passwords = yes
mail_debug = yes

設定内容の表示

$ dovecot -a

/etc/dovecot/passwd作成

  • gidはmail(12)にする。
<user name>:{PLAIN}<password>:<user's uid>:<gid of "mail" group>

/home/{user}/mailディレクトリ作成

  • 10-mail.confで指定したディレクトリを作成する。
  • ディレクトリのグループとパーミッションは以下のように設定しないとエラーになった。
    • パーミッションは760
    • groupはmail(12)
$ ls -l /home/{user}/
drwxrw---- 3 {user} mail   4096  3月  3 00:37 2017 mail/

起動

# 起動
$ sudo /etc/init.d/dovecot restart
# 停止
$ sudo /etc/init.d/dovecot stop
# 再起動
$ sudo /etc/init.d/dovecot restart

トラブルシューティング

ログ

/var/log/dovecot.log

参考文献

15
25
0

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
15
25