1. 安裝 openldap 套件
# yum install -y openldap-servers openldap-clients openldap-devel
2. 複製設定檔到對應的目錄
# cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
# cp /usr/share/openldap-servers/slapd.conf.obsolete /etc/openldap/slapd.conf
3. 產生 LDAP 管理者 root 的密碼,並記下來等會兒設定至 slapd.conf
# slappasswd
New password: (輸入密碼)Re-enter new password: (輸入密碼)
{SSHA} UtMBTNpmr8QuRHyDBEQEBljDWwet3iv9
4. 修改主要 Config 檔 slapd.conf (以下是我的範例,僅供參考)
# vi /etc/openldap/slapd.conf
#
# See slapd.conf(5) for details on configuration options.
# This file should NOT be world readable.
#
include /etc/openldap/schema/corba.schema
include /etc/openldap/schema/core.schema
include /etc/openldap/schema/cosine.schema
include /etc/openldap/schema/duaconf.schema
include /etc/openldap/schema/dyngroup.schema
include /etc/openldap/schema/inetorgperson.schema
include /etc/openldap/schema/java.schema
include /etc/openldap/schema/misc.schema
include /etc/openldap/schema/nis.schema
include /etc/openldap/schema/openldap.schema
include /etc/openldap/schema/ppolicy.schema
include /etc/openldap/schema/collective.schema
# Allow LDAPv2 client connections. This is NOT the default.
allow bind_v2
# Do not enable referrals until AFTER you have a working directory
# service AND an understanding of referrals.
#referral ldap://root.openldap.org
pidfile /var/run/openldap/slapd.pid
argsfile /var/run/openldap/slapd.args
loglevel 256
logfile /var/log/slapd/ldap.log
# Load dynamic backend modules
# – modulepath is architecture dependent value (32/64-bit system)
# – back_sql.la overlay requires openldap-server-sql package
# – dyngroup.la and dynlist.la cannot be used at the same time
# modulepath /usr/lib/openldap
# modulepath /usr/lib64/openldap
# moduleload accesslog.la
# moduleload auditlog.la
# moduleload back_sql.la
# moduleload chain.la
# moduleload collect.la
# moduleload constraint.la
# moduleload dds.la
# moduleload deref.la
# moduleload dyngroup.la
# moduleload dynlist.la
# moduleload memberof.la
# moduleload pbind.la
# moduleload pcache.la
# moduleload ppolicy.la
# moduleload refint.la
# moduleload retcode.la
# moduleload rwm.la
# moduleload seqmod.la
# moduleload smbk5pwd.la
# moduleload sssvlv.la
# moduleload syncprov.la
# moduleload translucent.la
# moduleload unique.la
# moduleload valsort.la
# The next three lines allow use of TLS for encrypting connections using a
# dummy test certificate which you can generate by running
# /usr/libexec/openldap/generate-server-cert.sh. Your client software may balk
# at self-signed certificates, however.
TLSCACertificatePath /etc/openldap/certs
TLSCertificateFile “”OpenLDAP Server””
TLSCertificateKeyFile /etc/openldap/certs/password
# Sample security restrictions
# Require integrity protection (prevent hijacking)
# Require 112-bit (3DES or better) encryption for updates
# Require 63-bit encryption for simple bind
# security ssf=1 update_ssf=112 simple_bind=64
# Sample access control policy:
# Root DSE: allow anyone to read it
# Subschema (sub)entry DSE: allow anyone to read it
# Other DSEs:
# Allow self write access
# Allow authenticated users read access
# Allow anonymous users to authenticate
# Directives needed to implement policy:
# access to dn.base=”” by * read
# access to dn.base=”cn=Subschema” by * read
# access to *
# by self write
# by users read
# by anonymous auth
#
# if no access controls are present, the default policy
# allows anyone and everyone to read anything but restricts
# updates to rootdn. (e.g., “access to * by * read”)
#
# rootdn can always read and write EVERYTHING!
# enable on-the-fly configuration (cn=config)
database config
access to *
by dn.exact=”gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth” manage
by * none
# enable server status monitoring (cn=monitor)
database monitor
access to *
by dn.exact=”gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth” read
by dn.exact=”cn=root,dc=ldap,dc=bojack,dc=com,dc=tw” read
by * none
access to attrs=userPassword
by self write
by anonymous auth
by dn.base=”cn=root,dc=ldap,dc=bojack,dc=com,dc=tw” write
by * none
access to *
by self write
by users read
by dn.base=”cn=root,dc=ldap,dc=bojack,dc=com,dc=tw” write
by * none
#######################################################################
# database definitions
#######################################################################
database bdb
suffix “dc=ldap,dc=bojack,dc=com,dc=tw”
checkpoint 1024 15
rootdn “cn=root,dc=ldap,dc=bojack,dc=com,dc=tw”
# Cleartext passwords, especially for the rootdn, should
# be avoided. See slappasswd(8) and slapd.conf(5) for details.
# Use of strong authentication encouraged.
# rootpw secret
# rootpw {crypt}ijFYNcSNctBYg
rootpw {SSHA}vgn3Ra9Qp0o4BpoYdbvBwLw9eYzNvZJS
# The database directory MUST exist prior to running slapd AND
# should only be accessible by the slapd and slap tools.
# Mode 700 recommended.
directory /var/lib/ldap
# Indices to maintain for this database
index objectClass eq,pres
index ou,cn,mail,surname,givenname eq,pres,sub
index uidNumber,gidNumber,loginShell eq,pres
index uid,memberUid eq,pres,sub
index nisMapName,nisMapEntry eq,pres,sub
# Replicas of this database
#replogfile /var/lib/ldap/openldap-master-replog
#replica host=ldap-1.example.com:389 starttls=critical
# bindmethod=sasl saslmech=GSSAPI
# authcId=host/ldap-master.example.com@EXAMPLE.COM
5. 設定將 LDAP 的 log 寫入 /var/log/slapd
# vi /etc/rsyslog.conf
加入這一行
local4.* /var/log/slapd
6. 定義並新增 base.ldif 來定義整個樹狀結構
# cd /etc/openldap/
# mkdir ldif
# cd ldif
# vi base.ldif
我的 base.ldif 檔案內容如下,原則上就是只有一個員工 ou
# Bojack LDAP Base DN
dn: dc=ldap,dc=bojack,dc=com,dc=tw
objectClass: dcObject
objectClass: organization
dc: ldap
o: BOJACK-LDAP
# Magager ldap.bojack.com.tw Root DN
dn: cn=root,dc=ldap,dc=bojack,dc=com,dc=tw
objectClass: organizationalRole
cn: root
dn: ou=employee,dc=ldap,dc=bojack,dc=com,dc=tw
ou: employee
objectClass: organizationalUnit
description: employee
刪除原有的資料後再新增進去
# rm -rf /etc/openldap/slapd.d/*
# slapadd -v -l /etc/openldap/ldif/base.ldif
The first database does not allow slapadd; using the first available one (2)
added: “dc=ldap,dc=bojack,dc=com,dc=tw” (00000001)
added: “cn=root,dc=ldap,dc=bojack,dc=com,dc=tw” (00000002)
added: “ou=employee,dc=ldap,dc=bojack,dc=com,dc=tw” (00000003)
_#################### 100.00% eta none elapsed none fast!
Closing DB…
將 LDAP 新的資料庫內容產生檔案到 slapd.d 的目錄內並改變擁有權
# slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d
config file testing succeeded
最後來改變權限
# chown -R ldap:ldap /var/lib/ldap
# chown -R ldap:ldap /etc/openldap/slapd.d
7. 設定開機自動啟動 LDAP
# service slapd start
# chkconfig –list slapd
# chkconfig –level 345 slapd on
8. 利用 ldapsearch 驗證一下資料是否已匯入
# ldapsearch -x -b “dc=ldap,dc=bojack,dc=com,dc=tw”
# extended LDIF
#
# LDAPv3
# base <dc=ldap,dc=bojack,dc=com,dc=tw> with scope subtree
# filter: (objectclass=*)
# requesting: ALL
#
# ldap.bojack.com.tw
dn: dc=ldap,dc=bojack,dc=com,dc=tw
objectClass: dcObject
objectClass: organization
dc: ldap
o: BOJACK-LDAP
# root, ldap.bojack.com.tw
dn: cn=root,dc=ldap,dc=bojack,dc=com,dc=tw
objectClass: organizationalRole
cn: root
# employee, ldap.bojack.com.tw
dn: ou=employee,dc=ldap,dc=bojack,dc=com,dc=tw
ou: employee
objectClass: organizationalUnit
description: employee
# search result
search: 2
result: 0 Success
# numResponses: 4
# numEntries: 3
9. 如果要重新匯入請記得清除舊有全部資料
# service slapd stop
# rm -rf /var/lib/ldap/*
# cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
# rm -rf /etc/openldap/slapd.d/*
# slapadd -v -l /etc/openldap/ldif/base.ldif
# slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d
# chown -R ldap:ldap /etc/openldap/slapd.d
# chown -R ldap:ldap /var/lib/ldap
# service slapd start
以上的步骤已经是最简步骤,每一步都不能缺。
需要注意的是,slapd.conf只做初始设置,当执行slaptest后,它就没作用了。slaptest命令会生成slapd.d目录下文件,和/var/lib/ldap/目录下的alock,__db.001~__db.006,slapadd生成/var/lib/ldap目录下的其它文件(也可用ldapadd来导入)。因此,如果做重大修改,最好删除slapd.d和/var/lib/ldap目录,从新来。
修改root密码有些麻烦
先查询:ldapsearch -LLL -Y EXTERNAL -H ldapi:/// -b cn=config olcRootDN=cn=root,dc=example,dc=com dn olcRootDN olcRootPW
显示如下
- SASL/EXTERNAL authentication started
- SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
- SASL SSF: 0
- dn: olcDatabase={2}bdb,cn=config
- olcRootDN: cn=root,dc=example,dc=com
- olcRootPW: secret
然后修改,输入ldapmodify -Y EXTERNAL -H ldapi:///
显示如下
- SASL/EXTERNAL authentication started
- SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
- SASL SSF: 0
然后输入要修改的内容
- dn: olcDatabase={2}bdb,cn=config
- replace: olcRootPW
- olcRootPW: {SSHA}3KRWUN3c85RKTULNnJifSEHH9BHdRv4y
按Ctrl+D结束输入,显示modifying entry “olcDatabase={2}bdb,cn=config”,表明修改成功。从启slapd。
转载请注明:爱开源 » 在centos6.4下使用openldap