ICT Diary

Network(主にCisco系)、Server(RedHat系)、Program(適当)を気まぐれにUPしていく。

CentOS7 メールサーバ(postfic dovecot)構築

環境情報



注意点



設定ファイルを編集する際は必ずバックアップを取ること

例:cp 設定ファイル名 設定ファイル名.org

手順



postfix



postfixのインストール

  • yumコマンドでpostfixをインストールする Command

      yum -y install postfix
    

    結果

      yum -y install postfix
      読み込んだプラグkvイン:fastestmirror
      Loading mirror speeds from cached hostfile
      * base: ftp.tsukuba.wide.ad.jp
      * epel: ftp.yz.yamagata-u.ac.jp
      * extras: ftp.tsukuba.wide.ad.jp
      * updates: ftp.tsukuba.wide.ad.jp
      依存性の解決をしています
      --> トランザクションの確認を実行しています。
      ---> パッケージ postfix.x86_64 2:2.10.1-6.el7 を インストール
      --> 依存性解決を終了しました。
    
      依存性を解決しました
    
      ===================================================================================================================
      Package                   アーキテクチャー         バージョン                        リポジトリー            容量
      ===================================================================================================================
      インストール中:
      postfix                   x86_64                   2:2.10.1-6.el7                    base                   2.4 M
    
      トランザクションの要約
      ===================================================================================================================
      インストール  1 パッケージ
    
      総ダウンロード容量: 2.4 M
      インストール容量: 12 M
      Downloading packages:
      postfix-2.10.1-6.el7.x86_64.rpm                                                             | 2.4 MB  00:00:00
      Running transaction check
      Running transaction test
      Transaction test succeeded
      Running transaction
      インストール中          : 2:postfix-2.10.1-6.el7.x86_64                                                       1/1
      検証中                  : 2:postfix-2.10.1-6.el7.x86_64                                                      1/1
    
      インストール:
      postfix.x86_64 2:2.10.1-6.el7
    
      完了しました!
    
  • インストール確認

    Command

      rpm -qa |grep postfix
    

    結果

      rpm -qa |grep postfix
      postfix-2.10.1-6.el7.x86_64
    

postfixの設定

設定ファイルパス

/etc/postfix/main.cf

設定

  • ドメインhoge.com
    • デフォルト

        mydomain = domain.tld(83行目)
      
    • 変更後

        mydomain = hoge.com
      
  • 外部メールの受信許可

    • デフォルト

        #inet_interfaces = all(113行目)
        inet_interfaces = localhost(116行目)
      
    • 変更後

        inet_interfaces = all(113行目):コメントアウト解除
        #inet_interfaces = localhost(116行目):コメントアウト
      
  • ホスト名:mail.hoge.com

    • デフォルト

        #myhostname = host.domain.tld(75行目)
        #myhostname = virtual.domain.tld(76行目)
      
    • 変更後

        myhostname = mail.hoge.com(76行目の下追記)
      
  • メールドメイン指定(アドレスの@以降)

    • デフォルト

        #myorigin = $myhostname(98行目)
        #myorigin = $mydomain(99行目)
      
    • 変更後

        #myorigin = $myhostname(98行目)
        myorigin = $mydomain(99行目)
      
  • ドメイン宛メールを受信

    • デフォルト

        mydestination = $myhostname, localhost.$mydomain, localhost(164行目)
        #mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain(165行目)
      
    • 変更後

        #mydestination = $myhostname, localhost.$mydomain, localhost(76行目)
        mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain(76行目)
      
  • メールボックスを指定:Maildir/

    • デフォルト

        #home_mailbox = Maildir/(419行目)
      
    • 変更後

        home_mailbox = Maildir/
      
  • 受信メールサイズの上限:5M
    • 変更後:最終行に追記(5MBに制限)

        message_size_limit = 5242880
      

