そういうのがいいブログ

SIerで働く30代サラリーマンSEがインフラエンジニアに憧れてLinux・クラウド・AWSの勉強をするブログ

CentOS8でメール受信サーバを構築する手順【Dovecotインストール】

※[PR]当ブログの記事の中にはプロモーションが含まれています。

CentOS8でメール受信サーバを構築する手順

・CentOS8でローカルネットワーク内で使うメールサーバを構築したい。
・具体的な手順を教えてほしい。

こういった疑問に答えます。

本記事の内容

  1. CentOS8.2にPostfixDovecotをインストールしてメール送受信ができるサーバを構築する手順

この記事を書いている私は、某SIerに勤務しながら、
WEB系エンジニア・インフラエンジニアに憧れて、プログラミングおよびインフラ技術の勉強をしています。

こういった私が、解説していきます。

私が実機で試したコマンドや画像を載せて書いています。
記事の信頼性担保に繋がると思います。

souiunogaii.hatenablog.com

CentOS8.2にPostfixDovecotをインストールしてメール送受信ができるサーバを構築する手順

前提(Postfixのインストール)

前回の記事の手順で、CentOS8にPostfixのインストールが完了している状態です。

souiunogaii.hatenablog.com

Dovecot」をインストールすると、「POP3/IMAP4」でのメール受信ができるようになります。

Dovecotのインストール

dnf install dovecot
[root@SV-EARTH mail01]# dnf install dovecot
メタデータの期限切れの最終確認: 1:07:43 時間前の 20201024165600秒 に実施しました。
依存関係が解決しました。
========================================================================================================================
 パッケージ               アーキテクチャー   バージョン                                     リポジトリー          サイズ
========================================================================================================================
インストール中:
 dovecot                  x86_64             1:2.3.8-2.el8_2.2                              AppStream             5.0 M
依存関係のインストール中:
 clucene-core             x86_64             2.3.3.4-31.20130812.e8e3d20git.el8             AppStream             596 k

トランザクションの概要
========================================================================================================================
インストール  2 パッケージ

ダウンロードサイズの合計: 5.6 M
インストール済みのサイズ: 19 M
これでよろしいですか? [y/N]: y
パッケージのダウンロード:
(1/2): clucene-core-2.3.3.4-31.20130812.e8e3d20git.el8.x86_64.rpm                       2.9 MB/s | 596 kB     00:00
(2/2): dovecot-2.3.8-2.el8_2.2.x86_64.rpm                                               5.8 MB/s | 5.0 MB     00:00
------------------------------------------------------------------------------------------------------------------------
合計                                                                                    4.7 MB/s | 5.6 MB     00:01
トランザクションの確認を実行中
トランザクションの確認に成功しました。
トランザクションのテストを実行中
トランザクションのテストに成功しました。
トランザクションを実行中
  準備              :                                                                                               1/1
  インストール中    : clucene-core-2.3.3.4-31.20130812.e8e3d20git.el8.x86_64                                        1/2
  scriptlet の実行中: dovecot-1:2.3.8-2.el8_2.2.x86_64                                                              2/2
  インストール中    : dovecot-1:2.3.8-2.el8_2.2.x86_64 [=============================                             ] 2/2


  インストール中    : dovecot-1:2.3.8-2.el8_2.2.x86_64                                                              2/2
  scriptlet の実行中: dovecot-1:2.3.8-2.el8_2.2.x86_64                                                              2/2
  検証              : clucene-core-2.3.3.4-31.20130812.e8e3d20git.el8.x86_64                                        1/2
  検証              : dovecot-1:2.3.8-2.el8_2.2.x86_64                                                              2/2
Installed products updated.

インストール済み:
  clucene-core-2.3.3.4-31.20130812.e8e3d20git.el8.x86_64                dovecot-1:2.3.8-2.el8_2.2.x86_64

完了しました!

Dovecotの起動

