CentOS7下搭建OpenLDAP伺服器

LDAP 是一款輕量級目錄訪問協議(Lightweight Directory Access Protocol,LDAP),LDAP輕量目錄訪問協議為用戶管理提供了統一認證服務,解決了長期存在的多套用戶認證系統孤立、繁雜、難以維護的問題。具有簡捷、高效、易用的特性,是用戶認證管理的不二選擇,OpenLDAP屬於開源集中帳號管理架構的實現

下面介紹在CentOS7下搭建OpenLDAP伺服器,環境如下

1)CentOS7 Linux伺服器一台 IP:192.168.31.200 搭建OpenLDAP伺服器,需要部署的LDAP域資訊為walkingcloud.cn

2)CentOS7 Linux伺服器一台 IP:192.168.31.76 作為OpenLDAP的客戶端機器用於驗證

管理賬戶資訊:dn:cn=Manager,dc=walkingcloud,dc=cn

1、 yum方式安裝OpenLDAP服務

yum -y install openldap-servers openldap-clients

2、拷貝資料庫配置配置文件,並啟動服務

cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG

chown ldap. /var/lib/ldap/DB_CONFIG

systemctl start slapd

systemctl enable slapd

3、slappasswd生成OpenLDAP管理員密碼

1)使用slappasswd命令設置OpenLDAP管理員密碼,並記錄下

2)vi chrootpw.ldif

dn: olcDatabase={0}config,cn=config

changetype: modify

add: olcRootPW

olcRootPW: {SSHA}TNFs8yAo1VQ82uMAzK+1o8Q2XRgznBMb

#指定上面slappasswd命令生成的密碼串

3)ldapadd -Y EXTERNAL -H ldapi:/// -f chrootpw.ldif

4、導入基本的模式

ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/cosine.ldif

ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/nis.ldif

ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/inetorgperson.ldif

5、在OpenLDAP DB上配置域資訊

1)slappasswd 生成管理員的密碼

2)vi chdomain.ldif

dn: olcDatabase={1}monitor,cn=config

changetype: modify

replace: olcAccess

olcAccess: {0}to * by dn.base="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth"

read by dn.base="cn=Manager,dc=walkingcloud,dc=cn" read by * none

dn: olcDatabase={2}hdb,cn=config

changetype: modify

replace: olcSuffix

olcSuffix: dc=walkingcloud,dc=cn

dn: olcDatabase={2}hdb,cn=config

changetype: modify

replace: olcRootDN

olcRootDN: cn=Manager,dc=walkingcloud,dc=cn

dn: olcDatabase={2}hdb,cn=config

changetype: modify

add: olcRootPW

olcRootPW: {SSHA}XnSjDW+kj37E6TdwwkUw1P32ezGkkYPr

dn: olcDatabase={2}hdb,cn=config

changetype: modify

add: olcAccess

olcAccess: {0}to attrs=userPassword,shadowLastChange by

dn="cn=Manager,dc=walkingcloud,dc=cn" write by anonymous auth by self write by * none

olcAccess: {1}to dn.base="" by * read

olcAccess: {2}to * by dn="cn=Manager,dc=walkingcloud,dc=cn" write by * read

3)ldapmodify -Y EXTERNAL -H ldapi:/// -f chdomain.ldif

4)vi basedomain.ldif

dn: dc=walkingcloud,dc=cn

objectClass: top

objectClass: dcObject

objectclass: organization

o: Walking Cloud

dc: walkingcloud

dn: cn=Manager,dc=walkingcloud,dc=cn

objectClass: organizationalRole

cn: Manager

description: Directory Manager

dn: ou=People,dc=walkingcloud,dc=cn

objectClass: organizationalUnit

ou: People

dn: ou=Group,dc=walkingcloud,dc=cn

objectClass: organizationalUnit

ou: Group

5)ldapadd -x -D cn=Manager,dc=walkingcloud,dc=cn -W -f basedomain.ldif

這時輸入Manager的密碼

6、添加一個用戶

1)slappasswd

New password:

Re-enter new password:

{SSHA}R8l3FCSfEHN4vrFnTsGc9EOib71HPSN4

2)vi ldapuser.ldif

dn: uid=yuanfan,ou=People,dc=walkingcloud,dc=cn

objectClass: inetOrgPerson

objectClass: posixAccount

objectClass: shadowAccount

cn: yuanfan

sn: Linux

userPassword: {SSHA}R8l3FCSfEHN4vrFnTsGc9EOib71HPSN4

loginShell: /bin/bash

uidNumber: 1000

gidNumber: 1000

homeDirectory: /home/yuanfan

dn: cn=yuanfan,ou=Group,dc=walkingcloud,dc=cn

objectClass: posixGroup

cn: yuanfan

gidNumber: 1000

memberUid: yuanfan

3)ldapadd -x -D cn=Manager,dc=walkingcloud,dc=cn -W -f ldapuser.ldif

輸入管理員的密碼

7、OpenLDAP 客戶端配置

1)yum -y install openldap-clients nss-pam-ldapd

2)authconfig –enableldap –enableldapauth –ldapserver=192.168.31.200 –ldapbasedn="dc=walkingcloud,dc=cn" –enablemkhomedir –update

3)接下來用上面創建的LDAP用戶登錄

輸入openLDAP用戶的密碼,可以正常登錄

8、安裝phpLDAPadmin實現通過Web管理LDAP服務

1)yum install -y httpd

2)mv /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.conf_bak

3) vi /etc/httpd/conf/httpd.conf

95行下面加一行

95 #ServerName www.example.com:80

96 ServerName www.walkingcloud.cn

在151行處改為

AllowOverride All

164行處改為

DirectoryIndex index.html index.php index.cgi

在最下面添加如下兩行

ServerTokens Prod

KeepAlive On

4)

systemctl start httpd

systemctl enable httpd.service

6) 安裝php

yum install php php-mbstring php-pear

systemctl restart httpd

7) yum –enablerepo=epel -y install phpldapadmin

8)vi /etc/phpldapadmin/config.php

397行取消注釋,398行注釋掉

$servers->setValue('login','attr','dn');

//$servers->setValue('login','attr','uid');

9)vi /etc/httpd/conf.d/phpldapadmin.conf

12行處添加訪問的網段資訊,例如192.168.31.0/24

Require ip 192.168.31.0/24

然後重啟httpd服務

10)驗證phpldapadmin是否可以打開

11)用管理員帳號進行登錄

cn=Manager,dc=walkingcloud,dc=cn

可以看到之前添加的一個賬戶yuanfan

9、LDAPadmin客戶端工具

Ldap Admin是用於LDAP目錄管理的免費Windows LDAP客戶端和管理工具。此應用程式使您可以瀏覽,搜索,修改,創建和刪除LDAP伺服器上的對象。它還支援更複雜的操作,例如目錄複製和在遠程伺服器之間移動,並擴展了常用的編輯功能以支援特定的對象類型(例如組和帳戶)。

http://www.ldapadmin.org/