Centralizing Mail Credentials: A Hands‑On Guide to LDAP Authentication in Dovecot
Move Dovecot authentication from local files to LDAP with a step‑by‑step guide. Learn how to configure the ldap_passwd scheme, secure credentials, enable caching, and verify the setup in production.
29 Sept 2026, 05:56 UTC

Why LDAP for Dovecot?
When a mail server grows beyond a handful of users, keeping passwords in local /etc/dovecot/passwd becomes a maintenance nightmare. LDAP lets you store all credentials in a single directory service, making password changes, user provisioning, and group‑based ACLs a one‑stop shop. However, the jump from local to LDAP authentication introduces a handful of pitfalls that can lock out users or expose sensitive data if misconfigured.
Problem Statement
Administrators often find themselves dealing with two separate authentication mechanisms: local files for legacy accounts and LDAP for new ones. This dual setup can cause inconsistent login experiences and double the effort required to manage passwords. The goal is to replace the local file with LDAP without breaking existing mail flows.
Thesis
By configuring Dovecot’s ldap_passwd scheme, binding securely to the LDAP server, and enabling query caching, you can move all user authentication to LDAP while maintaining performance and security.
Step‑by‑Step Configuration
Install the LDAP support package. On Debian‑based systems this is
apt install dovecot-ldap. On RHEL/CentOS useyum install dovecot-ldap. The package provides thedovecot-ldapplugin and theldap_passwdschema.Secure the credentials file. Create a file
/etc/dovecot/conf.d/ldap.confand setchmod 600and ownership toroot:dovecotto protect the bind DN and password.Define the LDAP connection. In
ldap.confadd:hosts = ldap.example.com port = 636 ssl = start_tls binddn = cn=admin,dc=example,dc=com bindpw = <admin‑password>Replace placeholders with your server’s actual host, port, and bind credentials. Using TLS is mandatory in production to protect credentials in transit.
Specify the user lookup schema. Still in
ldap.conf:user_filter = (&(objectClass=inetOrgPerson)(uid=%u)) user_attrs = uid,mail,homeDirectory,mailHomeDirectory password_attr = userPassword password_scheme = ldap_passwdThe
user_filtertells Dovecot how to locate a user; the%uplaceholder expands to the username supplied by the client. Thepassword_schemeinstructs Dovecot to compare the client’s password against theuserPasswordattribute stored in LDAP.Enable caching. Add to
/etc/dovecot/conf.d/10-auth.conf:auth_mechanisms = plain login !include_try <mail_auth>Then create
/etc/dovecot/conf.d/20-lua.confwith a simple Lua script to cache LDAP queries if you have thedovecot-ldap-cacheplugin installed. A minimal script:local cache = require('cache') function ldap_cache(key) return cache.get(key) endFor many environments, the built‑in
dovecot-ldap-cacheis sufficient; no extra Lua is needed.Restart Dovecot. Run
systemctl restart dovecotand verify the service is active.
Concrete Worked Example
Suppose your LDAP tree looks like this:
| DN | Attributes |
|---|---|
| uid=jdoe,ou=Users,dc=example,dc=com | uid=jdoe,mail=[contact removed],userPassword=… |
| uid=asmith,ou=Users,dc=example,dc=com | uid=asmith,mail=[contact removed],userPassword=… |
With the configuration above, a client that connects to Dovecot and sends:
AUTHENTICATE LOGIN
jdoe
P@ssw0rd
will have Dovecot query LDAP for (uid=jdoe), retrieve the userPassword attribute, and compare it against the supplied password. If the hash matches, authentication succeeds and the user can open IMAP/POP3 sessions.
Verification Checklist
View current settings. Run
dovecot -non the server. Look for thehosts,binddn, anduser_filterlines under theldapsection.Test LDAP lookup. From the Dovecot host, execute:
ldapsearch -x -H ldap://ldap.example.com -b "ou=Users,dc=example,dc=com" "(uid=jdoe)"Ensure the output contains the expected attributes.
Attempt a real login. Use an IMAP client (e.g.,
openssl s_client -connect mail.example.com:993) to issue an AUTHENTICATE command. Observe the log entry in/var/log/dovecot.logfor anldapentry that shows the query and result.Check cache hits. In the log, look for
ldap_cache_hitorldap_cache_missmarkers to confirm caching is active.
Trade‑Offs and Limitations
- Performance vs. Freshness. Caching reduces round‑trips to LDAP but may serve stale passwords if the user changes credentials outside of Dovecot. The cache TTL (default 10 minutes) can be tuned via
cache_ttlin the LDAP config. - Single Point of Failure. If the LDAP server goes down, authentication fails for all users. Consider a secondary LDAP host or a fallback to local files for critical accounts.
- Credential Exposure. Storing the bind DN and password in
ldap.confrequires strict file permissions. Failure to secure this file can compromise the entire directory. - Complex Filters. Overly broad search filters can inadvertently expose unrelated entries. Always include the
objectClassconstraint and limit the scope withbase DN.
Actionable Closing
By following the configuration steps, securing credentials, and enabling caching, you can migrate all Dovecot authentication to LDAP with minimal disruption. Remember to monitor the logs for authentication failures and cache hit ratios. If you notice frequent cache misses, consider increasing the cache size or adjusting TTLs. Finally, test the setup in a staging environment before rolling out to production to catch any subtle misconfigurations.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.