systemctl start dovecot
systemctl status dovecot
[root@SV-EARTH mail01]# systemctl start dovecot
[root@SV-EARTH mail01]# systemctl status dovecot
● dovecot.service - Dovecot IMAP/POP3 email server
   Loaded: loaded (/usr/lib/systemd/system/dovecot.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2020-10-24 18:04:38 JST; 9s ago
     Docs: man:dovecot(1)
           http://wiki2.dovecot.org/
  Process: 83898 ExecStartPre=/usr/libexec/dovecot/prestartscript (code=exited, status=0/SUCCESS)
 Main PID: 83904 (dovecot)
    Tasks: 4 (limit: 47652)
   Memory: 4.9M
   CGroup: /system.slice/dovecot.service
           tq83904 /usr/sbin/dovecot -F
           tq83906 dovecot/anvil
           tq83907 dovecot/log
           mq83908 dovecot/config

1024 18:04:38 SV-EARTH systemd[1]: Starting Dovecot IMAP/POP3 email server...
1024 18:04:38 SV-EARTH systemd[1]: Started Dovecot IMAP/POP3 email server.
1024 18:04:38 SV-EARTH dovecot[83904]: master: Dovecot v2.3.8 (9df20d2db) starting up for imap, pop3, lmtp

Dovecot自動起動の設定

systemctl enable dovecot
[root@SV-EARTH mail01]# systemctl enable dovecot
Created symlink /etc/systemd/system/multi-user.target.wants/dovecot.service → /usr/lib/systemd/system/dovecot.service.

設定ファイル「dovecot.conf」の修正

変更前の「dovecot.conf」ファイルのバックアップ

cp -p /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf.original_bk
[root@SV-EARTH mail01]# cp -p /etc/dovecot/dovecot.conf /etc/dovecot/dovecot.conf.original_bk

dovecot.conf」ファイルの修正

vi /etc/dovecot/dovecot.conf
[root@SV-EARTH dovecot]# vi /etc/dovecot/dovecot.conf
protocols (プロトコル
protocols = imap pop3 lmtp
# Protocols we want to be serving.
#protocols = imap pop3 lmtp submission
protocols = imap pop3 lmtp
listen (IPv4のみ)
listen = *
# A comma separated list of IPs or hosts where to listen in for connections.
# "*" listens in all IPv4 interfaces, "::" listens in all IPv6 interfaces.
# If you want to specify non-default ports or anything more complex,
# edit conf.d/master.conf.
#listen = *, ::
listen = *

設定ファイル「10-mail.conf」の修正

vi /etc/dovecot/conf.d/10-mail.conf
[root@SV-EARTH conf.d]# vi /etc/dovecot/conf.d/10-mail.conf
mail_location (ファイルの場所)
mail_location = maildir:~/Maildir
# 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

設定ファイル「10-auth.conf」の修正

vi /etc/dovecot/conf.d/10-auth.conf
[root@SV-EARTH conf.d]# vi /etc/dovecot/conf.d/10-auth.conf
disable_plaintext_auth (平文で認証を許可)
disable_plaintext_auth = no
# 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.
# See also ssl=required setting.
#disable_plaintext_auth = yes
disable_plaintext_auth = no
auth_mechanisms (認証メカニズムにログインを追加)
auth_mechanisms = plain login
# Space separated list of wanted authentication mechanisms:
#   plain login digest-md5 cram-md5 ntlm rpa apop anonymous gssapi otp skey
#   gss-spnego
# NOTE: See also disable_plaintext_auth setting.
#auth_mechanisms = plain
auth_mechanisms = plain login

設定ファイル「10-ssl.conf」の修正

vi /etc/dovecot/conf.d/10-ssl.conf
[root@SV-EARTH conf.d]# vi /etc/dovecot/conf.d/10-ssl.conf
sslSSLは一旦、無効にする)
ssl = no
# 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

設定ファイル変更後のDovecotの再起動

systemctl restart dovecot
systemctl status dovecot
[root@SV-EARTH conf.d]# systemctl restart dovecot
[root@SV-EARTH conf.d]# 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 Sat 2020-10-24 18:15:07 JST; 8s ago
     Docs: man:dovecot(1)
           http://wiki2.dovecot.org/
  Process: 84104 ExecStop=/usr/bin/doveadm stop (code=exited, status=0/SUCCESS)
  Process: 84109 ExecStartPre=/usr/libexec/dovecot/prestartscript (code=exited, status=0/SUCCESS)
 Main PID: 84115 (dovecot)
    Tasks: 4 (limit: 47652)
   Memory: 4.8M
   CGroup: /system.slice/dovecot.service
           tq84115 /usr/sbin/dovecot -F
           tq84117 dovecot/anvil
           tq84118 dovecot/log
           mq84119 dovecot/config

1024 18:15:07 SV-EARTH systemd[1]: Starting Dovecot IMAP/POP3 email server...
1024 18:15:07 SV-EARTH systemd[1]: Started Dovecot IMAP/POP3 email server.
1024 18:15:07 SV-EARTH dovecot[84115]: master: Dovecot v2.3.8 (9df20d2db) starting up for imap, pop3, lmtp

Dovecotインストール後のメール受信テスト

telnetコマンドでメール受信のテスト

IMAP4の143番ポートに接続します。

telnet localhost 143
[root@SV-EARTH conf.d]# telnet localhost 143
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
* OK [CAPABILITY IMAP4rev1 SASL-IR LOGIN-REFERRALS ID ENABLE IDLE LITERAL+ AUTH=PLAIN AUTH=LOGIN] Dovecot ready.

ユーザー「mail01」でログイン

1 login mail01 ●●●●●●パスワード
1 login mail01 ●●●●●●パスワード
1 OK [CAPABILITY IMAP4rev1 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 BINARY MOVE SNIPPET=FUZZY PREVIEW=FUZZY LITERAL+ NOTIFY SPECIAL-USE] Logged in

ホームディレクトリの一覧表示

2 list "" *
2 list "" *
* LIST (\HasNoChildren) "." INBOX
2 OK List completed (0.042 + 0.000 + 0.042 secs).

受信ボックスの中身を見る

3 select INBOX
3 select INBOX
* FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
* OK [PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft \*)] Flags permitted.
* 1 EXISTS
* 1 RECENT
* OK [UNSEEN 1] First unseen.
* OK [UIDVALIDITY 1603530995] UIDs valid
* OK [UIDNEXT 2] Predicted next UID
3 OK [READ-WRITE] Select completed (0.027 + 0.000 + 0.026 secs).

