|
OpenLDAP命令汇总
- ldapsearch:搜索 OpenLDAP 目录树条目。
- ldapadd:通过 LDIF 格式,添加目录树条目。
- ldapdelete:删除 OpenLDAP 目录树条目。
- ldapmodify:修改 OpenLDAP 目录树条目。
- ldapwhoami:效验 OpenLDAP 用户的身份。
- ldapmodrdn:判断 OpenLDAP 目录树 DN 条目。
- ldapcompare:判断 DN 值和指定参数值是否属于同一个条目。
- ldappasswd:修改 OpenLDAP 目录树用户条目实现密码重置。
- slaptest:验证 slapd.conf 文件或 cn=配置目录。
- slapindex:创建 OpenLDAP 目录树索引,提供查询效率。
- slapcat:将数据小牧转换为 OpenLDAP 的 LDIF 文件。
ldapadd 命令
选项 | 描述 | -x | 进行简单认证 | -D | 用来绑定服务器的DN | -h | 目录服务的地址 | -w | 绑定DN的密码 | -f | 使用ldif文件进行条目添加的文件 |
首先,我们准备一个 test.ldif 文件,命令如下:
内容如下:
dn: uid=xzz,ou=Users,dc=itsvse,dc=com
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount
homeDirectory: /home/itsvse_xzz
loginShell: /bin/bash
cn: xzz
uidNumber: 1000
gidNumber: 1000
sn: System Administrator
mail: xzz@itsvse.com
postalAddress: shanghai
mobile: 13788888888
添加命令如下:
输入密码后,添加成功,如下图:
ldapmodify 命令
ldapmodify 命令是固定的,主要是通过配置文件实现不同的修改功能。
demo.ldif 文件如下,表示修改 uid=xzz 这个用户的 uidNumber 参数。
ldappasswd 命令
ldappasswd 可打开与 LDAP 服务器的连接,修改条目密码。
选项 | 描述 | -x | 进行简单认证 | -D | 用来绑定服务器的DN | -w | 绑定DN的密码 | -S | 提示的输入密码 | -s | pass 把密码设置为pass | -a | pass 设置old passwd为pass | -A | 提示的设置old passwd | -H | 是指要绑定的服务器 | -I | 使用sasl会话方式 |
ldapsearch 命令
ldap 搜索命令
[root@VM_0_9_centos ~]# ldapsearch -h
ldapsearch: option requires an argument -- 'h'
ldapsearch: unrecognized option -h
usage: ldapsearch [options] [filter [attributes...]]
where:
filter RFC 4515 compliant LDAP search filter
attributes whitespace-separated list of attribute descriptions
which may include:
1.1 no attributes
* all user attributes
+ all operational attributes
Search options:
-a deref one of never (default), always, search, or find
-A retrieve attribute names only (no values)
-b basedn base dn for search
-c continuous operation mode (do not stop on errors)
-E [!]<ext>[=<extparam>] search extensions (! indicates criticality)
[!]domainScope (domain scope)
!dontUseCopy (Don't Use Copy)
[!]mv=<filter> (RFC 3876 matched values filter)
[!]pr=<size>[/prompt|noprompt] (RFC 2696 paged results/prompt)
[!]sss=[-]<attr[:OID]>[/[-]<attr[:OID]>...]
(RFC 2891 server side sorting)
[!]subentries[=true|false] (RFC 3672 subentries)
[!]sync=ro[/<cookie>] (RFC 4533 LDAP Sync refreshOnly)
rp[/<cookie>][/<slimit>] (refreshAndPersist)
[!]vlv=<before>/<after>(/<offset>/<count>|:<value>)
(ldapv3-vlv-09 virtual list views)
[!]deref=derefAttr:attr[,...][;derefAttr:attr[,...][;...]]
[!]<oid>[=:<b64value>] (generic control; no response handling)
-f file read operations from `file'
-F prefix URL prefix for files (default: file:///tmp/)
-l limit time limit (in seconds, or "none" or "max") for search
-L print responses in LDIFv1 format
-LL print responses in LDIF format without comments
-LLL print responses in LDIF format without comments
and version
-M enable Manage DSA IT control (-MM to make critical)
-P version protocol version (default: 3)
-s scope one of base, one, sub or children (search scope)
-S attr sort the results by attribute `attr'
-t write binary values to files in temporary directory
-tt write all values to files in temporary directory
-T path write files to directory specified by path (default: /tmp)
-u include User Friendly entry names in the output
-z limit size limit (in entries, or "none" or "max") for search
Common options:
-d level set LDAP debugging level to `level'
-D binddn bind DN
-e [!]<ext>[=<extparam>] general extensions (! indicates criticality)
[!]assert=<filter> (RFC 4528; a RFC 4515 Filter string)
[!]authzid=<authzid> (RFC 4370; "dn:<dn>" or "u:<user>")
[!]chaining[=<resolveBehavior>[/<continuationBehavior>]]
one of "chainingPreferred", "chainingRequired",
"referralsPreferred", "referralsRequired"
[!]manageDSAit (RFC 3296)
[!]noop
ppolicy
[!]postread[=<attrs>] (RFC 4527; comma-separated attr list)
[!]preread[=<attrs>] (RFC 4527; comma-separated attr list)
[!]relax
[!]sessiontracking
abandon, cancel, ignore (SIGINT sends abandon/cancel,
or ignores response; if critical, doesn't wait for SIGINT.
not really controls)
-h host LDAP server
-H URI LDAP Uniform Resource Identifier(s)
-I use SASL Interactive mode
-n show what would be done but don't actually do it
-N do not use reverse DNS to canonicalize SASL host name
-O props SASL security properties
-o <opt>[=<optparam>] general options
nettimeout=<timeout> (in seconds, or "none" or "max")
ldif-wrap=<width> (in columns, or "no" for no wrapping)
-p port port on LDAP server
-Q use SASL Quiet mode
-R realm SASL realm
-U authcid SASL authentication identity
-v run in verbose mode (diagnostics to standard output)
-V print version info (-VV only)
-w passwd bind password (for simple authentication)
-W prompt for bind password
-x Simple authentication
-X authzid SASL authorization identity ("dn:<dn>" or "u:<user>")
-y file Read password from file
-Y mech SASL mechanism
-Z Start TLS request (-ZZ to require successful response)
命令如下:
查询结果如下:
查询所有用户:
LDAP名词解释
Objectclass
LDAP对象类,是LDAP内置的数据模型。每种objectClass有自己的数据结构,比如我们有一种叫“电话薄”的objectClass,肯定会内置很多属性(attributes),如姓名(uid),身份证号(uidNumber),单位名称(gid),家庭地址(homeDirectory)等,同时,还有一种叫“同学录”的objectClass,具备“电话薄”里的一些attributes(如uid、homeDirectory),还会具有“电话薄”没有的attributes(如description等)。
Entry
entry可以被称为条目,一个entry就是一条记录,是LDAP中一个基本的存储单元;也可以被看作是一个DN和一组属性的集合。注意,一条entry可以包含多个objectClass,例如zhang3可以存在于“电话薄”中,也可以同时存在于“同学录”中。
DN
Distinguished Name,LDAP中entry的唯一辨别名,一条完整的DN写法:uid=zhang3,ou=People,dc=163,dc=com。LDAP中的entry只有DN是由LDAP Server来保证唯一的。
LDAP Search filter
使用filter对LDAP进行搜索。 Filter一般由 (attribute=value) 这样的单元组成,比如:(&(uid=ZHANGSAN)(objectclass=person)) 表示搜索用户中,uid为ZHANGSAN的LDAP Entry.再比如:(&(|(uid= ZHANGSAN)(uid=LISI))(objectclass=person)),表示搜索uid为ZHANGSAN, 或者LISI的用户;也可以使用*来表示任意一个值,比如(uid=ZHANG*SAN),搜索uid值以 ZHANG开头SAN结尾的Entry。更进一步,根据不同的LDAP属性匹配规则,可以有如下的Filter: (&(createtimestamp>=20050301000000)(createtimestamp<=20050302000000)),表示搜索创建时间在20050301000000和20050302000000之间的entry。
Filter中 “&” 表示“与”;“!”表示“非”;“|”表示“或”。根据不同的匹配规则,我们可以使用“=”,“~=”,“>=”以及“<=”。
Base DN
一条Base DN可以是“dc=163,dc=com”,也可以是“dc=People,dc=163,dc=com”。执行LDAP Search时一般要指定basedn,由于LDAP是树状数据结构,指定basedn后,搜索将从BaseDN开始,我们可以指定Search Scope为:只搜索basedn(base),basedn直接下级(one level),和basedn全部下级(sub tree level)。
objectClass 介绍
LDAP中,一个条目必须包含一个objectClass属性,且需要赋予至少一个值。每一个值将用作一条LDAP条目进行数据存储的模板;模板中包含了一个条目必须被赋值的属性和可选的属性。objectClass有着严格的等级之分,最顶层是top和alias。例如,organizationalPerson这个objectClass就隶属于person,而person又隶属于top。
objectClass可分为以下3类:
结构型(Structural):如person和organizationUnit;
辅助型(Auxiliary):如extensibeObject;
抽象型(Abstract):如top,抽象型的objectClass不能直接使用。
在OpenLDAP的schema中定义了很多objectClass,下面列出部分常用的objectClass的名称。
- account
- alias
- dcobject
- domain
- ipHost
- organization
- organizationalRole
- organizationalUnit
- person
- organizationalPerson
- inetOrgPerson
- residentialPerson
- posixAccount
- posixGroup
对象类(ObjectClass)是属性的集合,LDAP预想了很多人员组织机构中常见的对象,并将其封装成对象类。比如人员(person)含有姓(sn)、名(cn)、电话(telephoneNumber)、密码(userPassword)等属性,单位职工(organizationalPerson)是人员(person)的继承类,除了上述属性之外还含有职务(title)、邮政编码(postalCode)、通信地址(postalAddress)等属性。
通过对象类可以方便的定义条目类型。每个条目可以直接继承多个对象类,这样就继承了各种属性。如果2个对象类中有相同的属性,则条目继承后只会保留1个属性。对象类同时也规定了那些属性是基本信息,必须含有(Must 活Required,必要属性):哪些属性是扩展信息,可以含有(May或Optional,可选属性)。
对象类有三种类型:结构类型(Structural)、抽象类型(Abstract)和辅助类型(Auxiliary)。结构类型是最基本的类型,它规定了对象尸体的基本属性,每个条目属于且仅属于一个结构型对象类。抽象类型可以是结构类型或其他抽象类型父类,它公国将对象属性中共性的部分组织在一起,称为其他类的模板,条目不能直接集成抽象型对象类。辅助类型规定了对象实体的扩展属性。虽然每个条米只属于一个结构型对象类,但可以同时属于多个辅助型对象类。
对象类本身是可以相互继承的,所以对象类的根类是top抽象型对象类。以常用的人员类型为例,他们的继承关系如图:
accout内置的attributes有:userid、description、host、localityName、organizationName、organizationalUnitName、seeAlso;
inetOrgPerson内置的attributes有cn、sn、description、seeAlso、telephoneNumber、userPassword、destinationIndicator、facsimileTelephoneNumber、internationaliSDNNumber、l、ou、physicalDeliveryOfficeName、postOfficeBox、postalAddress、postalCode、preferredDeliveryMethod、registeredAddress、st、street、telephoneNumber、teletexTerminalIdentifier、telexNumber、title、x121Address、audio、usinessCategory、carLicense、departmentNumber、isplayName、employeeNumber、employeeType、givenName、homePhone、homePostalAddress、initials、jpegPhoto、labeledURI、mail、manager、mobile、o、pager、photo、preferredLanguage、roomNumber、secretary、uid、userCertificate等;
由上可见,accout仅仅预置了几个必要且实用的属性(完成登陆验证肯定是够了),而inetOrgPerson内置了非常之多的属性,例如电话号码、手机号码、街道地址、邮箱号码,邮箱地址,房间号码,头像,经理,雇员号码等等。
因此,在配置LDAP时,如果仅仅是基于验证登陆的目的,建议将objectClass类型设置为accout,而如果希望打造一个大而全的员工信息宝库,建议将objectClass设置为inetOrgPerson
这里我常用的是'inetOrgPerson', 'posixAccount', 'shadowAccount'。
account的必要属性是userid,而posixAccount的必要属性是cn、gidNumber、homeDirectory、uid、uidNumber;shadowAccount的必要属性是uid,可选属性有shadowExpire、shadowInactive、shadowMax、shadowMin、userPassword等;top必要属性是objectClass(可见,top和其它objectClass是继承的关系)。
Attribute
属性(Attribute)类似于程序设计中的变量,可以被赋值。在OpenLDAP中声明了许多常用的Attribute(用户也可自己定义Attribute)。常见的Attribute含义如下:
- c:国家。
- cn:common name,指一个对象的名字。如果指人,需要使用其全名。
- dc:domain Component,常用来指一个域名的一部分。
- givenName:指一个人的名字,不能用来指姓。
- l:指一个地名,如一个城市或者其他地理区域的名字。
- mail:电子信箱地址。
- o:organizationName,指一个组织的名字。
- ou:organizationalUnitName,指一个组织单元的名字。
- sn:surname,指一个人的姓。
- telephoneNumber:电话号码,应该带有所在的国家的代码。
提示:objectClass是一种特殊的Attribute,它包含其他用到的Attribute以及其自身。
对于不同的objectClass,通常具有一些必设属性值和一些可选属性值。例如,可使用person这个objectClass来表示系统中一个用户的条目,对于系统中用户通常需要有这样一些信息:姓名、电话、密码、描述等。如下图所示,对于person,通过cn和sn设置用户的名和姓,这是必须设置的,而其他属性则是可选的。
下面列出部分常用objectClass要求必设的属性。
- account:userid。
- organization:o。
- person:cn和sn。
- organizationalPerson:与person相同。
- organizationalRole:cn。
- organizationUnit:ou。
- posixGroup:cn、gidNumber。
- posixAccount:cn、gidNumber、homeDirectory、uid、uidNumber。
(完)
|
上一篇:OpenLDAP:Failed to start OpenLDAP Server Daemon.下一篇:Angular 9 系列(九)#id 选择器在页面应用
|