postfixの起動

  • サービスの起動

      systemctl start postfix
    
  • サービスの自動起動

      systemctl enable postfix
    
  • postfixのステータスで起動・自動機能の確認

      systemctl status postfix
      ● postfix.service - Postfix Mail Transport Agent
      Loaded: loaded (/usr/lib/systemd/system/postfix.service; enabled; vendor preset: disabled)
      Active: active (running) since 金 2018-07-13 13:17:15 JST; 22s ago
      Main PID: 22793 (master)
      CGroup: /system.slice/postfix.service
              tq22793 /usr/libexec/postfix/master -w
              tq22794 pickup -l -t unix -u
              mq22795 qmgr -l -t unix -u
    
      7月 13 13:17:14 localhost.localdomain systemd[1]: Starting Postfix Mail Transport Agent...
      7月 13 13:17:15 localhost.localdomain postfix/master[22793]: daemon started -- version 2.10.1, configuratio...fix
      7月 13 13:17:15 localhost.localdomain systemd[1]: Started Postfix Mail Transport Agent.
      Hint: Some lines were ellipsized, use -l to show in full.
    

dovecot



dovecotのインストール

  • dovecotインストール Command

      yum –y install dovecot
    

    結果

      yum -y install dovecot
      読み込んだプラグイン:fastestmirror
      Loading mirror speeds from cached hostfile
      * base: ftp.tsukuba.wide.ad.jp
      * epel: mirror.dmmlabs.jp
      * extras: ftp.tsukuba.wide.ad.jp
      * updates: ftp.tsukuba.wide.ad.jp
      依存性の解決をしています
      --> トランザクションの確認を実行しています。
      ---> パッケージ dovecot.x86_64 1:2.2.10-8.el7 を インストール
      --> 依存性の処理をしています: libclucene-shared.so.1()(64bit) のパッケージ: 1:dovecot-2.2.10-8.el7.x86_64
      --> 依存性の処理をしています: libclucene-core.so.1()(64bit) のパッケージ: 1:dovecot-2.2.10-8.el7.x86_64
      --> トランザクションの確認を実行しています。
      ---> パッケージ clucene-core.x86_64 0:2.3.3.4-11.el7 を インストール
      --> 依存性解決を終了しました。
    
      依存性を解決しました
    
      ===================================================================================================================
      Package                       アーキテクチャー        バージョン                      リポジトリー           容量
      ===================================================================================================================
      インストール中:
      dovecot                       x86_64                  1:2.2.10-8.el7                  base                  3.2 M
      依存性関連でのインストールをします:
      clucene-core                  x86_64                  2.3.3.4-11.el7                  base                  528 k
    
      トランザクションの要約
      ===================================================================================================================
      インストール  1 パッケージ (+1 個の依存関係のパッケージ)
    
      総ダウンロード容量: 3.7 M
      インストール容量: 12 M
      Downloading packages:
      (1/2): clucene-core-2.3.3.4-11.el7.x86_64.rpm                                               | 528 kB  00:00:00
      (2/2): dovecot-2.2.10-8.el7.x86_64.rpm                                                      | 3.2 MB  00:00:00
      -------------------------------------------------------------------------------------------------------------------
      合計                                                                               3.3 MB/s | 3.7 MB  00:00:01
      Running transaction check
      Running transaction test
      Transaction test succeeded
      Running transaction
      インストール中          : clucene-core-2.3.3.4-11.el7.x86_64                                                 1/2
      インストール中          : 1:dovecot-2.2.10-8.el7.x86_64                                                      2/2
      検証中                  : clucene-core-2.3.3.4-11.el7.x86_64                                                 1/2
      検証中                  : 1:dovecot-2.2.10-8.el7.x86_64                                                      2/2
    
      インストール:
      dovecot.x86_64 1:2.2.10-8.el7
    
      依存性関連をインストールしました:
      clucene-core.x86_64 0:2.3.3.4-11.el7
    
      完了しました!
    
  • dovecotインストール確認 Command

      rpm -qa |grep dovecot
    

    結果

      rpm -qa |grep dovecot
      dovecot-2.2.10-8.el7.x86_64
    