1通目のメールを見る。

4 fetch 1 body[]
4 fetch 1 body[]
* 1 FETCH (FLAGS (\Seen \Recent) BODY[] {436}
Return-Path: <mail01@ad.planet.jp>
X-Original-To: mail01@ad.planet.jp
Delivered-To: mail01@ad.planet.jp
Received: from localhost (localhost [127.0.0.1])
        by SV-EARTH.ad.planet.jp (Postfix) with SMTP id D0460C2600
        for <mail01@ad.planet.jp>; Sat, 24 Oct 2020 17:59:42 +0900 (JST)
Message-Id: <20201024090001.D0460C2600@SV-EARTH.ad.planet.jp>
Date: Sat, 24 Oct 2020 17:59:42 +0900 (JST)
From: mail01@ad.planet.jp

helo postfix
)
4 OK Fetch completed (0.003 + 0.000 + 0.002 secs).

終了します。

5 logout
5 logout
* BYE Logging out
5 OK Logout completed (0.001 + 0.000 secs).
Connection closed by foreign host.

Postfixの設定ファイル「main.cf」の修正

ローカルネットワークのアドレスを追記します。

vi /etc/postfix/main.cf
[root@SV-EARTH postfix]# vi /etc/postfix/main.cf
mynetworks
mynetworks = localhost, 192.168.1.0/24
# You can also specify the absolute pathname of a pattern file instead
# of listing the patterns here. Specify type:table for table-based lookups
# (the value on the table right-hand side is not used).
#
#mynetworks = 168.100.189.0/28, 127.0.0.0/8
#mynetworks = $config_directory/mynetworks
#mynetworks = hash:/etc/postfix/network_table
#mynetworks = localhost
mynetworks = localhost, 192.168.1.0/24

設定ファイル変更後のPostfix再起動

 systemctl restart postfix
[root@SV-EARTH dovecot]# systemctl restart postfix

Dovecotの設定ファイル「dovecot.conf」の修正

ローカルネットワークのアドレスを追記します。

vi /etc/dovecot/dovecot.conf
[root@SV-EARTH dovecot]# vi /etc/dovecot/dovecot.conf
login_trusted_networks
login_trusted_networks = 192.168.1.0/24
# Space separated list of trusted network ranges. Connections from these
# IPs are allowed to override their IP addresses and ports (for logging and
# for authentication checks). disable_plaintext_auth is also ignored for
# these networks. Typically you'd specify your IMAP proxy servers here.
#login_trusted_networks =
login_trusted_networks = 192.168.1.0/24

設定ファイル変更後のDovecot再起動

 systemctl restart dovecot
[root@SV-EARTH dovecot]# systemctl restart dovecot

ポートの確認

SMTPの25番ポート、POP3110番ポート、IMAP4の143番ポート、が「LISTEN」になっていることを確認します。

