Skip to content

How to Fix Keycloak LDAP Sync Error

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about How to Fix Keycloak LDAP Sync Error. We cover key concepts, practical examples, and best practices.

Keycloak LDAP user federation fails to synchronize users — the sync shows 0 imported or the LDAP connection test fails with Unable to connect to LDAP from the Keycloak admin console.

The Problem

ERROR [org.keycloak.storage.ldap] Failed to sync LDAP users
LDAP connection error: javax.naming.CommunicationException:
localhost:389 [Connection refused (Connection refused)]

Step-by-Step Fix

Step 1: Test LDAP connection from Keycloak server

ldapsearch -H ldap://localhost:389 -D "cn=admin,dc=example,dc=com" -w password -b "dc=example,dc=com"

Step 2: Configure LDAP provider correctly

Vendor: Active Directory / OpenLDAP
Connection URL: ldap://ldap.example.com:389
Users DN: ou=users,dc=example,dc=com
Bind DN: cn=admin,dc=example,dc=com
Bind Credential: ****

Step 3: Set up attribute mapping

LDAP Attribute Keycloak Attribute
cn username
mail email
givenName firstName
sn lastName

Step 4: Configure periodic sync

Import Users: ON
Sync Registrations: ON
Periodic Full Sync: ON — every 86400 seconds
Periodic Changed Users Sync: ON — every 3600 seconds

Step 5: Test synchronization

In the Keycloak admin console, go to User Federation > ldap > Actions > Sync All Users.

Step 6: Check logs

sudo tail -f /opt/keycloak/data/log/server.log | grep -i ldap

Prevention Tips

  • Use a dedicated LDAP bind account instead of admin
  • Enable STARTTLS or LDAPS for production
  • Set realistic sync intervals (hourly for changes, daily for full)
  • Monitor LDAP server load during full syncs

Common Mistakes with ldap sync

  1. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  2. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  3. Misunderstanding that String is [Char] with poor performance for large text operations

These mistakes appear frequently in real-world KEYCLOAK code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why does Keycloak LDAP sync show 0 imported users?

The LDAP search base (Users DN) might be wrong, the filter might not match any users, or the bind credentials are incorrect. Verify with ldapsearch from the Keycloak server. Check the LDAP query in the Keycloak console under User Federation settings.

How do I use LDAPS with Keycloak?

Use ldaps://ldap.example.com:636 as the Connection URL. The Keycloak server must trust the LDAP server's SSL certificate. Import the certificate into the Java truststore: keytool -import -alias ldap -keystore $JAVA_HOME/lib/security/cacerts -file ldap-cert.pem.

What is the difference between Full Sync and Changed Users Sync?

Full Sync imports all LDAP users matching the filter. Changed Users Sync imports only users modified since the last sync (using the modifyTimestamp attribute). Use Changed Users Sync at shorter intervals for better performance.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro