Ubuntu OpenLDAP Server搭建

参考文献:

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
这个里面最关键的就是DNS domain name和password,其他的默认就好了。DNS domain name应该是和/etc/hosts里面的配置一致的。这个配置完以后slapd会自动重启。接下来通过下面的命令对这些配置进行验证:
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
这个就是slapd-config DIT的样子,一般这个都不会有问题的,安装好slapd以后,这个都行的。关键是下面的命令是否执行成功:
ldapsearch -x -LLL -H ldap:/// -b dc=example,dc=com dn

dn: dc=example,dc=com

dn: cn=admin,dc=example,dc=com
这个结果如果出来了,基本上说明suffix(baseDN)的配置是生效的了。后面基本就是添加user和group的事情了。
我当时运行的时候会提示:”no such object“,结果我发现就是在dpkg-reconfigure slapd的时候,DNS domain name我写的是ubuntu.example.com,而正确的应该是example.com。改好后,上面的search结果就出来了。

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)


上面这个步骤很简单,不用解释,就是添加了group和people了。添加好以后,我们通过ldapadmin验证一下就知道结果了。

使用ldapadmin连接管理ldap可以参考:http://www.ldapadmin.org/docs/introduction.html

具体的连接方式可以参考下图,这里选择的version是3,因为ldap默认安装的就是3,细心的人还会记得在安装ldap过程中还会提示我们是否安装ldap2。

Ubuntu OpenLDAP Server搭建_第1张图片

使用这个工具,在People下New一个User即可,然后在SetPassword。

到此为止LDAP部分完成。



你可能感兴趣的:(ldap,server)