netstat -nat
[root@SV-EARTH dovecot]# netstat -nat
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:110             0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:143             0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:25              0.0.0.0:*               LISTEN

ファイアーウォールの設定変更

firewall-cmd --zone=public --add-service=pop3 --permanent
firewall-cmd --zone=public --add-service=imap --permanent
firewall-cmd --zone=public --add-service=smtp --permanent
firewall-cmd --reload
[root@SV-EARTH dovecot]# firewall-cmd --zone=public --add-service=pop3 --permanent
Warning: ALREADY_ENABLED: pop3
success
[root@SV-EARTH dovecot]# firewall-cmd --zone=public --add-service=imap --permanent
Warning: ALREADY_ENABLED: imap
success
[root@SV-EARTH dovecot]# firewall-cmd --zone=public --add-service=smtp --permanent
Warning: ALREADY_ENABLED: smtp
success
[root@SV-EARTH dovecot]# firewall-cmd --reload
success

設定変更後のファイアーウォールの確認

firewall-cmd --list-all
[root@SV-EARTH dovecot]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: enp2s0
  sources:
  services: cockpit dhcpv6-client dns freeipa-ldaps freeipa-trust imap pop3 samba smtp
  ports: 3259-3268/tcp 49152-65535/tcp
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

メールソフトでの受信テスト・送信テスト

Windows10上のメールソフト「Mozilla Thunderbird」を使ってメール受信のテストをします。

CentOS8でメール受信サーバを構築する手順

www.thunderbird.net

メールソフトのアカウント設定

以下のように手動でアカウント設定をします。

CentOS8でメール受信サーバを構築する手順

SSL暗号化を無しにしているので、「警告!」の画面が表示されます。
「接続する上での危険性を理解しました」のチェックを入れて「完了」

CentOS8でメール受信サーバを構築する手順

メールソフトの受信テスト

CentOS8でメール受信サーバを構築する手順

Postfixのインストール時に送信していたテストメールが受信できました。

CentOS8でメール受信サーバを構築する手順

サーバー側の受信ログの確認

/var/log/maillog
Oct 24 18:48:16 sv-earth dovecot[84357]: pop3-login: Login: user=<mail01>, method=PLAIN, rip=192.168.1.101, lip=192.168.1.102, mpid=84493, secured, session=<r0yvmGeyCOLAqAFl>
Oct 24 18:48:16 sv-earth dovecot[84357]: pop3(mail01)<84493><r0yvmGeyCOLAqAFl>: Disconnected: Logged out top=0/0, retr=0/0, del=0/1, size=436

メールソフトの送信テスト

CentOS8でメール受信サーバを構築する手順

CentOS8でメール受信サーバを構築する手順

サーバー側の送信ログの確認

/var/log/maillog
Oct 24 21:11:10 sv-earth postfix/smtpd[85237]: connect from pc-ganymede[192.168.1.101]
Oct 24 21:11:10 sv-earth postfix/smtpd[85237]: AF9ABCD9AC: client=pc-ganymede[192.168.1.101]
Oct 24 21:11:10 sv-earth postfix/cleanup[85240]: AF9ABCD9AC: message-id=<0be12c86-5e6c-575b-593e-902fddbcaac1@ad.planet.jp>
Oct 24 21:11:10 sv-earth postfix/qmgr[84303]: AF9ABCD9AC: from=<mail01@ad.planet.jp>, size=638, nrcpt=2 (queue active)
Oct 24 21:11:10 sv-earth postfix/smtpd[85237]: disconnect from pc-ganymede[192.168.1.101] ehlo=1 mail=1 rcpt=2 data=1 quit=1 commands=6
Oct 24 21:11:10 sv-earth postfix/local[85241]: AF9ABCD9AC: to=<mail01@ad.planet.jp>, relay=local, delay=0.09, delays=0.06/0.02/0/0.01, dsn=2.0.0, status=sent (delivered to maildir)
Oct 24 21:11:10 sv-earth postfix/local[85241]: AF9ABCD9AC: to=<mail02@ad.planet.jp>, relay=local, delay=0.1, delays=0.06/0.03/0/0, dsn=2.0.0, status=sent (delivered to maildir)
Oct 24 21:11:10 sv-earth postfix/qmgr[84303]: AF9ABCD9AC: removed

まとめ

CentOS8.2にPostfixDovecotをインストールしてメール送受信ができるサーバを構築する手順を紹介しました。

参考サイト

www.rem-system.com

qiita.com