Saikat

A Friendly Guide to 'ldapsearch'

๐ŸŒฑ Introduction

ldapsearch is a versatile command-line tool for querying LDAP directories like OpenLDAP or Active Directory. Great for sysadmins, SREs, or developers dealing with user directories.


โš™๏ธ Basic Syntax

ldapsearch [options] [filter] [attributes]

๐Ÿ› ๏ธ Common Options

OptionDescription
-xUse simple authentication
-H ldap://host:portSpecify LDAP server
-b "base_dn"Set search base DN
-D "bind_dn"Login identity (DN)
-w passwordPassword for bind DN
-WPrompt for password (preferred)
-LLLClean, LDIF-free output
-s scopeSearch scope: base, one, or sub
-z limitLimit number of entries

๐Ÿ” Code Samples

ldapsearch -x -H ldap://ldap.example.com -b "dc=example,dc=com"

Search for Specific User

ldapsearch -x -H ldap://ldap.example.com -b "dc=example,dc=com" \
  -D "cn=admin,dc=example,dc=com" -w adminpassword "(uid=jdoe)"

Get Only Specific Attributes

ldapsearch -x -b "dc=example,dc=com" "(objectClass=person)" cn mail telephoneNumber

Filter with AND Condition

ldapsearch -x -b "dc=example,dc=com" "(&(objectClass=person)(departmentNumber=Engineering))"

Use Secure Connection (LDAPS)

ldapsearch -x -H ldaps://ldap.example.com:636 -b "dc=example,dc=com"

Handle Large Result Sets

ldapsearch -x -b "dc=example,dc=com" -E pr=500/noprompt "objectClass=person"

๐Ÿ“Š Conceptual Overview

flowchart TD
  Start[Start] -->|Use -x| Auth[Simple Bind Auth]
  Auth --> Server[Specify LDAP Server (-H)]
  Server --> Base[Set Base DN (-b)]
  Base -->|Optional| BindDN[Bind DN (-D)]
  BindDN -->|Optional| Password[-w or -W]
  Base --> Filter[Search Filter]
  Filter -->|Optional| Attrs[Requested Attributes]
  Attrs -->|Optional| Output[-LLL for clean output]
  Output --> Done[Results]

๐Ÿงพ Conclusion

ldapsearch gives you direct, scriptable access to directory data. Mastering the filters and options makes you way more effective in dealing with directory services. Dive deeper with man ldapsearch or your LDAP vendor’s documentation.