Dovecot(ダヴコット、[ˈdʌvkɒt])とは、Unix系のOS上で動作する、IMAPおよびPOP3のサーバである。
Ubuntu 24.04 に Dovecot をインストールし、基本的なメール受信機能(IMAP/POP3)を設定する手順を、FQDN mail.mydomain.net に基づいて説明します。
sudo apt update sudo apt install dovecot-core dovecot-imapd dovecot-pop3d
出力例
Reading package lists... Done Building dependency tree... Done Reading state information... Done The following additional packages will be installed: libexttextcat-2.0-0 libexttextcat-data Suggested packages: dovecot-gssapi dovecot-ldap dovecot-lmtpd dovecot-managesieved dovecot-mysql dovecot-pgsql dovecot-sieve dovecot-solr dovecot-sqlite dovecot-submissiond The following NEW packages will be installed: dovecot-core dovecot-imapd dovecot-pop3d libexttextcat-2.0-0 libexttextcat-data 0 upgraded, 5 newly installed, 0 to remove and 1 not upgraded. Need to get 3,788 kB of archives. After this operation, 12.1 MB of additional disk space will be used. Do you want to continue? [Y/n] Y ← Yを入力
hostnamectl set-hostname mail.mydomain.net
出力例
==== AUTHENTICATING FOR org.freedesktop.hostname1.set-static-hostname ==== Authentication is required to set the statically configured local hostname, as well as the pretty hostname. Authenticating as: ,,, (USERNAME) Password: (←作業用ユーザーのパスワードを入力) ==== AUTHENTICATION COMPLETE ====
または
/etc/hostname に直接 mail.mydomain.net と記載。
sudo nano /etc/hostname
hostname -f
出力例
mail.mydomain.net
sudo mkdir -p /etc/skel/Maildir sudo maildirmake.dovecot /etc/skel/Maildir sudo maildirmake.dovecot /etc/skel/Maildir/.Drafts sudo maildirmake.dovecot /etc/skel/Maildir/.Sent sudo maildirmake.dovecot /etc/skel/Maildir/.Trash
sudo maildirmake.dovecot /home/USERNAME/Maildir sudo chown -R USERNAME:USERNAME /home/USERNAME/Maildir
設定ファイルは /etc/dovecot/ 以下にあります。
drwxr-xr-x 2 root root 4096 Mar 21 22:50 conf.d -rw-r--r-- 1 root root 4343 Aug 27 2024 dovecot.conf -rw-r----- 1 root dovecot 1507 Sep 14 2023 dovecot-dict-auth.conf.ext -rw-r----- 1 root dovecot 522 Sep 14 2023 dovecot-dict-sql.conf.ext -rw-r----- 1 root dovecot 5824 Sep 14 2023 dovecot-sql.conf.ext drwx------ 2 root root 4096 Mar 21 22:50 private
ls -la /etc/dovecot/conf.d
出力例
drwxr-xr-x 2 root root 4096 Mar 21 22:50 . drwxr-xr-x 4 root root 4096 Mar 21 23:14 .. -rw-r--r-- 1 root root 5248 Sep 14 2023 10-auth.conf -rw-r--r-- 1 root root 1781 Sep 14 2023 10-director.conf -rw-r--r-- 1 root root 3757 Sep 14 2023 10-logging.conf -rw-r--r-- 1 root root 18068 Aug 27 2024 10-mail.conf -rw-r--r-- 1 root root 3619 Sep 14 2023 10-master.conf -rw-r--r-- 1 root root 3482 Aug 27 2024 10-ssl.conf -rw-r--r-- 1 root root 291 Aug 27 2024 10-tcpwrapper.conf -rw-r--r-- 1 root root 1657 Sep 14 2023 15-lda.conf -rw-r--r-- 1 root root 3111 Sep 14 2023 15-mailboxes.conf -rw-r--r-- 1 root root 4520 Sep 14 2023 20-imap.conf -rw-r--r-- 1 root root 4066 Sep 14 2023 20-pop3.conf -rw-r--r-- 1 root root 676 Sep 14 2023 90-acl.conf -rw-r--r-- 1 root root 292 Sep 14 2023 90-plugin.conf -rw-r--r-- 1 root root 2596 Sep 14 2023 90-quota.conf -rw-r--r-- 1 root root 499 Sep 14 2023 auth-checkpassword.conf.ext -rw-r--r-- 1 root root 489 Sep 14 2023 auth-deny.conf.ext -rw-r--r-- 1 root root 343 Sep 14 2023 auth-dict.conf.ext -rw-r--r-- 1 root root 561 Sep 14 2023 auth-master.conf.ext -rw-r--r-- 1 root root 515 Sep 14 2023 auth-passwdfile.conf.ext -rw-r--r-- 1 root root 788 Sep 14 2023 auth-sql.conf.ext -rw-r--r-- 1 root root 611 Sep 14 2023 auth-static.conf.ext -rw-r--r-- 1 root root 2182 Sep 14 2023 auth-system.conf.ext
いろいろな設定ファイルがあるんですね。
sudo nano /etc/dovecot/dovecot.conf
↓
!include_try /usr/share/dovecot/protocols.d/*.protocol dict { } !include conf.d/*.conf
デフォルトでこの構成になっているはずです。
10-mail.conf は、Dovecot の設定ファイルの1つで、主にメールボックスの保存場所(mail_location)や形式(Maildir など)に関する設定を記述するファイルです。
設定ファイルを編集する前にバックアップ(コピー)を取っておきます。
sudo cp /etc/dovecot/conf.d/10-mail.conf /etc/dovecot/conf.d/10-mail.conf.bak
include conf.d/*.confこの指定は、conf.d ディレクトリ内の「.conf」で終わるファイルだけを読み込むという意味です。
10-mail.confを編集して、メールのデータをMaildir形式にします。
sudo nano /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 = mbox:~/mail:INBOX=/var/mail/%u
この行を以下ように変更します。(コメントアウトして追加してもOK)
mail_location = maildir:~/Maildir
10-auth.conf は、Dovecot の認証に関する設定ファイルです。
Dovecot はメールの受信時にユーザー認証(ログイン)を行うため、どのように認証するか(システムユーザー、SQL、パスワードファイルなど)をこのファイルで定義します。
設定ファイルを編集する前にバックアップ(コピー)を取っておきます。
sudo cp /etc/dovecot/conf.d/10-auth.conf /etc/dovecot/conf.d/10-auth.conf.bak
sudo nano /etc/dovecot/conf.d/10-auth.conf
次の行を確認・変更(パスワードはシステムアカウントと連動させる場合):
disable_plaintext_auth = no auth_mechanisms = plain login !include auth-system.conf.ext
sudo nano /etc/dovecot/conf.d/10-ssl.conf
Let’s Encrypt や自己署名証明書を使って SSL/TLS を有効にします。
ssl = yes ssl_cert = </etc/letsencrypt/live/mail.mydomain.net/fullchain.pem ssl_key = </etc/letsencrypt/live/mail.mydomain.net/privkey.pem
証明書が未取得の場合、後から設定可能です(仮に ssl = no として進めることも可能)。
sudo systemctl restart dovecot sudo systemctl enable dovecot
IMAP(ポート143, 993)、POP3(ポート110, 995)が開いているか確認:
sudo ss -tnlp | grep dovecot
問題がある場合はログをチェック:
sudo journalctl -u dovecot sudo tail -f /var/log/mail.log
sudo ufw allow 143,993/tcp # IMAP sudo ufw allow 110,995/tcp # POP3