dovecotの設定

  • メール格納形式を指定
    • ファイルパス

        /etc/dovecot/conf.d/10-mail.conf
      
    • 変更後:追記(30行の下)

        mail_location = maildir:~/Maildir
      
  • 使用するプロトコルの指定
    • ファイルパス

        /etc/dovecot/dovecot.conf
      
    • デフォルト

        #protocols = imap pop3 lmtp(24行目)
      
    • 変更後

        protocols = imap lmtp
      
  • プレインテキスト認証の許可
    • ファイルパス

        /etc/dovecot/conf.d/10-auth.conf
      
    • デフォルト

        #disable_plaintext_auth = yes
      
    • 変更後:10行目の下に追記

        disable_plaintext_auth = no
      
  • SSL接続の無効化
    • ファイルパス

        /etc/dovecot/conf.d/10-ssl.conf
      
    • デフォルト

        ssl = required(8行目)
      
    • 変更後

        ssl = no
      

dovecotの起動

  • サービスの起動 Command

      systemctl start dovecot
    

    結果

  • サービスの自動起動 Command

      systemctl enable dovecot
    

    結果

      systemctl enable dovecot
      Created symlink from /etc/systemd/system/multi-user.target.wants/dovecot.service to /usr/lib/systemd/system/dovecot.service.
    
  • postfixのステータスで起動・自動機能の確認

      systemctl status dovecot
      ● dovecot.service - Dovecot IMAP/POP3 email server
      Loaded: loaded (/usr/lib/systemd/system/dovecot.service; enabled; vendor preset: disabled)
      Active: active (running) since 金 2018-07-13 13:34:38 JST; 41min ago
      Main PID: 22942 (dovecot)
      CGroup: /system.slice/dovecot.service
              tq22942 /usr/sbin/dovecot -F
              tq22944 dovecot/anvil
              tq22945 dovecot/log
              mq22947 dovecot/config
    
      7月 13 13:34:38 localhost.localdomain systemd[1]: Starting Dovecot IMAP/POP3 email server...
      7月 13 13:34:38 localhost.localdomain systemd[1]: Started Dovecot IMAP/POP3 email server.
      7月 13 13:34:39 localhost.localdomain dovecot[22942]: master: Dovecot v2.2.10 starting up for imap, lmtp (c...ed)
      Hint: Some lines were ellipsized, use -l to show in full.
    

SMTP-AUTH



postfixsmtp-authの設定

  • SMTP-AUTHの有効化

    • ファイルパス

        /etc/postfix/main.cf
      
    • デフォルト
    • 変更後:最終行に追記

        smtpd_sasl_auth_enable  = yes
      
  • ドメイン設定

    • ファイルパス

        /etc/postfix/main.cf
      
    • デフォルト
    • 変更後:最終行に追記

        smtpd_sasl_local_domain = $myhostname
      
  • メールの受信の許可/拒否設定(SMTP認証されたものを許可,中継(リレー)禁止)
    • ファイルパス

        /etc/postfix/main.cf
      
    • デフォルト
    • 変更後:最終行に追記

        smtpd_recipient_restrictions = permit_mynetworks,
                                    permit_sasl_authenticated,
                                    reject_unauth_destination,
      

cyrus-sasl(smtp-auth)のインストール

  • cyrus-saslインストール Command

      yum –y install cyrus-sasl
    

    結果

      yum -y install cyrus-sasl
      読み込んだプラグイン:fastestmirror
      Loading mirror speeds from cached hostfile
      * base: ftp.tsukuba.wide.ad.jp
      * epel: ftp.yz.yamagata-u.ac.jp
      * extras: ftp.tsukuba.wide.ad.jp
      * updates: ftp.tsukuba.wide.ad.jp
      依存性の解決をしています
      --> トランザクションの確認を実行しています。
      ---> パッケージ cyrus-sasl.x86_64 0:2.1.26-23.el7 を インストール
      --> 依存性解決を終了しました。
    
      依存性を解決しました
    
      ===================================================================================================================
      Package                     アーキテクチャー        バージョン                        リポジトリー           容量
      ===================================================================================================================
      インストール中:
      cyrus-sasl                  x86_64                  2.1.26-23.el7                     base                   88 k
    
      トランザクションの要約
      ===================================================================================================================
      インストール  1 パッケージ
    
      総ダウンロード容量: 88 k
      インストール容量: 141 k
      Downloading packages:
      cyrus-sasl-2.1.26-23.el7.x86_64.rpm                                                         |  88 kB  00:00:00
      Running transaction check
      Running transaction test
      Transaction test succeeded
      Running transaction
      インストール中          : cyrus-sasl-2.1.26-23.el7.x86_64                                                    1/1
      検証中                  : cyrus-sasl-2.1.26-23.el7.x86_64                                                    1/1
    
      インストール:
      cyrus-sasl.x86_64 0:2.1.26-23.el7
    
      完了しました!
    
  • 確認 Command

      rpm -qa |grep cyrus-sasl
    

    結果

      rpm -qa |grep cyrus-sasl
      cyrus-sasl-lib-2.1.26-23.el7.x86_64
      cyrus-sasl-2.1.26-23.el7.x86_64
    

