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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux Networking

From novice to tech pro — start learning today.