Batch 3: SQL Databases LDAP YAML ← Full TOC

← LDAP Index

🔍 ldapsearch & CLI — ldapsearch, ldapadd, ldapmodify, ldapdelete command-line tools

The OpenLDAP command-line tools are the primary way to interact with any LDAP directory from Linux. They work against OpenLDAP, Active Directory, FreeIPA, and any other LDAP-compliant server.

📋 Reference

ItemSyntax / ValueDescription
ldapsearchldapsearch -x -H ldap://host -b baseDN filter [attrs]Search the directory
-xldapsearch -xSimple bind (no SASL)
-Hldapsearch -H ldap://ldap.example.comLDAP URI (ldap:// or ldaps://)
-bldapsearch -b 'dc=example,dc=com'Base DN for search
-Dldapsearch -D 'cn=admin,dc=example,dc=com'Bind DN (who you authenticate as)
-Wldapsearch -WPrompt for bind password
-wldapsearch -w 'password'Bind password on command line (insecure)
-sldapsearch -s base/one/subSearch scope: base=entry only, one=direct children, sub=subtree
-LLLldapsearch -LLLOutput clean LDIF without version/comments
-o ldif-wrap=noldapsearch -o ldif-wrap=noDisable line wrapping in output
filterldapsearch '(uid=alice)'LDAP search filter
attributesldapsearch '(uid=alice)' cn mail uidSpecify attributes to return (default: all)
'*'ldapsearch '(uid=alice)' '*'Return all user attributes
'+'ldapsearch '(uid=alice)' '+'Return operational attributes (createTimestamp etc.)
-zldapsearch -z 100Limit results to 100 entries
-lldapsearch -l 30Time limit in seconds
-Zldapsearch -ZUse StartTLS
ldapaddldapadd -x -H ldap://host -D bindDN -W -f file.ldifAdd entries from LDIF file
ldapmodifyldapmodify -x -H ldap://host -D bindDN -W -f modify.ldifModify entries from LDIF file
ldapdeleteldapdelete -x -H ldap://host -D bindDN -W dn1 dn2Delete entries by DN
ldappasswdldappasswd -x -H ldap://host -D bindDN -W -S userDNChange user password
ldapwhoamildapwhoami -x -H ldap://host -D bindDN -WShow current bind identity
slapcatsudo slapcat -n 0Export entire OpenLDAP database to LDIF (server-side)
slapaddsudo slapadd -n 0 -l backup.ldifImport LDIF directly to OpenLDAP (server must be stopped)
slaptestsudo slaptest -uTest slapd configuration

💡 Example

# ── ldapsearch examples ───────────────────────────────────────

# Search all users (anonymous)
ldapsearch -x -H ldap://localhost     -b "dc=mywebuniversity,dc=com"     "(objectClass=inetOrgPerson)"

# Find specific user
ldapsearch -x -H ldap://localhost     -D "cn=admin,dc=mywebuniversity,dc=com" -W     -b "ou=users,dc=mywebuniversity,dc=com"     "(uid=alice)" cn mail uid telephoneNumber

# Search with authentication, clean output
ldapsearch -LLL -x     -H ldaps://ldap.mywebuniversity.com     -D "uid=ldap-bind,ou=services,dc=mywebuniversity,dc=com" -W     -b "ou=users,dc=mywebuniversity,dc=com"     -s sub     "(&(objectClass=inetOrgPerson)(departmentNumber=Engineering))"     cn uid mail title

# Count all users
ldapsearch -LLL -x -H ldap://localhost     -b "ou=users,dc=mywebuniversity,dc=com"     "(objectClass=inetOrgPerson)" dn | grep "^dn:" | wc -l

# Find all members of a group
ldapsearch -LLL -x -H ldap://localhost     -b "ou=groups,dc=mywebuniversity,dc=com"     "(cn=developers)" member

# Check if user is in group (Active Directory style)
ldapsearch -LLL -x -H ldap://ad.example.com     -b "DC=example,DC=com"     "(&(objectClass=user)(sAMAccountName=alice)(memberOf=CN=Developers,OU=Groups,DC=example,DC=com))"

# Find users who haven't logged in recently (shadowLastChange)
ldapsearch -LLL -x -H ldap://localhost     -b "ou=users,dc=mywebuniversity,dc=com"     "(shadowLastChange<=18000)" uid cn mail

# Active Directory specific
ldapsearch -LLL -x     -H ldap://dc01.example.com     -D "DOMAIN\\binduser" -W     -b "DC=example,DC=com"     "(&(objectCategory=person)(objectClass=user)(sAMAccountName=alice))"     sAMAccountName displayName mail department memberOf

# Add entries
ldapadd -x -H ldap://localhost     -D "cn=admin,dc=mywebuniversity,dc=com" -W     -f add_users.ldif

# Modify entries
ldapmodify -x -H ldap://localhost     -D "cn=admin,dc=mywebuniversity,dc=com" -W     -f modify.ldif

# Delete a user
ldapdelete -x -H ldap://localhost     -D "cn=admin,dc=mywebuniversity,dc=com" -W     "uid=bob,ou=users,dc=mywebuniversity,dc=com"

# Change password
ldappasswd -x -H ldap://localhost     -D "cn=admin,dc=mywebuniversity,dc=com" -W     -S "uid=alice,ou=users,dc=mywebuniversity,dc=com"

# Backup entire directory
sudo slapcat > /backup/ldap-backup-$(date +%Y%m%d).ldif

# Test bind (authentication check)
ldapwhoami -x -H ldap://localhost     -D "uid=alice,ou=users,dc=mywebuniversity,dc=com" -W

← LDIF Format  |  🏠 Index  |  Python LDAP →