「cyrus-sasl-plain」パッケージのインストール

SMTP-AUTHでプレインテキスト認証を使用する場合、「cyrus-sasl-plain」パッケージの導入が必須となる。

SMTP-AUTHmd5認証を行う場合はまた別のパッケージの「」

  • パッケージのインストール Command

      yum -y install cyrus-sasl-plain
    

    結果

      yum install -y cyrus-sasl-plain
      読み込んだプラグイン:fastestmirror
      Loading mirror speeds from cached hostfile
      * base: ftp.tsukuba.wide.ad.jp
      * epel: mirror.dmmlabs.jp
      * extras: ftp.tsukuba.wide.ad.jp
      * updates: ftp.tsukuba.wide.ad.jp
      依存性の解決をしています
      --> トランザクションの確認を実行しています。
      ---> パッケージ cyrus-sasl-plain.x86_64 0:2.1.26-23.el7 を インストール
      --> 依存性解決を終了しました。
    
      依存性を解決しました
    
      ===================================================================================================================
      Package                          アーキテクチャー       バージョン                     リポジトリー          容量
      ===================================================================================================================
      インストール中:
      cyrus-sasl-plain                 x86_64                 2.1.26-23.el7                  base                  39 k
    
      トランザクションの要約
      ===================================================================================================================
      インストール  1 パッケージ
    
      総ダウンロード容量: 39 k
      インストール容量: 39 k
      Downloading packages:
      cyrus-sasl-plain-2.1.26-23.el7.x86_64.rpm                                                   |  39 kB  00:00:00
      Running transaction check
      Running transaction test
      Transaction test succeeded
      Running transaction
      インストール中          : cyrus-sasl-plain-2.1.26-23.el7.x86_64                                              1/1
      検証中                  : cyrus-sasl-plain-2.1.26-23.el7.x86_64                                              1/1
    
      インストール:
      cyrus-sasl-plain.x86_64 0:2.1.26-23.el7
    
      完了しました!
    
  • 確認 Command

      rpm -qa |grep cyrus-sasl-plain
    

    結果

      rpm -qa |grep cyrus-sasl-plain
      cyrus-sasl-plain-2.1.26-23.el7.x86_64
    

cyrus-sasl(smtp-auth)の設定

  • 認証方法 メール送信専用のユーザーを作成し、スワードファイルを利用する場合
    • ファイルパス

      /etc/sasl2/smtpd.conf

    • デフォルト

        pwcheck_method: saslauthd(1行目)
      
    • 変更後

        pwcheck_method: auxprop
      

