英文安装文档 比较清晰,不过为了以防万一还是记录一下。
1、安装 openldap openldap-servers
[root@yl08 tools]# yum install openldap openldap-servers -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.bupt.edu.cn
* extras: mirrors.bupt.edu.cn
* updates: mirrors.bupt.edu.cn
Package openldap-2.4.44-25.el7_9.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package openldap-servers.x86_64 0:2.4.44-25.el7_9 will be installed
--> Processing Dependency: libltdl.so.7()(64bit) for package: openldap-servers-2.4.44-25.el7_9.x86_64
--> Running transaction check
---> Package libtool-ltdl.x86_64 0:2.4.2-22.el7_3 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
2、安装OpenLDAP Client
[root@yl08 tools]# yum install openldap-clients -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.bupt.edu.cn
* extras: mirrors.bupt.edu.cn
* updates: mirrors.bupt.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package openldap-clients.x86_64 0:2.4.44-25.el7_9 will be installed
--> Finished Dependency Resolution
3、启动ldap服务,需要注意的是Ldap服务的名称是:slapd
[root@yl08 tools]# systemctl enable slapd
[root@yl08 tools]# systemctl start slapd
[root@yl08 tools]# systemctl status slapd
● slapd.service - OpenLDAP Server Daemon
Loaded: loaded (/usr/lib/systemd/system/slapd.service; disabled; vendor preset: disabled)
Active: active (running) since Fri 2022-09-30 11:31:17 HKT; 5s ago
Docs: man:slapd
man:slapd-config
man:slapd-hdb
man:slapd-mdb
file:///usr/share/doc/openldap-servers/guide.html
Process: 9049 ExecStart=/usr/sbin/slapd -u ldap -h ${SLAPD_URLS} $SLAPD_OPTIONS (code=exited, status=0/SUCCESS)
Process: 9034 ExecStartPre=/usr/libexec/openldap/check-config.sh (code=exited, status=0/SUCCESS)
Main PID: 9051 (slapd)
4、修改管理员密码
4.1、生成openldap的管理密码,注意只是生成还没有生效
[root@yl08 tools]# slappasswd
New password:
Re-enter new password:
{SSHA}YvElk0m4ZVg2JYYJshXv6cVcYfzC6K8f
4.2、编写ldif文件(填入上面生成的ssha为olcRootPW密码)
[root@yl08 ldap]# vi a.ldif
dn: olcDatabase={0}config,cn=config
changetype: modify
add: olcRootPW
olcRootPW:{SSHA}YvElk0m4ZVg2JYYJshXv6cVcYfzC6K8f #填入上面生成的ssha
4.3、使ldif文件生效
这里要特殊说明一下,ldap的所有变更都是通过ldif文件来修改的,不要修改任何OpenLDAP装好的配置文件,可能会有不必要的麻烦
那么如何让他生效呢
[root@yl08 ldap]# ldapadd -Y EXTERNAL -H ldapi:/// -f a.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"
ldap_modify: Constraint violation (19)
additional info: <olcRootPW> extra cruft after <password>
其中 -Y 指定用于身份验证的 SASL 机制
-H 指定uri来代替ldap server ,只允许填写protocol/host/port
-f 指定文件中读取修改信息
5、配置LDAP数据库
[root@yl08 ldap]# cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
[root@yl08 ldap]# vi /var/lib/ldap/DB_CONFIG
[root@yl08 ldap]# chown -R ldap:ldap /var/lib/ldap/DB_CONFIG
[root@yl08 ldap]# systemctl restart slapd
导入一些基础的预设scheme:
[root@yl08 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@yl08 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@yl08 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"
6、配置openldap的配置
[root@yl08 ldap]# cat chdomain.ldif
# replace to your own domain name for "dc=***,dc=***" section
# specify the password generated above for "olcRootPW" section
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=service,dc=light,dc=cn" read by * none
dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcSuffix
olcSuffix: dc=light,dc=cn
dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcRootDN
olcRootDN: cn=service,dc=light,dc=cn
#dn: olcDatabase={2}hdb,cn=config
#changetype: modify
#add: olcRootPW
#olcRootPW: {SSHA}YvElk0m4ZVg2JYYJshXv6cVcYfzC6K8f
dn: olcDatabase={2}hdb,cn=config
changetype: modify
#add: olcAccess
replace: olcAccess
olcAccess: {0}to attrs=userPassword,shadowLastChange by
dn="cn=service,dc=light,dc=cn" write by anonymous auth by self write by * none
olcAccess: {1}to dn.base="" by * read
olcAccess: {2}to * by dn="cn=service,dc=light,dc=cn" write by * read
这一段大家是不是有种云里雾里的感觉,大家可以看一下如下这个目录内容
[root@yl08 cn=config]# pwd
/etc/openldap/slapd.d/cn=config
[root@yl08 cn=config]#
[root@yl08 cn=config]# ls
cn=schema cn=schema.ldif olcDatabase={0}config.ldif olcDatabase={-1}frontend.ldif olcDatabase={1}monitor.ldif olcDatabase={2}hdb.ldif
是不是一下子顿悟了,没错,dn就是指的这里面的文件名,changetype:操作类型modify
replace|add 等是添加文件里的key,下边是具体内容
然后导入文件即可
[root@yl08 ldap]# ldapmodify -Y EXTERNAL -H ldapi:/// -f chdomain.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"
7、创建基础domain条目的ldif(该段本身并不适用,只是创建组织架构)
7.1、编写基础的domain条目的ldif,加组织架构啦
[root@yl08 ldap]# cat basedomain.ldif
# replace to your own domain name for "dc=***,dc=***" section
dn: dc=light,dc=cn
objectClass: top
objectClass: dcObject
objectclass: organization
o: light cn
dc: light
dn: cn=service,dc=light,dc=cn
objectClass: organizationalRole
cn: service
description: Service Account
dn: ou=product,dc=light,dc=cn
objectClass: organizationalUnit
ou: product
dn: ou=server,dc=light,dc=cn
objectClass: organizationalUnit
ou: server
dn: ou=client,dc=light,dc=cn
objectClass: organizationalUnit
ou: client
dn: ou=art,dc=light,dc=cn
objectClass: organizationalUnit
ou: art
dn: ou=qa,dc=light,dc=cn
objectClass: organizationalUnit
ou: qa
上图为从其它网站copy过来的ldap目录树,要了解上面这些内容,首先要知道一些ldap的基础设定。
1、entry(条目):上图的所有节点都可称为一个entry
2、dn(Distinguished Name,意为识别名),它表示条目在目录树中从根出发的绝对路径,是条目的唯一标识。可以跟 UNIX 文件系统中文件或目录的完整路径做类比。例如:4.1 的图中右下角的条目的 DN 是 cn=group1,dc=zenandidi,dc=com
。
3、rdn(Relative Distinguished Name, RDN),相对识别名就是识别名第一个逗号左侧的内容。
可以跟 UNIX 文件系统中文件或目录名做类比。例如:4.1 的图中右下角的条目的 RDN 是 cn=group1
。
4、CN=Common Name 为用户名或服务器名,最长可以到80个字符,可以为中文;
5、OU=Organization Unit为组织单元,最多可以有四级,每级最长32个字符,可以为中文;
6、O=Organization 为组织名,可以3—64个字符长
7、C=Country为国家名,可选,为2个字符长
8、DC (Domain Component)
7.2、导入基础的domain条目文件
[root@yl08 ldap]# ldapadd -x -D cn=service,dc=light,dc=cn -W -f basedomain.ldif
Enter LDAP Password:
adding new entry "dc=light,dc=cn"
adding new entry "cn=service,dc=light,dc=cn"
adding new entry "ou=product,dc=light,dc=cn"
adding new entry "ou=server,dc=light,dc=cn"
adding new entry "ou=client,dc=light,dc=cn"
adding new entry "ou=art,dc=light,dc=cn"
adding new entry "ou=qa,dc=light,dc=cn"
这里要输的密码是ldap的数据库密码,也就是第二个密码
7.3、验证是否正常
[root@yl08 ldap]# ldapsearch -x -b "dc=light,dc=cn" -H ldap://127.0.0.1
# extended LDIF
#
# LDAPv3
# base <dc=light,dc=cn> with scope subtree
# filter: (objectclass=*)
# requesting: ALL
#
# light.cn
dn: dc=light,dc=cn
objectClass: top
objectClass: dcObject
objectClass: organization
o: light cn
dc: light
# service, light.cn
dn: cn=service,dc=light,dc=cn
objectClass: organizationalRole
cn: service
description: Service Account
# product, light.cn
dn: ou=product,dc=light,dc=cn
objectClass: organizationalUnit
ou: product
# server, light.cn
dn: ou=server,dc=light,dc=cn
objectClass: organizationalUnit
ou: server
# client, light.cn
dn: ou=client,dc=light,dc=cn
objectClass: organizationalUnit
ou: client
# art, light.cn
dn: ou=art,dc=light,dc=cn
objectClass: organizationalUnit
ou: art
# qa, light.cn
dn: ou=qa,dc=light,dc=cn
objectClass: organizationalUnit
ou: qa
# search result
search: 2
result: 0 Success
# numResponses: 8
# numEntries: 7
当然,并不建议用脚本创建用户,很麻烦,为了便于管理,可以安装一个phpldapadmin
8、添加memberof
如果没有这个模块,第三方的组权限将会非常恶心,因为你无法用到ldap的组概念,关于第三方的用户将都会是空组。想想吧,没有默认权限的用户跟没有有什么分别。
添加memberof这个模块并不复杂:
8.1、添加memberof模块
[root@yl08 openldap]# cat add_moudle_group.ldif
dn: cn=module,cn=config
cn: module
objectClass: olcModuleList
olcModulePath: /usr/lib64/openldap
dn: cn=module{0},cn=config
changetype: modify
add: olcModuleLoad
olcModuleLoad: memberof.la
dn: olcOverlay=memberof,olcDatabase={2}hdb,cn=config
objectClass: olcConfig
objectClass: olcMemberOf
objectClass: olcOverlayConfig
objectClass: top
olcOverlay: memberof
olcMemberOfDangling: ignore
olcMemberOfRefInt: TRUE
olcMemberOfGroupOC: groupOfUniqueNames
olcMemberOfMemberAD: uniqueMember
olcMemberOfMemberOfAD: memberOf
其中memberof.la一般都会在/usr/lib64/openldap,如果不在,自己搜一下。
8.2 添加refint模块
[root@yl08 openldap]# cat refint1.ldif
dn: cn=module{0},cn=config
add: olcmoduleload
olcmoduleload: refint
[root@yl08 openldap]# cat refint2.ldif
dn: olcOverlay=refint,olcDatabase={2}hdb,cn=config
objectClass: olcConfig
objectClass: olcOverlayConfig
objectClass: olcRefintConfig
objectClass: top
olcOverlay: refint
olcRefintAttribute: memberof uniqueMember manager owner
8.3 执行上面的脚本
[root@yl08 openldap]# ldapadd -Q -Y EXTERNAL -H ldapi:/// -f add_moudle_group.ldif
adding new entry "cn=module,cn=config"
modifying entry "cn=module{0},cn=config"
adding new entry "olcOverlay=memberof,olcDatabase={2}hdb,cn=config"
[root@yl08 openldap]# ldapmodify -Q -Y EXTERNAL -H ldapi:/// -f refint1.ldif
modifying entry "cn=module{0},cn=config"
[root@yl08 openldap]# ldapadd -Q -Y EXTERNAL -H ldapi:/// -f refint2.ldif
adding new entry "olcOverlay=refint,olcDatabase={2}hdb,cn=config"
8.4 添加个组测试一下
[root@yl08 openldap]# cat add_group.ldif
dn: cn=gitlab-users,ou=Groups,dc=light,dc=cn
cn: gitlab-users
uniquemember: uid=xxx,ou=Users,dc=light,dc=cn
objectClass: groupofuniquenames
[root@yl08 openldap]# ldapmodify -a -H ldap://172.16.60.53:389 -D "cn=service,dc=light,dc=cn" -f add_group.ldif
adding new entry "cn=gitlab-users,ou=Groups,dc=light,dc=cn"