1.3. Setting up a Dovecot server with MariaDB SQL authentication


If you store users and passwords in a MariaDB SQL server, you can configure Dovecot to use it as the user database and authentication backend. With this configuration, you manage accounts centrally in a database, and users have no local access to the file system on the Dovecot server.

Centrally managed accounts are also a benefit if you plan to set up multiple Dovecot servers with replication to make your mailboxes highly available.

1.3.1. Installing Dovecot

The dovecot package provides:

  • The dovecot service and the utilities to maintain it
  • Services that Dovecot starts on demand, such as for authentication
  • Plugins, such as server-side mail filtering
  • Configuration files in the /etc/dovecot/ directory
  • Documentation in the /usr/share/doc/dovecot/ directory

Procedure

  • Install the dovecot package:

    # dnf install dovecot
    참고

    If Dovecot is already installed and you require clean configuration files, rename or remove the /etc/dovecot/ directory. Afterwards, reinstall the package. Without removing the configuration files, the dnf reinstall dovecot command does not reset the configuration files in /etc/dovecot/.

1.3.2. Configuring TLS encryption on a Dovecot server

Dovecot provides a secure default configuration. For example, TLS is enabled by default to transmit credentials and data encrypted over networks. To configure TLS on a Dovecot server, you only need to set the paths to the certificate and private key files. Additionally, you can increase the security of TLS connections by generating and by using Diffie-Hellman parameters to provide perfect forward secrecy (PFS).

Prerequisites

  • Dovecot is installed.
  • The following files have been copied to the listed locations on the server:

    • The server certificate: /etc/pki/dovecot/certs/server.example.com.crt
    • The private key: /etc/pki/dovecot/private/server.example.com.key
    • The Certificate Authority (CA) certificate: /etc/pki/dovecot/certs/ca.crt
  • The hostname in the Subject DN field of the server certificate matches the server’s Fully-qualified Domain Name (FQDN).
  • If the FIPS mode is enabled, clients must either support the Extended Master Secret (EMS) extension or use TLS 1.3. TLS 1.2 connections without EMS fail. For more information, see the Red Hat Knowledgebase solution TLS extension "Extended Master Secret" enforced.

Procedure

  1. Set secure permissions on the private key file:

    # chown root:root /etc/pki/dovecot/private/server.example.com.key
    # chmod 600 /etc/pki/dovecot/private/server.example.com.key
  2. Generate a file with Diffie-Hellman parameters:

    # openssl dhparam -out /etc/dovecot/dh.pem 4096

    Depending on the hardware and entropy on the server, generating Diffie-Hellman parameters with 4096 bits can take several minutes.

  3. Set the paths to the certificate and private key files in the /etc/dovecot/conf.d/10-ssl.conf file:

    1. Update the ssl_cert and ssl_key parameters, and set them to use the paths of the server’s certificate and private key:

      ssl_cert = </etc/pki/dovecot/certs/server.example.com.crt
      ssl_key = </etc/pki/dovecot/private/server.example.com.key
    2. Uncomment the ssl_ca parameter, and set it to use the path to the CA certificate:

      ssl_ca = </etc/pki/dovecot/certs/ca.crt
    3. Uncomment the ssl_dh parameter, and set it to use the path to the Diffie-Hellman parameters file:

      ssl_dh = </etc/dovecot/dh.pem
    중요

    To ensure that Dovecot reads the value of a parameter from a file, the path must start with a leading < character.

1.3.3. Preparing Dovecot to use virtual users

By default, Dovecot performs many actions on the file system as the user who uses the service. However, configuring the Dovecot back end to use one local user to perform these actions has several benefits:

  • Dovecot performs file system actions as a specific local user instead of using the user’s ID (UID).
  • Users do not need to be available locally on the server.
  • You can store all mailboxes and user-specific files in one root directory.
  • Users do not require a UID and group ID (GID), which reduces administration efforts.
  • Users who have access to the file system on the server cannot compromise their mailboxes or indexes because they cannot access these files.
  • Setting up replication is easier.

Prerequisites

  • Dovecot is installed.

Procedure

  1. Create the vmail user:

    # useradd --home-dir /var/mail/ --shell /usr/sbin/nologin vmail

    Dovecot will later use this user to manage the mailboxes. For security reasons, do not use the dovecot or dovenull system users for this purpose.

  2. If you use a different path than /var/mail/, set the mail_spool_t SELinux context on it, for example:

    # semanage fcontext -a -t mail_spool_t "<path>(/.)?"*
    # restorecon -Rv <path>
  3. Grant write permissions on /var/mail/ only to the vmail user:

    # chown vmail:vmail /var/mail/
    # chmod 700 /var/mail/
  4. Uncomment the mail_location parameter in the /etc/dovecot/conf.d/10-mail.conf file, and set it to the mailbox format and location:

    mail_location = sdbox:/var/mail/%n/

    With this setting:

    • Dovecot uses the high-performant dbox mailbox format in single mode. In this mode, the service stores each mail in a separate file, similar to the maildir format.
    • Dovecot resolves the %n variable in the path to the username. This is required to ensure that each user has a separate directory for its mailbox.

1.3.4. Using a MariaDB SQL database as the Dovecot authentication backend

Dovecot can read accounts and passwords from a MariaDB database and use it to authenticate users when they log in to the IMAP or POP3 service. The benefits of this authentication method include:

  • Administrators can manage users centrally in a database.
  • Users have no access locally on the server.