cyrus-sasl(smtp-auth)の起動

  • サービスの起動 Command

      systemctl start saslauthd
    
  • サービスの自動起動 Command

      systemctl enable saslauthd
    

    結果

      systemctl enable saslauthd
      Created symlink from /etc/systemd/system/multi-user.target.wants/saslauthd.service to /usr/lib/systemd/system/saslauthd.service.
    
  • postfixのステータスで起動・自動機能の確認 Command

      systemctl status saslauthd
    

    結果

      systemctl status saslauthd
      ● saslauthd.service - SASL authentication daemon.
      Loaded: loaded (/usr/lib/systemd/system/saslauthd.service; enabled; vendor preset: disabled)
      Active: active (running) since 金 2018-07-13 14:31:33 JST; 1min 56s ago
      Main PID: 23047 (saslauthd)
      CGroup: /system.slice/saslauthd.service
              tq23047 /usr/sbin/saslauthd -m /run/saslauthd -a pam
              tq23048 /usr/sbin/saslauthd -m /run/saslauthd -a pam
              tq23049 /usr/sbin/saslauthd -m /run/saslauthd -a pam
              tq23050 /usr/sbin/saslauthd -m /run/saslauthd -a pam
              mq23051 /usr/sbin/saslauthd -m /run/saslauthd -a pam
    
      7月 13 14:31:33 localhost.localdomain systemd[1]: Starting SASL authentication daemon....
      7月 13 14:31:33 localhost.localdomain saslauthd[23047]: detach_tty      : master pid is: 23047
      7月 13 14:31:33 localhost.localdomain saslauthd[23047]: ipc_init        : listening on socket: /run/saslauthd/mux
      7月 13 14:31:33 localhost.localdomain systemd[1]: Started SASL authentication daemon..
      Hint: Some lines were ellipsized, use -l to show in full.
    

メール用新規ユーザ作成



  • ユーザ作成 ユーザ名

      mailtest
    

    Command

      useradd -s /sbin/nologin ユーザ名
    

    結果

      useradd -s /sbin/nologin mailtest
    
  • パスワード変更 Command

      passwd
    

    結果

      passwd mailtest
      ユーザー mailtest のパスワードを変更。
      新しいパスワード:
      よくないパスワード: このパスワードには一部に何らかの形でユーザー名が含まれています。
      新しいパスワードを再入力してください:
      passwd: すべての認証トークンが正しく更新できました。
    

SMTP認証用ユーザ/パスワード登録



  • 認証用ユーザ/パスワードの登録

    Command

      saslpasswd2 -u ドメイン名 -c ユーザ名
    

    結果

      saslpasswd2 -u mail.hoge.com -c mailtest
      Password:
      Again (for verification):
    

※この設定時のドメイン名は「/etc/postfix/main.cf」の設定項目である「smtpd_sasl_local_domain」の内容と必ず一致させる必要がある - 証用ユーザ/パスワードの確認 Command

    sasldblistusers2

結果

    sasldblistusers2
    mailtest@mail.hoge.com: userPassword

権限(グループ)変更



Command

chgrp postfix /etc/sasldb2

テスト



今回のメールサーバのテストでは「Telnet」コマンドを使用するので、サーバで「telnet」のコマンドが存在しない場合は、事前に「telnet」のパッケージをインストールしておく

  • Telnet」パッケージのインストール Command

      yum -y install telnet
    

    結果

      yum -y install telnet
      読み込んだプラグイン:fastestmirror
      Loading mirror speeds from cached hostfile
      * base: ftp.tsukuba.wide.ad.jp
      * epel: ftp.yz.yamagata-u.ac.jp
      * extras: ftp.tsukuba.wide.ad.jp
      * updates: ftp.tsukuba.wide.ad.jp
      依存性の解決をしています
      --> トランザクションの確認を実行しています。
      ---> パッケージ telnet.x86_64 1:0.17-64.el7 を インストール
      --> 依存性解決を終了しました。
    
      依存性を解決しました
    
      ===================================================================================================================
      Package                  アーキテクチャー         バージョン                         リポジトリー            容量
      ===================================================================================================================
      インストール中:
      telnet                   x86_64                   1:0.17-64.el7                      base                    64 k
    
      トランザクションの要約
      ===================================================================================================================
      インストール  1 パッケージ
    
      総ダウンロード容量: 64 k
      インストール容量: 113 k
      Downloading packages:
      telnet-0.17-64.el7.x86_64.rpm                                                               |  64 kB  00:00:00
      Running transaction check
      Running transaction test
      Transaction test succeeded
      Running transaction
      インストール中          : 1:telnet-0.17-64.el7.x86_64                                                        1/1
      検証中                  : 1:telnet-0.17-64.el7.x86_64                                                        1/1
    
      インストール:
      telnet.x86_64 1:0.17-64.el7
    
      完了しました!
    
  • インストール確認

    Command

      rpm -qa |grep telnet
    

    結果

      rpm -qa |grep telnet
      telnet-0.17-64.el7.x86_64
    

