安裝:
yum install openldap openldap-servers openldap-clients
拷貝數據庫配置文件
cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
chown ldap:ldap /var/lib/ldap/DB_CONFIG
DB_CONIFG中主要是關于Berkeley DB的相關的一些配置
啟動OpenLDAP Server:
systemctl start slapd
systemctl enable slapd
systemctl status slapd
slapd即standard alone ldap daemon,該進程默認監聽389端口
設置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中數據交換的一種文件格式。文件內容采用的是key-value形式,注意value后面不能有空格。
- 上面內容中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.ldif
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
modifying entry "olcDatabase={0}config,cn=config"
導入schema
導入schema,schema包含為了支持特殊場景相關的屬性,可根據選擇導入,這里先全部導入:
ls /etc/openldap/schema/*.ldif | while read f; do ldapadd -Y EXTERNAL -H ldapi:/// -f $f; done
設定默認域
先使用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
- olcAccess即access,該key用于指定目錄的ACL即誰有什么權限可以存取什么
- olcRootDN設定管理員root用戶的distingush name
- 注意替換上面文件內容中cn為具體的域信息
- olcRootPW用上面新生成的密碼替換
寫入:
ldapmodify -Y EXTERNAL -H ldapi:/// -f domain.ldif
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
modifying 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"
添加基本目錄
新建一個basedomain.ldif的文件:
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
- 注意替換上面文件內容中dn為具體的域信息
- 理解dn,cn,dc
- DC即Domain Component,LDAP目錄類似文件系統目錄dc=zhidaoauto,dc=com相當于/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
寫入:
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"
測試:
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
管理工具
Docker run -d --privileged -p 10004:80 --name myphpldapadmin --env PHPLDAPADMIN_HTTPS=false --env PHPLDAPADMIN_LDAP_HOSTS="192.168.10.240" --detach osixia/phpldapadmin