* 環(huán)境
系統(tǒng)版本:centos7.4
openldap版本2.4
安裝和配置
安裝并啟動服務(wù)
* 安裝:
```
yum install openldap openldap-servers openldap-clients
```
拷貝數(shù)據(jù)庫配置文件
```
cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
chown ldap:ldap /var/lib/ldap/DB_CONFIG
```
?DB_CONIFG中主要是關(guān)于Berkeley DB的相關(guān)的一些配置
?啟動OpenLDAP Server:
```
systemctl start slapd
systemctl enable slapd
systemctl status slapd
```
slapd即standard alone ldap daemon,該進程默認監(jiān)聽389端口
設(shè)置root用戶密碼
先用一個命令生成一個LDAP管理用戶root密碼:
```
slappasswd
New password:
Re-enter new password:
{SSHA}krOGXDmiCdSXuXocOf10F96LJO5ijdXo? #記住這個,下面會用到
```
新建一個rootpwd.ldif(名稱是自定義的)的文件:
```
vi rootpwd.ldif
dn: olcDatabase={0}config,cn=config
changetype: modify
add: olcRootPW
olcRootPW: {SSHA}krOGXDmiCdSXuXocOf10F96LJO5ijdXo
```
ldif即LDAP Data Interchange Format,是LDAP中數(shù)據(jù)交換的一種文件格式。文件內(nèi)容采用的是key-value形式,注意value后面不能有空格。
上面內(nèi)容中dn即distingush name
olc即Online Configuration,表示寫入LDAP后不需要重啟即可生效
changetype: modify表示修改一個entry,changetype的值可以是add,delete, modify等。
add: olcRootPW表示對這個entry新增了一個olcRootPW的屬性
olcRootPW: {SSHA}krOGXDmiCdSXuXocOf10F96LJO5ijdXo指定了屬性值
下面使用ldapadd命令將上面的rootpwd.ldif文件寫入LDAP:
```
ldapadd -Y EXTERNAL -H ldapi:/// -f rootpwd.ldifSASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0modifying entry "olcDatabase={0}config,cn=config"
```
導(dǎo)入schema
導(dǎo)入schema,schema包含為了支持特殊場景相關(guān)的屬性,可根據(jù)選擇導(dǎo)入,這里先全部導(dǎo)入:
```
ls /etc/openldap/schema/*.ldif | while read f; do ldapadd -Y EXTERNAL -H ldapi:/// -f $f; done
```
設(shè)定默認域
先使用slappasswd生成一個密碼:
```
slappasswd
New password:
Re-enter new password:
{SSHA}OpMcf0c+pEqFLZm3i+YiI2qhId1G/yM3
```
新建一個domain.ldif的文件:
```
vi domain.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=zhidaoauto,dc=com" read by * none
dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcSuffix
olcSuffix: dc=zhidaoauto,dc=com
dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcRootDN
olcRootDN: cn=Manager,dc=zhidaoauto,dc=com
dn: olcDatabase={2}hdb,cn=config
changetype: modify
add: olcRootPW
olcRootPW: {SSHA}OpMcf0c+pEqFLZm3i+YiI2qhId1G/yM3 #替換上面生成的密碼
dn: olcDatabase={2}hdb,cn=config
changetype: modify
add: olcAccess
olcAccess: {0}to attrs=userPassword,shadowLastChange by
? dn="cn=Manager,dc=zhidaoauto,dc=com" write by anonymous auth by self write by * none
olcAccess: {1}to dn.base="" by * read
olcAccess: {2}to * by dn="cn=Manager,dc=zhidaoauto,dc=com" write by * read
need-to-insert-img
```
olcAccess即access,該key用于指定目錄的ACL即誰有什么權(quán)限可以存取什么
olcRootDN設(shè)定管理員root用戶的distingush name
注意替換上面文件內(nèi)容中cn為具體的域信息
olcRootPW用上面新生成的密碼替換
寫入:
```
need-to-insert-img
ldapmodify -Y EXTERNAL -H ldapi:/// -f domain.ldifSASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0modifying entry "olcDatabase={1}monitor,cn=config"modifying entry "olcDatabase={2}hdb,cn=config"modifying entry "olcDatabase={2}hdb,cn=config"modifying entry "olcDatabase={2}hdb,cn=config"modifying entry "olcDatabase={2}hdb,cn=config"
need-to-insert-img
```
添加基本目錄
新建一個basedomain.ldif的文件:
```
need-to-insert-img
dn: dc=zhidaoauto,dc=com
objectClass: top
objectClass: dcObject
objectclass: organization
o: zhidaoauto com
dc: zhidaoauto
dn: cn=Manager,dc=zhidaoauto,dc=com
objectClass: organizationalRole
cn: Manager
description: Directory Manager
dn: ou=People,dc=zhidaoauto,dc=com
objectClass: organizationalUnit
ou: People
dn: ou=Group,dc=zhidaoauto,dc=com
objectClass: organizationalUnit
ou: Group
need-to-insert-img
```
注意替換上面文件內(nèi)容中dn為具體的域信息
理解dn,cn,dc
DC即Domain Component,LDAP目錄類似文件系統(tǒng)目錄dc=zhidaoauto,dc=com相當(dāng)于/com/zhidaoauto
CN即Common Name,CN有可能代表一個用戶名,例如cn=Manager,dc=zhidaoauto,dc=com表示在/com/zhidaoauto域下的管理員用戶Manager
OU即Organizational Unit,例如ou=People,dc=zhidaoauto,dc=com表示在/com/zhidaoauto域下的一個組織單元People
?寫入:
```
need-to-insert-img
ldapadd -x -D cn=Manager,dc=zhidaoauto,dc=com -W -f basedomain.ldif
Enter LDAP Password:
adding new entry "dc=zhidaoauto,dc=com"adding new entry "cn=Manager,dc=zhidaoauto,dc=com"adding new entry "ou=People,dc=zhidaoauto,dc=com"adding new entry "ou=Group,dc=zhidaoauto,dc=com"
need-to-insert-img
```
測試:
```
need-to-insert-img
ldapsearch -LLL -W -x -D "cn=Manager,dc=zhidaoauto,dc=com" -H ldap://localhost -b "dc=zhidaoauto,dc=com"Enter LDAP Password:
dn: dc=zhidaoauto,dc=com
objectClass: top
objectClass: dcObject
objectClass: organization
o: zhidaoauto com
dc: zhidaoauto
dn: cn=Manager,dc=zhidaoauto,dc=com
objectClass: organizationalRole
cn: Manager
description: Directory Manager
dn: ou=People,dc=zhidaoauto,dc=com
objectClass: organizationalUnit
ou: People
dn: ou=Group,dc=zhidaoauto,dc=com
objectClass: organizationalUnit
ou: Group
need-to-insert-img
```
管理工具
可以在局域網(wǎng)內(nèi)的windows電腦上下載ldapadmin作為管理工具
need-to-insert-img
知識點:
need-to-insert-img
登錄賬號為:
登錄ldap的用戶dn為:cn=Manager,dc=zhidaoauto,dc=com
ou=Group和ou=People的區(qū)別:
從ou=Group里創(chuàng)建的是組,從ou=People創(chuàng)建的是用戶
ou=People里的用戶怎么加入到ou=Group下邊的組呢:
右鍵"yunwei"組-->properties
need-to-insert-img
need-to-insert-img
need-to-insert-img
這樣就把ou=People里"zhaijunming"用戶添加到ou=Group里的"yunwei"組了