自宅サーバー ≫ Linux Server ≫ CentOS 7 ≫ Mail (SMTP・POP3)サーバー(postfix + dovecot)

 
Mailサーバー

SMTPサーバー に postfix・ POP3サーバーに dovecot を使いMailサーバーを構築

Postfix のインストール
バージョン:postfix-2.10.1-6
 
 [root@
centos]# yum -y install postfix
 
Postfix の設定
 
 [root@
centos]# 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 =
papa-net-server
 #myhostname = virtual.domain.tld

 # 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 =
papa-net.info

 # 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

 
途中略
 
 mydestination = $myhostname, localhost.$mydomain, localhost
   <--- 追記
  
↓                                               (自分のドメイン宛メールを受信できるようにする)
 mydestination = $myhostname, localhost.$mydomain, localhost
, $mydomain
 
 
途中略
 
 # TRUST AND RELAY CONTROL

 
途中略
 
 
#relay_domains = $mydestination   <--- コメント解除(中継許可)****************************
  
 ↓
 relay_domains = $mydestination

 # INTERNET OR INTRANET

 
途中略途中略
 
 # 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/   <--- コメント解除(メールボックス形式をMaildir形式にする)
  

 home_mailbox = Maildir/

 
途中略
 
 
以下最終行へ追記
 
smtpd_sasl_auth_enable = yes
 
smtpd_sasl_local_domain = $mydomain, $myhostname
 smtpd_recipient_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination

 
OP25B対策(SMTP-Authの設定 )
SMTP認証の為のSASLSMTP認証にはシステムアカウントを利用する方法と、sasldb2を利用する方法がある

sasldb2を利用すれば、システムアカウントとは異なるパスワードを設定できるので、こちらの方がセキュリティは高くな

必要なパッケージのインストール

 
 [root@
centos]# yum -y install cyrus-sasl
 [root@
centos]# yum -y install cyrus-sasl-plain
 [root@
centos]# yum -y install cyrus-sasl-md5
 
Postfix の設定
 
 [root@
centos]# vi /etc/postfix/main.cf
 
 
途中略
 
 
#relayhost = $mydomain
 #relayhost = [gateway.my.domain]
 #relayhost = [mailserver.isp.tld]
 #relayhost = uucphost
 #relayhost = [an.ip.add.ress]
 
