当前位置:   article > 正文

centos8 OPEN LDAP部署_centos8部署ldap

centos8部署ldap

英文安装文档 比较清晰,不过为了以防万一还是记录一下。

1、安装 openldap openldap-servers

  1. [root@yl08 tools]# yum install openldap openldap-servers -y
  2. Loaded plugins: fastestmirror
  3. Loading mirror speeds from cached hostfile
  4. * base: mirrors.bupt.edu.cn
  5. * extras: mirrors.bupt.edu.cn
  6. * updates: mirrors.bupt.edu.cn
  7. Package openldap-2.4.44-25.el7_9.x86_64 already installed and latest version
  8. Resolving Dependencies
  9. --> Running transaction check
  10. ---> Package openldap-servers.x86_64 0:2.4.44-25.el7_9 will be installed
  11. --> Processing Dependency: libltdl.so.7()(64bit) for package: openldap-servers-2.4.44-25.el7_9.x86_64
  12. --> Running transaction check
  13. ---> Package libtool-ltdl.x86_64 0:2.4.2-22.el7_3 will be installed
  14. --> Finished Dependency Resolution
  15. Dependencies Resolved

2、安装OpenLDAP Client

  1. [root@yl08 tools]# yum install openldap-clients -y
  2. Loaded plugins: fastestmirror
  3. Loading mirror speeds from cached hostfile
  4. * base: mirrors.bupt.edu.cn
  5. * extras: mirrors.bupt.edu.cn
  6. * updates: mirrors.bupt.edu.cn
  7. Resolving Dependencies
  8. --> Running transaction check
  9. ---> Package openldap-clients.x86_64 0:2.4.44-25.el7_9 will be installed
  10. --> Finished Dependency Resolution

3、启动ldap服务,需要注意的是Ldap服务的名称是:slapd 

  1. [root@yl08 tools]# systemctl enable slapd
  2. [root@yl08 tools]# systemctl start slapd
  3. [root@yl08 tools]# systemctl status slapd
  4. ● slapd.service - OpenLDAP Server Daemon
  5. Loaded: loaded (/usr/lib/systemd/system/slapd.service; disabled; vendor preset: disabled)
  6. Active: active (running) since Fri 2022-09-30 11:31:17 HKT; 5s ago
  7. Docs: man:slapd
  8. man:slapd-config
  9. man:slapd-hdb
  10. man:slapd-mdb
  11. file:///usr/share/doc/openldap-servers/guide.html
  12. Process: 9049 ExecStart=/usr/sbin/slapd -u ldap -h ${SLAPD_URLS} $SLAPD_OPTIONS (code=exited, status=0/SUCCESS)
  13. Process: 9034 ExecStartPre=/usr/libexec/openldap/check-config.sh (code=exited, status=0/SUCCESS)
  14. Main PID: 9051 (slapd)

4、修改管理员密码

4.1、生成openldap的管理密码,注意只是生成还没有生效

  1. [root@yl08 tools]# slappasswd
  2. New password:
  3. Re-enter new password:
  4. {SSHA}YvElk0m4ZVg2JYYJshXv6cVcYfzC6K8f

   4.2、编写ldif文件(填入上面生成的ssha为olcRootPW密码)

  1. [root@yl08 ldap]# vi a.ldif
  2. dn: olcDatabase={0}config,cn=config
  3. changetype: modify
  4. add: olcRootPW
  5. olcRootPW:{SSHA}YvElk0m4ZVg2JYYJshXv6cVcYfzC6K8f #填入上面生成的ssha

 4.3、使ldif文件生效

这里要特殊说明一下,ldap的所有变更都是通过ldif文件来修改的,不要修改任何OpenLDAP装好的配置文件,可能会有不必要的麻烦

那么如何让他生效呢

  1. [root@yl08 ldap]# ldapadd -Y EXTERNAL -H ldapi:/// -f a.ldif
  2. SASL/EXTERNAL authentication started
  3. SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
  4. SASL SSF: 0
  5. modifying entry "olcDatabase={0}config,cn=config"
  6. ldap_modify: Constraint violation (19)
  7. additional info: <olcRootPW> extra cruft after <password>

其中 -Y 指定用于身份验证的 SASL 机制

-H  指定uri来代替ldap server ,只允许填写protocol/host/port

-f 指定文件中读取修改信息

5、配置LDAP数据库

  1. [root@yl08 ldap]# cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
  2. [root@yl08 ldap]# vi /var/lib/ldap/DB_CONFIG
  3. [root@yl08 ldap]# chown -R ldap:ldap /var/lib/ldap/DB_CONFIG
  4. [root@yl08 ldap]# systemctl restart slapd

