6.4. ldapsearch
ldapsearch is a configurable utility that locates and retrieves directory entries via LDAP. This utility opens a connection to the specified server using the specified distinguished name and password and locates entries based on a specified search filter. Search scopes can include a single entry, an entry's immediate subentries, or an entire tree or subtree. Search results are returned in LDIF format.
ldapsearch [
-b basedn
] [
optional_options
] [
filter
] [
optional_list_of_attributes
]
For any value that contains a space ( ), the value should be enclosed in double quotation marks. For example:
-b "ou=groups, dc=example,dc=com"
| Option | Description |
|---|---|
| optional_options | A series of command-line options. These must be specified before the search filter, if used. |
| filter |
An LDAP search filter as described in Directory Server Administration Guide. Do not specify a search filter if search filters are supplied in a file using the -f option.
|
| optional_list_of_attributes | A list of space-separated attributes that reduce the scope of the attributes returned in the search results. This list of attributes must appear after the search filter. For a usage example, see the Directory Server Administration Guide. If a list of attributes is not specified, the search returns values for all attributes permitted by the access control set in the directory with the exception of operational attributes. |
To return operational attributes as a result of a search operation, they must be explicitly specified in the search command. To retrieve regular attributes along with explicitly-specified operational attributes, specify an asterisk (*) in addition to the operational attributes.
The following table lists the most commonly used ldapsearch command-line options.
| Option | Description |
|---|---|
-b
|
Specifies the starting point for the search. The value specified here must be a distinguished name that currently exists in the database. This option is optional if the The value specified in this option should be provided in double quotation marks. For example: -b "cn=Barbara Jensen, ou=Product Development, dc=example,dc=com"
The root DSE entry is a special entry that contains a list of all the suffixes supported by the local directory. To search this entry, supply a search base of -b "" -s base "objectclass=*" |
-D
|
Specifies the distinguished name with which to authenticate to the server. This option is optional if anonymous access is supported by the server. If specified, this value must be a DN recognized by the Directory Server, and it must also have the authority to search for the entries. For example:
-D "uid=bjensen, dc=example,dc=com" |
-g
|
Specifies that the password policy request control not be sent with the bind request. By default, the new LDAP password policy request control is sent with bind requests.
The
The criticality of the request control is set to |
-h
|
Specifies the hostname or IP address of the machine on which the Directory Server is installed. If a host is not specified, ldapsearch uses the local host. For example:
-h mozilla |
-l
|
Specifies the maximum number of seconds to wait for a search request to complete. For example: -l 300
Regardless of the value specified here, |
-p
|
Specifies the TCP port number that the Directory Server uses. For example:
-p 1049The default is 389. If -Z is used, the default is 636.
|
-s
|
Specifies the scope of the search. The scope can be one of the following:
|
-w
|
Specifies the password associated with the distinguished name that is specified in the -w diner892 If this option is not specified, anonymous access is used. If a dash (-) is used as the password value, the utility prompts for the password after the command is entered. This avoids having the password on the command line. |
-x
|
Specifies that the search results are sorted on the server rather than on the client. This is useful to sort according to a matching rule, as with an international search. In general, it is faster to sort on the server rather than on the client. |
-z
|
Specifies the maximum number of entries to return in response to a search request. For example: -z 1000
Normally, regardless of the value specified here, |
The following command-line options can be used to specify that ldapsearch use LDAPS when communicating with an SSL-enabled Directory Server or used for certificate-based authentication. These options are valid only when LDAPS has been turned on and configured for the Directory Server. For information on certificate-based authentication and creating a certificate database for use with LDAP clients, see the "Managing SSL" chapter in the Directory Server Administration Guide.
In addition to the standard ldapsearch options, to run an ldapsearch command using SSL, specify the following:
ldapsearch {
-Z, -ZZ, -ZZZ
} [
-p secure_port
] [
-P certificate_database
] [
-N certificate_name
] [
-K key_database
] [
-W key database password
]
To run ldapsearch over TLS/SSL, either the -Z option is required (for SSL) or the -ZZ or -ZZZ option is required (for Start TLS).
|
Option |
Description |
|---|---|
-3
|
Specifies that hostnames should be checked in SSL certificates. |
-I
|
Specifies the SSL key password file that contains the token:password pair. |
-K
|
Specifies the absolute path, including the filename, of the private key database of the client.
The |
-m
|
Specifies the path to the security module database, such as /etc/dirsrv/slapd-. This option only need to be given if the security module database is in a different directory than the certificate database itself.
|
-N
|
Specifies the certificate name to use for certificate-based client authentication, such as -N "Server-Cert". If this option is specified, then the -Z, -P, and -W options are required. Also, if this option is specified, then the -D and -w options must not be specified, or certificate-based authentication will not occur, and the bind operation will use the authentication credentials specified on -D and -w.
|
-P
|
Specifies the absolute path, including the option, of the certificate database of the client. This option is used only with the When used on a machine where an SSL-enabled web browser is configured, the path specified on this option can be that of the certificate database for the browser. For example: -P /security/cert.db
The client security files can also be stored on the Directory Server in the -P /etc/dirsrv/slapd-
|
-Q
|
Specifies the token and certificate name, which is separated by a semi-colon (:) for PKCS11. |
-W
|
Specifies the password for the private key database identified in the -P option. For example:
-W serverpasswordIf a dash (-) is used as the password value, the utility promptes for the password after the command is entered. This avoids having the password on the command line. |
-Z
|
Specifies that SSL is to be used for the search request. |
-ZZ
|
Specifies the Start TLS request. Use this option to make a cleartext connection into a secure one. If the server does not support Start TLS, the command does not have to be aborted; it will continue in cleartext. |
-ZZZ
|
Enforces the Start TLS request. The server must respond that the request was successful. If the server does not support Start TLS, such as Start TLS is not enabled or the certificate information is incorrect, the command is aborted immediately. |
SASL mechanisms can be used to authenticate a user, using the -o the required SASL information.
To learn which SASL mechanisms are supported, search the root DSE. See the -b option in Table 6.3, “Commonly-Used ldapsearch Options”.
| Option | Description |
|---|---|
-o
|
Specifies SASL options. The format is -o saslOption=value. saslOption can have one of six values:
-o can be used multiple times to pass all of the required SASL information for the mechanism. For example:
-o "mech=DIGEST-MD5" -o "authzid=test_user" -o "authid=test_user" |
There are three SASL mechanisms supported in Red Hat Directory Server:
CRAM-MD5, described in Table 6.6, “Description of CRAM-MD5 Mechanism Options”
DIGEST-MD5, described in Table 6.7, “Description of DIGEST-MD5 SASL Mechanism Options”
GSSAPI, described in Table 6.8, “Description of GSSAPI SASL Mechanism Options”
| Required or Optional | Option | Description | Example |
|---|---|---|---|
| Required | mech=CRAM-MD5 | Gives the SASL mechanism. | -o “mech=CRAM-MD5” |
| Required | authid=authid_value |
Gives the ID used to authenticate to the server. authid_value can be the following:
|
-o “authid=dn:uid=msmith,ou=People,o=example.com" |
| Optional | secprop=value |
The secprop attribute sets the security properties for the connection. The secprop value can be any of the following:
|
-o "secprop=noplain,minssf=1,maxbufsize=512" |
| Required or Optional | Option | Description | Example |
|---|---|---|---|
| Required | mech=DIGEST-MD5 | Gives the SASL mechanism. | -o “mech=DIGEST-MD5” |
| Required | authid=authid_value |
Gives the ID used to authenticate to the server. authid_value can be the following:
|
-o “authid=dn:uid=msmith,ou=People,o=example.com" |
| Optional | secprop=value |
The secprop attribute sets the security properties for the connection. The secprop value can be any of the following:
|
-o “secprop=noplain,noanonymous, maxssf=128,minssf=128” |
| Required or Optional | Option | Description | Example |
|---|---|---|---|
| Required | mech=GSSAPI |
Gives the SASL mechanism.
NOTEHave the Kerberos ticket before issuing a GSS-API request. |
-o “mech=GSSAPI” |
| Optional | secprop=value |
The secprop attribute sets the security properties for the connection. The secprop value can be any of the following:
|
-o “secprop=noplain,noanonymous, maxssf=56,minssf=56” |
| Option | Description |
|---|---|
-A
|
Specifies that the search retrieve the attributes only, not the attribute values. This option is useful to determine if an attribute is present for an entry and the value is not important. |
-a
|
Specifies how alias dereferencing is completed. Values can be never, always, search, or find. The default value is never.
|
-B
|
Print non-ASCII values using the old output format (attrName=attrValue). |
-c
|
Specifies the getEffectiveRights control authzid. For example:
dn:uid=bjensen,dc=example,dc=comA value of "" means the authorization ID for the operation. A value of dn: means anonymous
|
-F
|
Specifies a different separator. This option allows a separator other than a colon (:) to separate an attribute name from the corresponding value. For example:
-F + |
-f
|
Specifies the file containing the search filters to be used in the search. For example: -f search_filters option to supply a search filter directly to the command line. For more information about search filters, see Appendix B, "Finding Directory Entries", in the Directory Server Administration Guide. |
-G
|
Conducts a virtual list view search. This option can set the number of entries before or after the search target and the index or value of the first entry returned.
For example, a value operation that sorts by surname,
An index operation which sorts by surname, |
-i
|
Specifies the characterset to use for command-line input. The default is the characterset specified in the This argument can input the bind DN, base DN, and the search filter pattern in the specified characterset.
|
-J
|
Send an arbitrary control. This option can be used in the following format to retrieve access control information on a specific entry:
-J
|
-k
|
Bypasses converting the password to UTF8. |
-M
|
Manages smart referrals. This causes the server not to return the smart referral contained on the entry but, instead, to return the actual entry containing the referral. Use this option to search for entries that contain smart referrals. For more information about smart referrals, see the "Configuring Directory Databases" chapter in the Directory Server Administration Guide. |
-n
|
Specifies that the search is not actually to be performed, but that ldapsearch is to show what it would do with the specified input.
|
-O
|
Specifies the maximum number of referral hops ldapsearch should automatically follow. For example:
-O 2 |
-R
|
Specifies that referrals are not to be followed automatically. By default, referrals are followed automatically. |
-S
|
Specifies the attribute to use as the sort criteria. For example:
-S snUse multiple -S arguments to further define the sort order. In the following example, the search results will be sorted first by surname and then by given name:
-S sn -S givennameThe default is not to sort the returned entries. |
-T
|
Specifies that no line breaks should be used within individual values in the search results. |
-t
|
Specifies that the results be written to a set of temporary files. With this option, each attribute value is placed in a separate file within the system temporary directory. No base-64 encoding is performed on the values, regardless of the content. |
-u
|
Specifies that the user-friendly form of the distinguished name be used in the output. |
-v
|
Specifies that the utility is to run in verbose mode. |
-V
|
Specifies the LDAP version number to be used on the search. For example:
-V 2LDAPv3 is the default. An LDAPv3 search cannot be performed against a Directory Server that only supports LDAPv2. |
-Y
|
Specifies the proxy DN to use for the search. This argument is provided for testing purposes. For more information about proxied authorization, see the "Managing Access Control" chapter in the Directory Server Administration Guide. |
-X
|
Specifies the getEffectiveRights control specific attribute list, where attributes are separated by spaces. For example:
"nsroledn userPassword" |