relayhost = [mail.***.***.ne.jp]:587   <--- 追記(OP25B対策 /********************************
                                            
     プロバイダーのSMTPサーバーのアドレス)
 
途中略

 
以下最終行へ追記
 
 
smtp_use_tls = yes
 smtp_sasl_mechanism_filter = plain

 
smtp_sasl_password_maps = hash:/etc/postfix/authinfo
 smtp_sasl_security_options = noanonymous
 smtp_sasl_mechanism_filter = LOGIN

 
SMTP認証ファイルの作成
 
 [root@
centos]# vi /etc/postfix/authinfo

 
以下新規記述
 
 
[**********]:587 +++++@++++++++++++:xxxxxxxx
 
 
**** = プロバイダーのSMTPサーバーアドレス
  ++++ =
プロバイダーのメールアドレス
  xxxx =
プロバイダーのログインパスワード
 
アクセス権の変更
 
 [root@
centos]# chmod 640 /etc/postfix/authinfo
 
authinfo.db の作成
 
 [root@
centos]# postmap /etc/postfix/authinfo
 
saslauthd の起動
 
 [root@
centos]# systemctl start saslauthd
 
saslauthd の自動 起動設定
 
 [root@
centos]# systemctl enable saslauthd
 
自動起動設定の確認
 
 [root@
centos]# systemctl list-unit-files | grep saslauthd
 
saslauthd.service                                  enabled  ← enabled になっていればOK
 
Postfix の起動
 
 [root@
centos]# systemctl start postfix
 
Postfix を自動起動設定
 
 [root@
centos]# systemctl enable postfix
 
自動起動設定の確認
 
 [root@
centos]# systemctl list-unit-files | grep postfix
 
postfix.service                                    enabled   ← enabled になっていればOK
 
SMTPサーバーの変更
sendmail がインストールされている場合に実行
sendmail の停止
 
 [root@
centos]# systemctl stop sendmail
 
sendmail を自動起動解除設定
 
 [root@
centos]# systemctl disable sendmail
 
自動起動解除設定の確認
 
 [root@
centos]# systemctl list-unit-files | grep sendmail
 
sendmail.service                                  disabled   <--- disabled になっていればOK
 
デフォルトのSMTPサーバーの変更
 
 [root@
centos]# alternatives --config mta
 2 プログラムがあり 'mta' を提供します。
 選択               コマンド
 -----------------------------------------------
 *+ 1               /usr/sbin/sendmail.sendmail
    2               /usr/sbin/sendmail.postfix
 Enter を押して現在の選択 [+] を保持するか、選択番号を入力します:
2   <--- 2 を入力
 
変更の確認
 
 [root@
centos]# alternatives --config mta
 2 プログラムがあり 'mta' を提供します。
 選択               コマンド
 -----------------------------------------------
 *  1               /usr/sbin/sendmail.sendmail
  + 2                /usr/sbin/sendmail.postfix
 Enter を押して現在の選択 [+] を保持するか、選択番号を入力します:
   <--- Enter を押す
 
バーチャルドメインの追加
通常使う papa-net.info ドメインの他に abc.papa-net.info ドメインを追加
 
 [root@
centos]# vi /etc/postfix/virtual
 
---最終行に以下を追記---
 
abc.papa-net.info anything
 @abc.papa-net.info @papa-net.info

 
設定の反映
 
 [root@
centos]# postmap /etc/postfix/virtual
 
Postfix の設定
 
 [root@
centos]# vi /etc/postfix/main.cf
 
---最終行に以下を追記---
 
virtual_maps = hash:/etc/postfix/virtual
 
Dovecot のインストール
バージョン:dovecot-2.2.10-8
 
 [root@
centos]# yum -y install dovecot
 
Dovecotの設定
設定ファイルが分割されているので下記のファイルを設定
 
 [root@
centos]# vi /etc/dovecot/dovecot.conf
 

 
途中略
 

 # Protocols we want to be serving.

 
#protocols = imap pop3 lmtp   <--- コメント解除 して書き換え
  

 protocols =
imap imaps pop3 pop3s lmtp

 
以下略
 
 
 [root@
centos]# 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

 
以下略
 
 
 [root@
centos]# vi /etc/dovecot/conf.d/10-ssl.conf
 ##
## SSL settings
##

# SSL/TLS support: yes, no, required. <doc/wiki/SSL.txt>
# 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 =
required   <--- 書き換え
  
 ssl =
no

 
以下略
 
 
 [root@
centos]# vi /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

 
途中略
 
 # Postfix smtp-auth
 
#unix_listener /var/spool/postfix/private/auth {
   <--- コメントを解除
  
  
unix_listener /var/spool/postfix/private/auth {
 
#   mode = 0666    <--- コメントを解除
  
     mode = 0666

    
user = postfix     <--- 追記
     group = postfix
   <--- 追記
 
#}    <--- コメントを解除
  
 }

 

 
以下略
 
Dovecotの起動
 
 [root@
centos]# systemctl start dovecot
 
Postfix を自動起動設定
 
 [root@
centos]# systemctl enable dovecot
 
自動起動設定の確認
 
 [root@
centos]# systemctl list-unit-files | grep dovecot
 
postfix.service                                    enabled   ← enabled になっていればOK
 
Mailサーバーの送受信テスト
・ポートチェック(ポート開放確認)

http://www.cman.jp/network/support/port.html にアクセスしてポートの開放状態をチェック

正常にアクセス出来ない場合はルーターの設定やファイアーフォールの設定を再度確認

・サーバー内送信テスト
 
 [root@
centos]# mail root    (root 宛てにメールを送信)
 
Subject: test   <--- 入力 (件名)
 
Hello           <--- 入力 (本文)
 
.         <--- 入力 (ドット入力で終了)
 
EOT
 
サーバー外送信テスト
 
 [root@
centos]# echo "Hello" | mail -s test abc@yahoo.com   <--- 入力 (自分のアドレス宛てメール送信)
 
サーバー内外送信テストを行って正常にメールが届けばOK もし、届かない場合は再度設定を確認

また、メールのログは下記にて確認

 
 [root@
centos]# more /var/log/maillog
 
メールユーザー
・新規ユーザー

新規ユーザー追加時に自動的にメールボックス作成

 
 [root@
centos]# mkdir -p /etc/skel/Maildir/new
 
[root@centos]# mkdir -p /etc/skel/Maildir/cur
 [root@centos]# mkdir -p /etc/skel/Maildir/tmp
 
アクセス権の設定
 
 [root@
centos]# chmod -R 700 /etc/skel/Maildir/
 
・既存ユーザー

既存ユーザーのメールボックス作成

 
 [root@
centos]# mkdir /home/****/Maildir       (ユーザー **** のメールボックス作成)
 [root@
centos]# mkdir /home/****/Maildir/new   (ユーザー **** のメールボックス作成)
 
[root@centos]# mkdir /home/****/Maildir/cur   (ユーザー **** のメールボックス作成)
 [root@centos]# mkdir /home/****/Maildir/tmp   (ユーザー **** のメールボックス作成)
 
アクセス権の設定
 
 [root@
centos]# chmod -R 700 /home/****/Maildir/   (ユーザー **** のアクセス権の設定)
 
所有権の設定
 
 [root@
centos]# chown -R **** /home/****   (ユーザー **** に所有権の設定)
 

戻る