OpenLDAP 在 CentOS 7 上的極速搭建教程

這是一篇在 CentOS 7 上快速搭建 OpenLDAP 的文檔,主要目的是留存一份能夠迅速搭建 OpenLDAP 環(huán)境的材料,以備不時(shí)之需。啥也別說(shuō),先點(diǎn)右上角收藏,未來(lái)的你可能會(huì)感謝現(xiàn)在的自己。

依照本文進(jìn)行配置之后,讀者將搭建一臺(tái)單機(jī)、具有用戶和用戶組結(jié)構(gòu)、無(wú)備份、無(wú)證書(shū)加密的 OpenLDAP 實(shí)例。備份和證書(shū)加密的內(nèi)容將放在其他文章中。

1. 為服務(wù)器選定一個(gè) FQDN,并且配置為該服務(wù)器的主機(jī)名

這里,我們將服務(wù)器命名為 ldap.colinlee.fish,并將該 FQDN 連同主機(jī)名配置在 /etc/hosts 中。除非你有什么特殊的原因?qū)е虏荒芘渲?FQDN,否則強(qiáng)烈建議配置:該 FQDN 在為 OpenLDAP 設(shè)置 CA 證書(shū)時(shí)有非常大的作用。

[root@ldap ~]# hostnamectl set-hostname ldap.colinlee.fish

/etc/hosts

127.0.1.1    ldap.colinlee.fish ldap
127.0.0.1    localhost
2. 安裝 OpenLDAP 服務(wù)端,設(shè)置數(shù)據(jù)庫(kù)配置文件,啟動(dòng) OpenLDAP 服務(wù)
[root@ldap ~]# yum -y openldap-servers openldap-clients
[root@ldap ~]# cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
[root@ldap ~]# chown ldap.ldap /var/lib/ldap/DB_CONFIG
[root@ldap ~]# systemctl start slapd
[root@ldap ~]# systemctl enable slapd
3. 設(shè)置 OpenLDAP 的管理員用戶 root 的密碼

首先,使用 slappasswd 命令,為 OpenLDAP 的超級(jí)管理員用戶(root)生成密碼。

[root@ldap ~]# slappasswd
New password:
Re-enter new password:
{SSHA}xxxxxxxxxxxxxxxxxxxxxxxx

將生成的密碼添加至 OpenLDAP 的 ldif 文件中。LDIF 是修改 OpenLDAP 內(nèi)容的標(biāo)準(zhǔn)文本格式。

chrootpw.ldif

dn: olcDatabase={0}config,cn=config
changetype: modify
add: olcRootPW
olcRootPW: {SSHA}xxxxxxxxxxxxxxxxxxxxxxxx

接下來(lái),執(zhí)行編輯好的 chrootpw.ldif 文件。

[root@ldap ~]# ldapadd -Y EXTERNAL -H ldapi:/// -f chrootpw.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"
4. 添加幾個(gè)基礎(chǔ)的 Schema

此處添加的 Schema 主要用于記錄人員信息。如果搭建該 OpenLDAP 的目的為用戶認(rèn)證,一般都會(huì)導(dǎo)入以下 Schema;如果使用 OpenLDAP 有別的用處,請(qǐng)確認(rèn)用處后再考慮導(dǎo)入哪些 Schema。

[root@ldap ~]# ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/cosine.ldif

SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
adding new entry "cn=cosine,cn=schema,cn=config"

[root@ldap ~]# ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/nis.ldif

SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
adding new entry "cn=nis,cn=schema,cn=config"

[root@ldap ~]# ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/inetorgperson.ldif

SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
adding new entry "cn=inetorgperson,cn=schema,cn=config"
5. 在 LDAP 數(shù)據(jù)庫(kù)中設(shè)置根域和數(shù)據(jù)庫(kù)超級(jí)管理員

這里的“根域”可以和這臺(tái)服務(wù)器 FQDN 中的根域不同,此處我們?cè)O(shè)置為 dc=colinlee,dc=fish。

數(shù)據(jù)庫(kù)管理員和上面設(shè)置過(guò)的 OpenLDAP 超級(jí)管理員并非同一管理員,而且這里設(shè)置的管理員目前尚未創(chuàng)建。此處的設(shè)置同樣需要一個(gè)用 slappasswd 命令生成的密碼,為了方便管理,我們使用剛剛生成的密碼,不再重新生成。

創(chuàng)建一個(gè)新的 ldif 文件,設(shè)置以上內(nèi)容。

domain-dbadmin.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=admin,dc=colinlee,dc=fish" read
  by * none

dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcSuffix
olcSuffix: dc=colinlee,dc=fish

dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcRootDN
olcRootDN: cn=admin,dc=colinlee,dc=fish

dn: olcDatabase={2}hdb,cn=config
changetype: modify
add: olcRootPW
olcRootPW: {SSHA}xxxxxxxxxxxxxxxxxxxxxxxx

dn: olcDatabase={2}hdb,cn=config
changetype: modify
add: olcAccess
olcAccess: {0}to attrs=userPassword,shadowLastChange
  by dn="cn=admin,dc=colinlee,dc=fish" write
  by anonymous auth
  by self write
  by * none
olcAccess: {1}to dn.base=""
  by * read
olcAccess: {2}to *
  by dn="cn=admin,dc=colinlee,dc=fish" write
  by * read

然后執(zhí)行該 ldif 文件。

[root@ldap ~]# ldapmodify -Y EXTERNAL -H ldapi:/// -f domain-dbadmin.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"
6. 創(chuàng)建用戶節(jié)點(diǎn)、組節(jié)點(diǎn)和數(shù)據(jù)庫(kù)超級(jí)管理員

同樣創(chuàng)建一個(gè)新的 ldif 文件,并填入節(jié)點(diǎn)信息。

basedomain.ldif

dn: dc=colinlee,dc=fish
objectClass: top
objectClass: dcObject
objectclass: organization
o: Example Inc.
dc: colinlee

dn: ou=user,dc=colinlee,dc=fish
objectClass: organizationalUnit
ou: user

dn: ou=group,dc=colinlee,dc=fish
objectClass: organizationalUnit
ou: group

dn: cn=admin,dc=colinlee,dc=fish
objectClass: organizationalRole
cn: admin
description: Directory Administrator

執(zhí)行該文件使內(nèi)容生效。這里的執(zhí)行方式和前面幾次有所不同,此處使用了數(shù)據(jù)庫(kù)超級(jí)管理員的身份,而且需要輸入之前設(shè)置的密碼。

[root@ldap ~]# ldapadd -x -D cn=admin,dc=colinlee,dc=fish -W -f basedomain.ldif
Enter LDAP Password:
adding new entry "dc=colinlee,dc=fish"

adding new entry "ou=user,dc=colinlee,dc=fish"

adding new entry "ou=group,dc=colinlee,dc=fish"

adding new entry "cn=admin,dc=colinlee,dc=fish"
7. 配置防火墻

如果使用了防火墻,需要開(kāi)啟針對(duì) LDAP 的策略。

[root@ldap ~]# firewall-cmd --add-service=ldap --permanent
success
[root@ldap ~]# firewall-cmd --reload
success

如果不使用防火墻,直接關(guān)閉即可。

[root@ldap ~]# systemctl stop firewalld
[root@ldap ~]# systemctl disable firewalld

參考資料:

https://www.server-world.info/en/note?os=CentOS_7&p=openldap
http://www.itzgeek.com/how-tos/linux/centos-how-tos/step-step-openldap-server-configuration-centos-7-rhel-7.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容