Set up OpenLDAP with LDAPS

OpenLDAP reads its TLS settings from cn=config. Three attributes for certificate, key and CA, plus the listener on port 636, that’s it. Almost every problem comes from file permissions.

Prepare the files

sudo mkdir -p /etc/ldap/tls
sudo cp ldap.example.com.crt /etc/ldap/tls/server.crt   # certificate with intermediates
sudo cp ldap.example.com.key /etc/ldap/tls/server.key
sudo cp ca.crt               /etc/ldap/tls/ca.crt
sudo chown -R root:openldap /etc/ldap/tls
sudo chmod 640 /etc/ldap/tls/server.key

On Debian and Ubuntu the service runs as user openldap, on RHEL as ldap, where the files usually live in /etc/openldap/certs. On Ubuntu AppArmor limits which paths slapd may read. Below /etc/ldap/ is allowed; for other places you need an addition in /etc/apparmor.d/local/usr.sbin.slapd. If you have a PFX, the certificate converter extracts certificate, key and chain as PEM.

Add the certificate to cn=config

File tls.ldif. Best put all values into one change, because slapd checks certificate and key together:

dn: cn=config
changetype: modify
replace: olcTLSCACertificateFile
olcTLSCACertificateFile: /etc/ldap/tls/ca.crt
-
replace: olcTLSCertificateFile
olcTLSCertificateFile: /etc/ldap/tls/server.crt
-
replace: olcTLSCertificateKeyFile
olcTLSCertificateKeyFile: /etc/ldap/tls/server.key
-
replace: olcTLSProtocolMin
olcTLSProtocolMin: 3.3
sudo ldapmodify -Y EXTERNAL -H ldapi:/// -f tls.ldif

olcTLSProtocolMin: 3.3 means TLS 1.2 as the minimum.

Enable port 636

# Debian, Ubuntu: /etc/default/slapd
SLAPD_SERVICES="ldap:/// ldapi:/// ldaps:///"

# RHEL, Rocky, Alma: /etc/sysconfig/slapd
SLAPD_URLS="ldapi:/// ldap:/// ldaps:///"

sudo systemctl restart slapd
sudo ss -ltnp | grep slapd

Enforce encryption

Port 389 without TLS stays open as long as it is in the listener list. Either remove ldap:/// entirely, or require an encrypted connection (StartTLS or LDAPS) for every access:

dn: cn=config
changetype: modify
add: olcSecurity
olcSecurity: tls=1

slapd then rejects requests on port 389 unless the client has issued StartTLS first. Local access via ldapi:/// is not affected.

Test

# LDAPS
ldapwhoami -H ldaps://ldap.example.com -x -D "cn=admin,dc=example,dc=com" -W

# StartTLS on port 389 (-ZZ aborts if TLS fails)
ldapsearch -H ldap://ldap.example.com -ZZ -x -b "dc=example,dc=com" "(uid=jdoe)"

# certificate and chain
openssl s_client -connect ldap.example.com:636 -showcerts

The client verifies against the CA from /etc/ldap/ldap.conf (TLS_CACERT). The LDAP filter builder creates matching search filters.

Typical errors

FAQ

LDAPS or StartTLS?

Both are equally secure. LDAPS on port 636 is more widespread among applications. StartTLS has the advantage of needing only one port. What matters is that the client requires TLS instead of silently continuing unencrypted.

Do I need olcTLSCACertificateFile if I don’t use client certificates?

Not always, but depending on the build slapd requires it. Just enter the CA that issued your server certificate and you are on the safe side.

How do I renew with Let's Encrypt?

With certbot and a deploy hook that copies fullchain.pem and privkey.pem to /etc/ldap/tls, sets owner and permissions and restarts slapd.

Sources and documentation