导入一些基础的预设scheme:

  1. [root@yl08 ldap]# ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/cosine.ldif
  2. SASL/EXTERNAL authentication started
  3. SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
  4. SASL SSF: 0
  5. adding new entry "cn=cosine,cn=schema,cn=config"
  6. [root@yl08 ldap]# ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/nis.ldif
  7. SASL/EXTERNAL authentication started
  8. SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
  9. SASL SSF: 0
  10. adding new entry "cn=nis,cn=schema,cn=config"
  11. [root@yl08 ldap]# ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/inetorgperson.ldif
  12. SASL/EXTERNAL authentication started
  13. SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
  14. SASL SSF: 0
  15. adding new entry "cn=inetorgperson,cn=schema,cn=config"

6、配置openldap的配置

  1. [root@yl08 ldap]# cat chdomain.ldif
  2. # replace to your own domain name for "dc=***,dc=***" section
  3. # specify the password generated above for "olcRootPW" section
  4. dn: olcDatabase={1}monitor,cn=config
  5. changetype: modify
  6. replace: olcAccess
  7. olcAccess: {0}to * by dn.base="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth"
  8. read by dn.base="cn=service,dc=light,dc=cn" read by * none
  9. dn: olcDatabase={2}hdb,cn=config
  10. changetype: modify
  11. replace: olcSuffix
  12. olcSuffix: dc=light,dc=cn
  13. dn: olcDatabase={2}hdb,cn=config
  14. changetype: modify
  15. replace: olcRootDN
  16. olcRootDN: cn=service,dc=light,dc=cn
  17. #dn: olcDatabase={2}hdb,cn=config
  18. #changetype: modify
  19. #add: olcRootPW
  20. #olcRootPW: {SSHA}YvElk0m4ZVg2JYYJshXv6cVcYfzC6K8f
  21. dn: olcDatabase={2}hdb,cn=config
  22. changetype: modify
  23. #add: olcAccess
  24. replace: olcAccess
  25. olcAccess: {0}to attrs=userPassword,shadowLastChange by
  26. dn="cn=service,dc=light,dc=cn" write by anonymous auth by self write by * none
  27. olcAccess: {1}to dn.base="" by * read
  28. olcAccess: {2}to * by dn="cn=service,dc=light,dc=cn" write by * read

这一段大家是不是有种云里雾里的感觉,大家可以看一下如下这个目录内容

  1. [root@yl08 cn=config]# pwd
  2. /etc/openldap/slapd.d/cn=config
  3. [root@yl08 cn=config]#
  4. [root@yl08 cn=config]# ls
  5. 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,下边是具体内容

然后导入文件即可

  1. [root@yl08 ldap]# ldapmodify -Y EXTERNAL -H ldapi:/// -f chdomain.ldif
  2. SASL/EXTERNAL authentication started
  3. SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
  4. SASL SSF: 0
  5. modifying entry "olcDatabase={1}monitor,cn=config"
  6. modifying entry "olcDatabase={2}hdb,cn=config"
  7. modifying entry "olcDatabase={2}hdb,cn=config"
  8. modifying entry "olcDatabase={2}hdb,cn=config"
  9. modifying entry "olcDatabase={2}hdb,cn=config"


7、创建基础domain条目的ldif(该段本身并不适用,只是创建组织架构)

7.1、编写基础的domain条目的ldif,加组织架构啦

  1. [root@yl08 ldap]# cat basedomain.ldif
  2. # replace to your own domain name for "dc=***,dc=***" section
  3. dn: dc=light,dc=cn
  4. objectClass: top
  5. objectClass: dcObject
  6. objectclass: organization
  7. o: light cn
  8. dc: light
  9. dn: cn=service,dc=light,dc=cn
  10. objectClass: organizationalRole
  11. cn: service
  12. description: Service Account
  13. dn: ou=product,dc=light,dc=cn
  14. objectClass: organizationalUnit
  15. ou: product
  16. dn: ou=server,dc=light,dc=cn
  17. objectClass: organizationalUnit
  18. ou: server
  19. dn: ou=client,dc=light,dc=cn
  20. objectClass: organizationalUnit
  21. ou: client
  22. dn: ou=art,dc=light,dc=cn
  23. objectClass: organizationalUnit
  24. ou: art
  25. dn: ou=qa,dc=light,dc=cn
  26. objectClass: organizationalUnit
  27. 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条目文件

  1. [root@yl08 ldap]# ldapadd -x -D cn=service,dc=light,dc=cn -W -f basedomain.ldif
  2. Enter LDAP Password:
  3. adding new entry "dc=light,dc=cn"
  4. adding new entry "cn=service,dc=light,dc=cn"
  5. adding new entry "ou=product,dc=light,dc=cn"
  6. adding new entry "ou=server,dc=light,dc=cn"
  7. adding new entry "ou=client,dc=light,dc=cn"
  8. adding new entry "ou=art,dc=light,dc=cn"
  9. adding new entry "ou=qa,dc=light,dc=cn"

 这里要输的密码是ldap的数据库密码,也就是第二个密码

