参考文献:
https://help.ubuntu.com/12.04/serverguide/openldap-server.html
Installation
首先安装下面的软件sudo apt-get install slapd ldap-utils
slapd的suffix或者叫base DN是需要本机的域名来决定的,所以我们需要根据需求改动/etc/hosts。比如,如果你想设置的suffix是dc=example,dc=com,那么你需要按照下面的方式设置/etc/hosts文件
127.0.1.1 hostname.example.com hostname
其中hostname是你的主机名,一般ubuntu os的主机名都是ubuntu。
接下来需要通过下面的命令对slapd进行一下配置:
sudo dpkg-reconfigure slapd
它是个有配置界面的,基本的配置如下:
Questions | Answer |
---|---|
Omit OpenLDAP server configuration | No |
DNS domain name | example.com |
Organization name | org |
Administrator password | secret |
Database backend to use | HDB |
Do you want the database to be purged | no |
Move old database | yes |
Allow LDAPv2 protocol | no |
sudo ldapsearch -Q -LLL -Y EXTERNAL -H ldapi:/// -b cn=config dn dn: cn=config dn: cn=module{0},cn=config dn: cn=schema,cn=config dn: cn={0}core,cn=schema,cn=config dn: cn={1}cosine,cn=schema,cn=config dn: cn={2}nis,cn=schema,cn=config dn: cn={3}inetorgperson,cn=schema,cn=config dn: olcBackend={0}hdb,cn=config dn: olcDatabase={-1}frontend,cn=config dn: olcDatabase={0}config,cn=config dn: olcDatabase={1}hdb,cn=config
ldapsearch -x -LLL -H ldap:/// -b dc=example,dc=com dn dn: dc=example,dc=com dn: cn=admin,dc=example,dc=com
Modifying/Populating your Database
Let's introduce some content to our database. We will add the following:
-
a node called People (to store users)
-
a node called Groups (to store groups)
-
a group called miners
-
a user called john
Create the following LDIF file and call it add_content.ldif:
dn: ou=People,dc=example,dc=com objectClass: organizationalUnit ou: People dn: ou=Groups,dc=example,dc=com objectClass: organizationalUnit ou: Groups dn: cn=miners,ou=Groups,dc=example,dc=com objectClass: posixGroup cn: miners gidNumber: 5000 dn: uid=john,ou=People,dc=example,dc=com objectClass: inetOrgPerson objectClass: posixAccount objectClass: shadowAccount uid: john sn: Doe givenName: John cn: John Doe displayName: John Doe uidNumber: 10000 gidNumber: 5000 userPassword: johnldap gecos: John Doe loginShell: /bin/bash homeDirectory: /home/john
It's important that uid and gid values in your directory do not collide with local values. Use high number ranges, such as starting at 5000. By setting the uid and gid values in ldap high, you also allow for easier control of what can be done with a local user vs a ldap one. More on that later.
Add the content:
ldapadd -x -D cn=admin,dc=example,dc=com -W -f add_content.ldif Enter LDAP Password: ******** adding new entry "ou=People,dc=example,dc=com" adding new entry "ou=Groups,dc=example,dc=com" adding new entry "cn=miners,ou=Groups,dc=example,dc=com" adding new entry "uid=john,ou=People,dc=example,dc=com"
We can check that the information has been correctly added with the ldapsearch utility:
ldapsearch -x -LLL -b dc=example,dc=com 'uid=john' cn gidNumber dn: uid=john,ou=People,dc=example,dc=com cn: John Doe gidNumber: 5000
Explanation of switches:
-
-x: "simple" binding; will not use the default SASL method
-
-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)
使用ldapadmin连接管理ldap可以参考:http://www.ldapadmin.org/docs/introduction.html
具体的连接方式可以参考下图,这里选择的version是3,因为ldap默认安装的就是3,细心的人还会记得在安装ldap过程中还会提示我们是否安装ldap2。
使用这个工具,在People下New一个User即可,然后在SetPassword。
到此为止LDAP部分完成。