第 1 章 使用命令行管理目录条目
您可以使用命令行添加、编辑、重命名和删除 LDAP 条目。
1.1. 为 ldapadd、ldapmodify 和 ldapdelete 工具提供输入
当您添加、更新或删除目录中的条目或属性时,您可以使用工具的交互模式进入 LDAP 数据交换格式(LDIF)语句或将 LDIF 文件传递给它们。
1.1.1. OpenLDAP 客户端工具的交互模式
在交互模式中,ldapadd
、ldapmodify
和 ldapdelete
工具从命令行读取输入。要退出交互模式,请按 Ctrl+D (^D)组合键发送文件结束(EOF)转义序列。
在交互模式中,当您按 Enter 两次或发送 EOF 序列时,实用程序会将语句发送到 LDAP 服务器。
使用互动模式:
在不创建文件的情况下输入 LDAP 数据交换格式(LDIF)语句。
例 1.1. 使用 ldapmodify 互动模式进入 LDIF 语句
以下示例以互动模式运行
ldapmodify
,删除telephoneNumber
属性,并使用cn=
值将 manager 属性添加到manager
_name,ou=people,dc=example,dc=comuid=user,ou=people,dc=example,dc=com
条目。在最后的声明后按 退出交互模式。# ldapmodify -D "cn=Directory Manager" -W -H ldap://server.example.com -x dn: uid=user,ou=people,dc=example,dc=com changetype: modify delete: telephoneNumber - add: manager manager: cn=manager_name,ou=people,dc=example,dc=com modifying entry "uid=user,ou=people,dc=example,dc=com" ^D
要将 LDIF 声明(由另一个命令输出)重定向到服务器:
例 1.2. 使用带有重定向内容的 ldapmodify 互动模式
以下示例将
command_that_outputs_LDIF
命令的输出重定向到ldapmodify
。在重定向的命令退出后,交互模式会自动退出。# command_that_outputs_LDIF | ldapmodify -D "cn=Directory Manager" -W -H ldap://server.example.com -x
其他资源
-
ldif (5)
手册页
1.1.2. OpenLDAP 客户端工具的文件模式
在交互模式中,ldapadd
、ldapmodify
和 ldapdelete
工具从文件中读取 LDAP 数据交换格式(LDIF)语句。使用此模式向服务器发送大量 LDIF 语句。
例 1.3. 将带有 LDIF 声明的文件传递给 ldapmodify
使用 LDIF 语句创建文件。例如,使用以下语句创建
~/example.ldif
文件:dn: uid=user,ou=people,dc=example,dc=com changetype: modify delete: telephoneNumber - add: manager manager: cn=manager_name,ou=people,dc=example,dc=com
本例删除
telephoneNumber
属性,并使用cn=
值将 manager 属性添加到manager
_name,ou=people,dc=example,dc=comuid=user,ou=people,dc=example,dc=com
条目。使用
-f
参数将文件传递给ldapmodify
命令:# ldapmodify -D "cn=Directory Manager" -W -H ldap://server.example.com -x -f ~/example.ldif
其他资源
-
ldif (5)
手册页
1.1.3. OpenLDAP 客户端工具的持续操作模式
默认情况下,如果您向服务器发送多个 LDAP 数据交换格式(LDIF)语句,另一个操作会失败,则进程将停止。但是,在错误发生前处理的条目已被成功添加、修改或删除。
要忽略错误并继续处理批处理中进一步的 LDIF 语句,请将 -c
参数传递给 ldapadd
和 ldapmodify
:
# ldpamodify -c -D "cn=Directory Manager" -W -H ldap://server.example.com -x