7.3、验证是否正常

  1. [root@yl08 ldap]# ldapsearch -x -b "dc=light,dc=cn" -H ldap://127.0.0.1
  2. # extended LDIF
  3. #
  4. # LDAPv3
  5. # base <dc=light,dc=cn> with scope subtree
  6. # filter: (objectclass=*)
  7. # requesting: ALL
  8. #
  9. # light.cn
  10. dn: dc=light,dc=cn
  11. objectClass: top
  12. objectClass: dcObject
  13. objectClass: organization
  14. o: light cn
  15. dc: light
  16. # service, light.cn
  17. dn: cn=service,dc=light,dc=cn
  18. objectClass: organizationalRole
  19. cn: service
  20. description: Service Account
  21. # product, light.cn
  22. dn: ou=product,dc=light,dc=cn
  23. objectClass: organizationalUnit
  24. ou: product
  25. # server, light.cn
  26. dn: ou=server,dc=light,dc=cn
  27. objectClass: organizationalUnit
  28. ou: server
  29. # client, light.cn
  30. dn: ou=client,dc=light,dc=cn
  31. objectClass: organizationalUnit
  32. ou: client
  33. # art, light.cn
  34. dn: ou=art,dc=light,dc=cn
  35. objectClass: organizationalUnit
  36. ou: art
  37. # qa, light.cn
  38. dn: ou=qa,dc=light,dc=cn
  39. objectClass: organizationalUnit
  40. ou: qa
  41. # search result
  42. search: 2
  43. result: 0 Success
  44. # numResponses: 8
  45. # numEntries: 7

当然,并不建议用脚本创建用户,很麻烦,为了便于管理,可以安装一个phpldapadmin

可以参考:hpLDAPadmin安装及配置使用_松龄的博客-CSDN博客_phpldapadmin   

 8、添加memberof

如果没有这个模块,第三方的组权限将会非常恶心,因为你无法用到ldap的组概念,关于第三方的用户将都会是空组。想想吧,没有默认权限的用户跟没有有什么分别。

添加memberof这个模块并不复杂:

8.1、添加memberof模块

  1. [root@yl08 openldap]# cat add_moudle_group.ldif
  2. dn: cn=module,cn=config
  3. cn: module
  4. objectClass: olcModuleList
  5. olcModulePath: /usr/lib64/openldap
  6. dn: cn=module{0},cn=config
  7. changetype: modify
  8. add: olcModuleLoad
  9. olcModuleLoad: memberof.la
  10. dn: olcOverlay=memberof,olcDatabase={2}hdb,cn=config
  11. objectClass: olcConfig
  12. objectClass: olcMemberOf
  13. objectClass: olcOverlayConfig
  14. objectClass: top
  15. olcOverlay: memberof
  16. olcMemberOfDangling: ignore
  17. olcMemberOfRefInt: TRUE
  18. olcMemberOfGroupOC: groupOfUniqueNames
  19. olcMemberOfMemberAD: uniqueMember
  20. olcMemberOfMemberOfAD: memberOf

其中memberof.la一般都会在/usr/lib64/openldap,如果不在,自己搜一下。

8.2 添加refint模块

  1. [root@yl08 openldap]# cat refint1.ldif
  2. dn: cn=module{0},cn=config
  3. add: olcmoduleload
  4. olcmoduleload: refint
  5. [root@yl08 openldap]# cat refint2.ldif
  6. dn: olcOverlay=refint,olcDatabase={2}hdb,cn=config
  7. objectClass: olcConfig
  8. objectClass: olcOverlayConfig
  9. objectClass: olcRefintConfig
  10. objectClass: top
  11. olcOverlay: refint
  12. olcRefintAttribute: memberof uniqueMember manager owner

8.3 执行上面的脚本

  1. [root@yl08 openldap]# ldapadd -Q -Y EXTERNAL -H ldapi:/// -f add_moudle_group.ldif
  2. adding new entry "cn=module,cn=config"
  3. modifying entry "cn=module{0},cn=config"
  4. adding new entry "olcOverlay=memberof,olcDatabase={2}hdb,cn=config"
  5. [root@yl08 openldap]# ldapmodify -Q -Y EXTERNAL -H ldapi:/// -f refint1.ldif
  6. modifying entry "cn=module{0},cn=config"
  7. [root@yl08 openldap]# ldapadd -Q -Y EXTERNAL -H ldapi:/// -f refint2.ldif
  8. adding new entry "olcOverlay=refint,olcDatabase={2}hdb,cn=config"

8.4 添加个组测试一下

  1. [root@yl08 openldap]# cat add_group.ldif
  2. dn: cn=gitlab-users,ou=Groups,dc=light,dc=cn
  3. cn: gitlab-users
  4. uniquemember: uid=xxx,ou=Users,dc=light,dc=cn
  5. objectClass: groupofuniquenames
  6. [root@yl08 openldap]# ldapmodify -a -H ldap://172.16.60.53:389 -D "cn=service,dc=light,dc=cn" -f add_group.ldif
  7. adding new entry "cn=gitlab-users,ou=Groups,dc=light,dc=cn"

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/246599
推荐阅读
相关标签
  

闽ICP备14008679号