Prerequisites

  • Dovecot is installed.
  • The virtual users feature is configured.
  • Connections to the MariaDB server support TLS encryption.
  • The dovecotDB database exists in MariaDB, and the users table contains at least a username and password column.
  • The password column contains passwords encrypted with a scheme that Dovecot supports.
  • The passwords either use the same scheme or have a {pw-storage-scheme} prefix.
  • The dovecot MariaDB user has read permission on the users table in the dovecotDB database.
  • The certificate of the Certificate Authority (CA) that issued the MariaDB server’s TLS certificate is stored on the Dovecot server in the /etc/pki/tls/certs/ca.crt file.
  • If the FIPS mode is enabled, this Dovecot server supports the Extended Master Secret (EMS) extension or uses TLS 1.3. TLS 1.2 connections without EMS fail. For more information, see the Red Hat Knowledgebase solution TLS extension "Extended Master Secret" enforced.

Procedure

  1. Install the dovecot-mysql package:

    # dnf install dovecot-mysql
  2. Configure the authentication backends in the /etc/dovecot/conf.d/10-auth.conf file:

    1. Comment out include statements for auth-*.conf.ext authentication backend configuration files that you do not require, for example:

      #!include auth-system.conf.ext
    2. Enable SQL authentication by uncommenting the following line:

      !include auth-sql.conf.ext
  3. Edit the /etc/dovecot/conf.d/auth-sql.conf.ext file, and add the override_fields parameter to the userdb section as follows:

    userdb {
      driver = sql
      args = /etc/dovecot/dovecot-sql.conf.ext
      override_fields = uid=vmail gid=vmail home=/var/mail/%n/
    }

    Due to the fixed values, Dovecot does not query these settings from the SQL server.

  4. Create the /etc/dovecot/dovecot-sql.conf.ext file with the following settings:

    driver = mysql**
    connect = host=mariadb_srv.example.com dbname=dovecotDB user=dovecot password=_<dovecotPW>_ ssl_ca=/etc/pki/tls/certs/ca.crt
    default_pass_scheme = SHA512-CRYPT
    user_query = SELECT username FROM users WHERE username='%u';
    password_query = SELECT username AS user, password FROM users WHERE username='%u';
    iterate_query = SELECT username FROM users;

    To use TLS encryption to the database server, set the ssl_ca option to the path of the certificate of the CA that issued the MariaDB server certificate. For a working certificate validation, the hostname of the MariaDB server must match the hostname used in its TLS certificate.

    If the password values in the database contain a {<pw-storage-scheme>} prefix, you can omit the default_pass_scheme setting.

    The queries in the file must be set as follows:

    • For the user_query parameter, the query must return the username of the Dovecot user. The query must also return only one result.
    • For the password_query parameter, the query must return the username and the password, and Dovecot must use these values in the user and password variables. Therefore, if the database uses different column names, use the AS SQL command to rename a column in the result.
    • For the iterate_query parameter, the query must return a list of all users.
  5. Set secure permissions on the /etc/dovecot/dovecot-sql.conf.ext file:

    # chown root:root /etc/dovecot/dovecot-sql.conf.ext
    # chmod 600 /etc/dovecot/dovecot-sql.conf.ext

1.3.5. Completing the Dovecot configuration

Once you have installed and configured Dovecot, open the required ports in the firewalld service, and enable and start the service. Afterwards, you can test the server.

Prerequisites

  • The following has been configured in Dovecot:

    • TLS encryption
    • An authentication backend
  • Clients trust the Certificate Authority (CA) certificate.

Procedure

  1. If you want to provide only an IMAP or POP3 service to users, uncomment the protocols parameter in the /etc/dovecot/dovecot.conf file, and set it to the required protocols. For example, if you do not require POP3, set:

    protocols = imap lmtp

    By default, the imap, pop3, and lmtp protocols are enabled.

  2. Open the ports in the local firewall. For example, to open the ports for the IMAPS, IMAP, POP3S, and POP3 protocols, enter:

    # firewall-cmd --permanent --add-service=imaps --add-service=imap --add-service=pop3s --add-service=pop3
    # firewall-cmd --reload
  3. Enable and start the dovecot service:

    # systemctl enable --now dovecot

Verification

  1. Use a mail client, such as Mozilla Thunderbird, to connect to Dovecot and read emails. The settings for the mail client depend on the protocol you want to use:

    Expand
    표 1.3. Connection settings to the Dovecot server
    ProtocolPortConnection securityAuthentication method

    IMAP

    143

    STARTTLS

    PLAIN[a]

    IMAPS

    993

    SSL/TLS

    PLAIN[a]

    POP3

    110

    STARTTLS

    PLAIN[a]

    POP3S

    995

    SSL/TLS

    PLAIN[a]

    [a] The client transmits data encrypted through the TLS connection. Consequently, credentials are not disclosed.

    Note that this table does not list settings for unencrypted connections because, by default, Dovecot does not accept plain text authentication on connections without TLS.

  2. Display configuration settings with non-default values:

    # doveconf -n
Red Hat logoGithubredditYoutubeTwitter

자세한 정보

평가판, 구매 및 판매

커뮤니티

Red Hat 문서 정보

Red Hat을 사용하는 고객은 신뢰할 수 있는 콘텐츠가 포함된 제품과 서비스를 통해 혁신하고 목표를 달성할 수 있습니다. 최신 업데이트를 확인하세요.

보다 포괄적 수용을 위한 오픈 소스 용어 교체

Red Hat은 코드, 문서, 웹 속성에서 문제가 있는 언어를 교체하기 위해 최선을 다하고 있습니다. 자세한 내용은 다음을 참조하세요.Red Hat 블로그.

Red Hat 소개

Red Hat은 기업이 핵심 데이터 센터에서 네트워크 에지에 이르기까지 플랫폼과 환경 전반에서 더 쉽게 작업할 수 있도록 강화된 솔루션을 제공합니다.

Theme

© 2026 Red Hat
맨 위로 이동