0%

通过LDIF文件对OpenLDAP条目进行增删改

ldif文件格式

ldif是The LDAP Data Interchange Format简写,
直译'轻量级目录访问协议数据交换格式'

ldif是存储LDAP配置信息及目录内容的标准文本文件格式.

  • 通常以ldif作为文件后缀
  • #行首代表注释
  • 属性:[空格]值 形式作为键值对
  • 多个条目间以空行分隔
  • 行尾不能有多余的字符

dn 是全局唯一的

1
2
3
4
5
6
dn: <distinguished name>
<attrdesc>: <attrvalue>
<attrdesc>: <attrvalue>
<attrdesc>:: <base64-encoded-value>
<attrdesc>:< <URL>
...

ldif条目示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
dn: uid=peter,ou=People,dc=intra,dc=test,dc=com
objectclass: inetOrgPerson
objectclass: posixAccount
objectclass: shadowAccount
uid: peter
cn: peter wang
sn: wang
userPassword: {SSHA}sVVXFfWEecC2S3/R3IL/DjuFHV+0hsLU
mail: peter@intra.test.com
loginShell: /bin/bash
uidNumber: 20001
gidNumber: 20001
homeDirectory: /home/peter
description: peter description
ou: People

ldif 增删改

基本语法

1
2
dn: <distinguishedname>
changetype: <[modify|add|delete|modrdn]>

注意: 针对dn和rdn的修改分别是moddnmodrdn

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
dn: uid=tom,ou=People,dc=intra,dc=test,dc=com
objectclass: inetOrgPerson
objectclass: posixAccount
objectclass: shadowAccount
uid: tom
cn: tom li
sn: li
userPassword: {SSHA}sVVXFfWEecC2S3/R3IL/DjuFHV+0hsLU
mail: tom@intra.test.com
loginShell: /bin/bash
uidNumber: 20002
gidNumber: 20002
homeDirectory: /home/tom
description: tom description
ou: People

dn: uid=tom,ou=People,dc=intra,dc=test,dc=com
changetype: modify
add: mail
mail: tomli@intra.test.com
mail: tom2@intra.test.com
-
replace: description
description: i am tom description
-
delete: mail
mail: tom@intra.test.com

dn: uid=peter,ou=People,dc=intra,dc=test,dc=com
changetype: moddn
newrdn: uid=Peter
deleteoldrdn: 0

测试

1
2
root@jp-vps:~$ldapadd -x -W -D 'cn=admin,dc=intra,dc=test,dc=com' -f test.ldif
Enter LDAP Password: