Skip to content

Installing Dovecot for IMAPS — the operating model

Install and configure Dovecot for IMAPS (993) and POP3S (995) with TLS, Maildir storage, virtual users, and the SASL socket Postfix needs for submission auth.

Postfix gets the mail in; Dovecot lets the user read it. This article is the Dovecot install we pair with postfix-install-ubuntu: package on, Maildir on disk, IMAPS and POP3S listening with the same Let’s Encrypt cert Postfix uses, and a SASL socket exposed to Postfix so submission auth on port 587 actually works. The deliverable is a Dovecot a client can connect to and a Postfix that can ask “is this password valid?” through the right socket.

How to verify

Audit before installing — Dovecot may already be present, an alternate IMAP server (Cyrus, Courier) may be on the box, or another process may hold the standard ports.

ss -ltnp | grep -E ':(143|993|110|995)\b'
dpkg -l | grep -E 'dovecot|cyrus-imap|courier'
ls -ld /var/mail /home/*/Maildir 2>/dev/null
getent passwd | awk -F: '$3 >= 1000 { print $1 }'
sudo doveconf -n 2>/dev/null | head -5

The mail directory layout matters here: /var/mail/ is the historical mbox spool, ~/Maildir/ is the per-user Maildir tree. We standardize on Maildir under /var/vmail/<domain>/<user>/ for virtual users — it is the only layout that scales to multi-domain without permission war crimes.

What’s happening

Dovecot is a modular IMAP/POP3 server. The base dovecot-core package gives you the master process and core auth; the per-protocol packages (dovecot-imapd, dovecot-pop3d, dovecot-lmtpd, dovecot-managesieved) light up the listeners you actually want. On Ubuntu, configuration lives in /etc/dovecot/ with a dovecot.conf entry point that pulls in conf.d/*.conf snippets. Each snippet covers one concern — authentication, mail location, master sockets, SSL, logging.

The user model splits into two axes:

  • System users vs. virtual users. System users live in /etc/passwd and have shell accounts; virtual users live in a flat passwd file, a SQL table, or LDAP. Virtual users scale; system users do not.
  • Password database (passdb) vs. user database (userdb). Passdb checks the password; userdb returns the UID/GID/home for the authenticated identity. They can share a source (one passwd-file for both) or come from different places.

For SMTP authentication, Postfix talks to Dovecot’s auth-client socket, hands it the SASL exchange, and gets back accept/reject. The socket is a UNIX domain socket under /var/spool/postfix/private/auth — Postfix’s chroot — owned by postfix:postfix.

The procedure

  1. Install the packages you need, not the meta-package. dovecot-imapd for IMAP, dovecot-lmtpd for accepting mail from Postfix via LMTP, and dovecot-pop3d only if you genuinely need POP3:

    sudo apt update
    sudo apt install -y dovecot-core dovecot-imapd dovecot-lmtpd dovecot-managesieved
    systemctl is-active dovecot
  2. Create the virtual mail user and storage tree. One UID owns all virtual mail; per-domain subdirectories partition tenants:

    sudo groupadd -g 5000 vmail
    sudo useradd -u 5000 -g vmail -s /usr/sbin/nologin -d /var/vmail -m vmail
    sudo mkdir -p /var/vmail/example.com
    sudo chown -R vmail:vmail /var/vmail
    sudo chmod 770 /var/vmail
  3. Set the mail location and storage format. Edit /etc/dovecot/conf.d/10-mail.conf:

    mail_location = maildir:/var/vmail/%d/%n/Maildir
    mail_uid = 5000
    mail_gid = 5000
    mail_privileged_group = vmail
    namespace inbox {
        inbox = yes
        separator = /
    }

    %d is the domain part of the login (example.com), %n is the local part (alice). The Maildir tree is per-user.

  4. Wire authentication to a passwd-file. Edit /etc/dovecot/conf.d/10-auth.conf:

    disable_plaintext_auth = yes
    auth_mechanisms = plain login
    !include auth-passwdfile.conf.ext

    Then /etc/dovecot/conf.d/auth-passwdfile.conf.ext:

    passdb {
        driver = passwd-file
        args = scheme=ARGON2ID username_format=%u /etc/dovecot/users
    }
    userdb {
        driver = static
        args = uid=vmail gid=vmail home=/var/vmail/%d/%n
    }

    Generate a hash and add a user to /etc/dovecot/users:

    sudo apt install -y libargon2-1
    doveadm pw -s ARGON2ID -p 'real-strong-passphrase'
    # output starts with {ARGON2ID}$argon2id$v=19$...
    echo 'alice@example.com:{ARGON2ID}$argon2id$...::::::' | sudo tee -a /etc/dovecot/users
    sudo chown root:dovecot /etc/dovecot/users
    sudo chmod 640 /etc/dovecot/users
  5. Enable IMAPS with the shared Let’s Encrypt cert. Edit /etc/dovecot/conf.d/10-ssl.conf:

    ssl = required
    ssl_cert = </etc/letsencrypt/live/mx1.example.com/fullchain.pem
    ssl_key  = </etc/letsencrypt/live/mx1.example.com/privkey.pem
    ssl_min_protocol = TLSv1.2
    ssl_cipher_list = ECDHE+AESGCM:ECDHE+CHACHA20:!aNULL:!MD5
    ssl_prefer_server_ciphers = yes

    The < prefix tells Dovecot to read the file contents, not treat the path as a string.

  6. Expose the SASL socket to Postfix. Edit /etc/dovecot/conf.d/10-master.conf, find the service auth block:

    service auth {
        unix_listener /var/spool/postfix/private/auth {
            mode = 0660
            user = postfix
            group = postfix
        }
        unix_listener auth-userdb {
            mode = 0600
            user = vmail
        }
    }
    
    service lmtp {
        unix_listener /var/spool/postfix/private/dovecot-lmtp {
            mode = 0600
            user = postfix
            group = postfix
        }
    }

    The path lives inside Postfix’s chroot. The lmtp socket is what Postfix uses to hand delivered mail off to Dovecot.

  7. Reload, verify the listener, and connect once with openssl:

    sudo doveconf -n > /tmp/dovecot.actual.conf
    sudo systemctl reload dovecot
    ss -ltnp | grep -E ':(143|993)\b'
    openssl s_client -connect mx1.example.com:993 -servername mx1.example.com -crlf </dev/null
    # paste once connected:
    # a1 LOGIN alice@example.com real-strong-passphrase
    # a2 LIST "" "*"
    # a3 LOGOUT

    You should see OK Logged in and a list with at least INBOX. If you see NO Authentication failed, the hash format or the userdb path is wrong — doveadm auth test alice@example.com is the cleanest debug command.

Common pitfalls

  • The mail_uid / mail_gid are set to vmail but the Maildir on disk is owned by root from an earlier mkdir — every IMAP login fails with “Permission denied” until you chown the tree.
  • Postfix and Dovecot share the Let’s Encrypt cert, but only one of them has the --post-hook to reload on renewal — the other keeps the old cert in memory; we always set both daemons in a single --post-hook string.
  • ssl = required is correct for production but locks out a legacy client doing port 143 cleartext — the client thinks the server is down; check /var/log/mail.log before assuming a network problem.
  • The SASL socket is created with the wrong owner because the service auth block was edited but Dovecot wasn’t reloaded — Postfix’s submission/smtpd errors with Connect to private/auth: Permission denied.
  • Mail rules in Sieve scripts (~/.dovecot.sieve) are silently ignored if dovecot-sieve isn’t installed; the package is separate from dovecot-managesieved (the latter is the protocol, the former is the executor).

Stack Harbor runs Dovecot as a paired artifact with Postfix — single TLS material, single auth source of truth, both daemons under one reload contract, and the IMAP listener verified from outside the network before handing the stack back. If you have a self-hosted IMAP server where logins fail intermittently or the spool has crept into terabytes without a retention plan, that work falls under our Managed Operations practice.