メール送信(自分から自分へ)

Command

telnet localhost 25

結果

telnet localhost 25
Trying ::1...
Connected to localhost.
Escape character is '^]'.
220 mail.hoge.com ESMTP Postfix
ehlo localhost
250-mail.hoge.com
250-PIPELINING
250-SIZE 5242880
250-VRFY
250-ETRN
250-AUTH PLAIN LOGIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
auth login                              #SMTP-AUTH(Plainテキスト認証)
334 VXNlcm5hbWU6
bWFpbHRlc3Q=                            #ユーザ名入力(base64に変換したもの)
334 UGFzc3dvcmQ6
bWFpbHRlc3Q=                            #パスワードを入力(base64に変換したもの)
235 2.7.0 Authentication successful
MAIL FROM:mailtest@hoge.com             #送信元メールサーバを指定
250 2.1.0 Ok
RCPT TO: mailtest@hoge.com              #送信先メールサーバを指定
250 2.1.5 Ok
DATA                                    #DATAメールの内容指定開始
354 End data with <CR><LF>.<CR><LF>
FROM: mailtest@hoge.com                 #メーラーに表示されるFromを指定
TO: mailtest@hoge.com                   #メーラーに表示されるToを指定
Subject:Send Test Mail                  #件名
Main messages                           #本文
.                                       #DATAメールの内容指定終了
250 2.0.0 Ok: queued as 9ADF81075EE1
quit                                    #メール送信の終了
221 2.0.0 Bye
Connection closed by foreign host.

メール受信

Command

telnet localhost 143

結果

telnet localhost 143
Trying ::1...
Connected to localhost.
Escape character is '^]'.
* OK [CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE IDLE AUTH=PLAIN] Dovecot ready.
a01 login mailtest mailtest      #ログイン
a01 OK [CAPABILITY IMAP4rev1 LITERAL+ SASL-IR LOGIN-REFERRALS ID ENABLE IDLE SORT SORT=DISPLAY THREAD=REFERENCES THREAD=REFS THREAD=ORDEREDSUBJECT MULTIAPPEND URL-PARTIAL CATENATE UNSELECT CHILDREN NAMESPACE UIDPLUS LIST-EXTENDED I18NLEVEL=1 CONDSTORE QRESYNC ESEARCH ESORT SEARCHRES WITHIN CONTEXT=SEARCH LIST-STATUS SPECIAL-USE BINARY MOVE] Logged in
a01 select inbox      #ディレクトリ選択
* FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
* OK [PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft \*)] Flags permitted.
* 1 EXISTS
* 0 RECENT
* OK [UIDVALIDITY 1531463085] UIDs valid
* OK [UIDNEXT 2] Predicted next UID
a01 OK [READ-WRITE] Select completed (0.001 secs).
a01 fetch 1 body[]      #一件目のメールを取得
* 1 FETCH (BODY[] {463}
Return-Path: <mailtest@hoge.com>
X-Original-To: mailtest@hoge.com
Delivered-To: mailtest@hoge.com
Received: from localhost (localhost [IPv6:::1])
        by mail.hoge.com (Postfix) with ESMTPA id 9ADF81075EE1
        for <mailtest@hoge.com>; Tue, 17 Jul 2018 13:10:36 +0900 (JST)
FROM: mailtest@hoge.com
TO: mailtest@hoge.com
Subject:Send Test Mail
Message-Id: <20180717041046.9ADF81075EE1@mail.hoge.com>
Date: Tue, 17 Jul 2018 13:10:36 +0900 (JST)

Main messages
)
a01 OK Fetch completed.
a01 logout      #ログアウト
* BYE Logging out
a01 OK Logout completed.
Connection closed by foreign host.