关于 LDAP
DN(区分名,Distinguished Name)
CN(Common Name)为用户名或服务器名,最长可以到80个字符,可以为中文;
OU(Organization Unit)为组织单元,最多可以有四级,每级最长32个字符,可以为中文;
O(Organization)为组织名,可以3—64个字符长
C(Country)为国家名,可选,为2个字符长
CN, OU, DC 都是 LDAP 连接服务器的端字符串中的区别名称(DN, distinguished name)
LDAP连接服务器的连接字串格式为:ldap://servername/DN
其中DN有三个属性,分别是CN,OU,DC
LDAP是一种通讯协议,如同HTTP是一种协议一样的。
在 LDAP 目录中,
- DC (Domain Component)
- CN (Common Name)
- OU (Organizational Unit)
LDAP 目录类似于文件系统目录。
Each entry has a unique identifier: its Distinguished Name (DN or dn). This, in turn, consists of a Relative Distinguished Name (RDN) followed by the parent entry's DN.
- DN is "cn=John Doe,dc=example,dc=com"
- RDN is "cn=John Doe"
- parent DN is "dc=example,dc=com"
下列目录:
- DC=redmond,DC=wa,DC=microsoft,DC=com
如果我们类比文件系统的话,可被看作如下文件路径:
- ComMicrosoftWaRedmond
例如:CN=test,OU=developer,DC=domainname,DC=com
在上面的代码中 cn=test 可能代表一个用户名,ou=developer 代表一个 active directory 中的组织单位。这句话的含义可能就是说明 test 这个对象处在domainname.com 域的 developer 组织单元中。
最后总结一下LDAP:
- LDAP的结构用树来表示,而不是用表格。正因为这样,就不能用SQL语句了。
- LDAP可以很快地得到查询结果,不过在写方面,就慢得多。
- LDAP提供了静态数据的快速查询方式。
- Client/server模型,Server 用于存储数据,Client提供操作目录信息树的工具。
- LDAP是一种开放Internet标准,LDAP协议是跨平台的Interent协议。
LDIF format (LDAP Data Interchange Format). Any information that you feed into your DIT must also be in such a format.
dn: cn=John Doe,dc=example,dc=com
cn: John Doe
givenName: John
sn: Doe
telephoneNumber: +1 888 555 6789
telephoneNumber: +1 888 555 1232
mail: john@example.com
manager: cn=Larry Smith,dc=example,dc=com
objectClass: inetOrgPerson
objectClass: organizationalPerson
objectClass: person
objectClass: top
安装 OpenLDAP 服务
sudo apt-get install slapd ldap-utils
如果需要重新配置 DN 及密码:
sudo dpkg-reconfigure slapd
验证配置成功
This is what the slapd-config DIT looks like via the LDAP protocol:(这是 slapd-config 的 DIT 通过 LDAP 协议看到的的样子:)
sudo ldapsearch -Q -LLL -Y EXTERNAL -H ldapi:/// -b cn=config dn
This is what the dc=www,dc=hejunlin,dc=cn DIT looks like:(这是我们的 DIT 的样子:)
ubuntu@VM-0-6-ubuntu:/etc/ldap/slapd.d$ ldapsearch -x -LLL -H ldap:/// -b dc=www,dc=hejunlin,dc=cn dn
dn: dc=www,dc=hejunlin,dc=cn
dn: cn=admin,dc=www,dc=hejunlin,dc=cn
增加配置
dn: ou=People,dc=www,dc=hejunlin,dc=cn
objectClass: organizationalUnit
ou: People
dn: ou=Groups,dc=www,dc=hejunlin,dc=cn
objectClass: organizationalUnit
ou: Groups
dn: cn=miners,ou=Groups,dc=www,dc=hejunlin,dc=cn
objectClass: posixGroup
cn: miners
gidNumber: 5000
dn: uid=john,ou=People,dc=www,dc=hejunlin,dc=cn
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount
uid: henry
sn: Doe
givenName: Henry
cn: Henry RO
displayName: Henry RO
uidNumber: 10000
gidNumber: 5000
userPassword: 12345678
gecos: Henry RO
loginShell: /bin/bash
homeDirectory: /home/john
上面创建了一个节点为 People,用于存放用户 user。
一个节点成为 Groups,用于存放用户组 groups。
一个叫 miner 的用户组。
一个名为 Henry 的用户。
在你的目录中的uid和gid的值不与本地值冲突是很重要的。使用比较高的数值范围,比如从5000开始。通过在ldap中设置较高的uid和gid值,允许你更容易地控制本地用户和ldap用户。
将配置写入:
ldapadd -x -D cn=admin,dc=www,dc=hejunlin,dc=cn -W -f add_content.ldif
ubuntu@VM-0-6-ubuntu:~/source$ ldapadd -x -D cn=admin,dc=www,dc=hejunlin,dc=cn -W -f ~/source/add_content.ldif
Enter LDAP Password:
adding new entry "ou=People,dc=www,dc=hejunlin,dc=cn"
adding new entry "ou=Groups,dc=www,dc=hejunlin,dc=cn"
adding new entry "cn=miners,ou=Groups,dc=www,dc=hejunlin,dc=cn"
adding new entry "uid=john,ou=People,dc=www,dc=hejunlin,dc=cn"
我们查询下 Henry 这个用户:
ubuntu@VM-0-6-ubuntu:~/source$ ldapsearch -x -LLL -b dc=www,dc=hejunlin,dc=cn 'uid=henry' cn gidNumber
dn: uid=john,ou=People,dc=www,dc=hejunlin,dc=cn
cn: Henry RO
gidNumber: 5000
Explanation of switches:
- -x: "simple" binding; will not use the default SASL method 不使用默认的 SASL 方式
- -LLL: disable printing extraneous information 不输出额外的信息
- uid=john: a "filter" to find the john user 一个过滤器,在这里用于查找用户
- cn gidNumber: requests certain attributes to be displayed (the default is to show all attributes) 需要返回的额外信息,这里是 gidNumber
参考资料:https://help.ubuntu.com/lts/serverguide/openldap-server.html.en