メールサーバ構築

環境
  idcfクラウド上のCentOS7に環境構築
  メール送信にはサーバのドメインが必要なので、Dynamic DO!(http://ddo.jp/)を利用してクラウド
  サーバにドメインを割り当てる。
 
 メールサーバの環境構築としてMTA(Message Transfer Agent)にpostfix, MDA(Mail Deliver Agent)に
 dovecotでそれぞれ設定の設定を行う。
 
 postfixについては、CentOS7に最初からインストールされていると思うが、以下のコマンドで確認してみる。

 rpm -q postfix

 
 postfixがインストールされていなければ以下のコマンドでインストールする。

 yum install postfix

 
 次に、自動起動の設定をしておく

 systemctl enable postfix

 
 dnsと同じでpostfixchrootで起動するようにする。
 以下のコマンドでpostfixchroot化する。

 /usr/share/doc/postfix-x.x.x/examples/chroot-setup/LINUX2

 
 それからmaster.cfにchrootの設定を行う。

 vi /etc/postfix/master.cf

# ==========================================================================
# service type private unpriv chroot wakeup maxproc command + args
# (yes) (yes) (yes) (never) (100)
# ==========================================================================
smtp inet n - y - - smtpd
#smtp inet n - n - 1 postscreen
#smtpd pass - - n - - smtpd
#dnsblog unix - - n - 0 dnsblog
#tlsproxy unix - - n - 0 tlsproxy
#submission inet n - n - - smtpd
# -o syslog_name=postfix/submission
# -o smtpd_tls_security_level=encrypt
# -o smtpd_sasl_auth_enable=yes
# -o smtpd_reject_unlisted_recipient=no
# -o smtpd_client_restrictions=$mua_client_restrictions
# -o smtpd_helo_restrictions=$mua_helo_restrictions
# -o smtpd_sender_restrictions=$mua_sender_restrictions
# -o smtpd_recipient_restrictions=permit_sasl_authenticated,reject
# -o milter_macro_daemon_name=ORIGINATING
#smtps inet n - n - - smtpd
# -o syslog_name=postfix/smtps
# -o smtpd_tls_wrappermode=yes
# -o smtpd_sasl_auth_enable=yes
# -o smtpd_reject_unlisted_recipient=no
# -o smtpd_client_restrictions=$mua_client_restrictions
# -o smtpd_helo_restrictions=$mua_helo_restrictions
# -o smtpd_sender_restrictions=$mua_sender_restrictions
# -o smtpd_recipient_restrictions=permit_sasl_authenticated,reject
# -o milter_macro_daemon_name=ORIGINATING
#628 inet n - n - - qmqpd
pickup unix n - y 60 1 pickup
cleanup unix n - y - 0 cleanup
qmgr unix n - n 300 1 qmgr
#qmgr unix n - n 300 1 oqmgr
tlsmgr unix - - n 1000? 1 tlsmgr
rewrite unix - - y - - trivial-rewrite
bounce unix - - y - 0 bounce
defer unix - - y - 0 bounce
trace unix - - n - 0 bounce
verify unix - - n - 1 verify
flush unix n - y 1000? 0 flush
proxymap unix - - y - - proxymap
proxywrite unix - - n - 1 proxymap
smtp unix - - y - - smtp
relay unix - - y - - smtp
# -o smtp_helo_timeout=5 -o smtp_connect_timeout=5
showq unix n - y - - showq
error unix - - y - - error
retry unix - - n - - error
discard unix - - n - - discard
local unix - n n - - local
virtual unix - n n - - virtual
lmtp unix - - y - - lmtp
anvil unix - - n - 1 anvil
scache unix - - n - 1 scache
#
# ====================================================================

上記に従い、chrootにyをつけたあとpostfixの設定再読み込み、または再起動を
するとchrootでの起動になっているはずである。

systemctl restart postfix

chrootで起動しているかはpsを確認したときに、プロセスに-cのオプションがついていること
で確認できる。

ps aux | grep postfix


次にpostfixの設定ファイル/etc/postfix/main.cfを編集する。

vi /etc/postfix/main.cf

#ホスト名
myhostname = ooo.xxx.ddo.jp

#ドメイン
mydomain= xxx.ddo.jp

#メールアドレスのドメイン
myorijin = $mydomain

#SMTP接続を待ち受けるネットワークインターフェース
inet_interfaces = all

#IPv4/IPv6の動作をall/ipv4/ipv6で指定
inet_protocols = all

#ローカル階層を行うドメイン
mydestination = $myhostname, localhost.$mydomain, localhost

#中継を許可するSMTPクライアントのアドレス
mynetowrks = 192.168.xx.00/24, 127.0.0.0/8

#メールすプールディレクトリ
mail_spool_directory = /var/spool/mail

#メールボックスへのパス
home_mailbox = Maildir/

#一人当たりのメールボックス要領
mailbox_size_limit = 104857600
#mailbox_size_limit = 0

次にpostfixを再起動する。

systemctl restart postfix

それから各ユーザーのメールボックスとして/home/*/Maildirが指定されているので
まず、ユーザディレクトリの雛形にMaildirを追加する

mkdir -p /etc/skel/Maildir/{new,cur,tmp}
chmod -R 700 /etc/skel/Maildir/

すでにユーザーが作成されている場合は以下のコマンドでメールボックス用のフォルダを作成できる

find /home/ -maxdepth 1 -mindepth 1 -type d | gawk '{print $NF}' | xargs -i mkdir -p {}/Maildir/{new,cur,tmp}
find /home/ -maxdepth 1 -mindepth 1 -type d | gawk '{print $NF}' | xargs -i chmod -R 700 {}/Maildir

次にsendmailコマンドでローカルのユーザーにメールが送信されているかを確認する

sendmail □□□@xxx.ddo.jp
From: □□□@xxx.ddo.jp
To: △△△@xxx.ddo.jp
Subject: test
this is test mail.
.

これでメールが送信されるはずだが実際に受け取っているかはmailコマンドで確かめてみる
mailコマンド実行できなかったらyumでインストールする

yum install mailx

mailコマンド使えるようになったら以下のコマンドでメール送信、受信できたか確認してみる

mail -f /home/△△△/Maildir

そしたらメールが受信されていることを確認されているはずである。
メールが受信されていなかったらメールログなど確認してみる。

tail -f /var/log/maillog