Running an LDAP Query from command line in Linux


I want to run an LDAP query from a command line on a linux system?.
 i.e., I want to run the following query to look up a user's DN:


How can I run this LDAP query from a linux command line and have it return query results for the provided username?

Thank You
Who is Participating?
sweetfa2Connect With a Mentor Commented:
ldapsearch -V -h -b 'YourADDN' -x -s base -z 1 -l 1 '(&(objectCategory=Person)(objectClass=user)(sAMAccountName=username))' @inetOrgPerson

Open in new window

Charlie_MelegaAuthor Commented:
Thank You sweetfa2.
I've bolded a couple of variables below that I would kindly ask clarification. YourADDN is your Active Directory DN?   Not sure what the reference to @inetOrgPerson is;

ldapsearch -V -h -b 'YourADDN' -x -s base -z 1 -l 1 '(&(objectCategory=Person)(objectClass=user)(sAMAccountName=username))' @inetOrgPerson
You are correct with YourADDN.

inetOrgPerson is the list of attributes to retrieve (equivalent to person)
Charlie_MelegaAuthor Commented:
Hello sweefta2,

I have one final question:

I am sure I have a syntax error as when I typed what I believed to be relevant command, it simply returned a syntax page as if i typed -help.

My DC machine is named
My Domain is cm.loc

Based on your guidance, I attempted the following:

ldapsearch -V -h -b 'DC=cm,DC=loc' -x -s base -z 1 -l 1 '(&(objectCategory=Person)(objectClass=user)(sAMAccountName=username))' @inetOrgPerson

Thanks for any guidance
I presume you have found your answer as you have marked this as solved.

If not, try "man ldapsearch" for other possible options.

Alternatively do a google search on ldapsearch syntax examples to give you other examples of how to do what you wish.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.