前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CentOS7下OpenLDAP统一认证的主从环境部署记录

CentOS7下OpenLDAP统一认证的主从环境部署记录

作者头像
洗尽了浮华
发布2018-07-03 12:57:53
3.6K1
发布2018-07-03 12:57:53
举报
文章被收录于专栏:散尽浮华散尽浮华

之前总结了OpenLDAP基础知识,以往在centos6.x系统上是通过slapd.conf配置部署OpenLDAP主从环境的,centos7上默认是动态部署的,通过yum安装发现无slapd.conf文件。下面记录下Centos7.4系统下OpenLDAP主从环境配置记录:

1)服务器基本信息

192.168.10.213    openldap-master
192.168.10.214    openldap-slave

关闭两个节点机器的防火墙和selinux(两个节点机器上都要操作)

[root@openldap-master ~]# systemctl stop firewalld.service
[root@openldap-master ~]# systemctl disable firewalld.service
[root@openldap-master ~]# firewall-cmd --state
not running

做下两台节点机器的hosts主机名绑定(两个节点机器上都要操作)

[root@openldap-master ~]# cat /etc/hosts
.......
192.168.10.213    openldap-master
192.168.10.214    openldap-slave

同步系统时间(两个节点机器上都要操作)

[root@openldap-master ~]# ntpdate ntp1.aliyun.com

2)OpenLDAP安装(两个节点机器上都要操作)

[root@openldap-master ~]# vim installOpenldap.sh
#!/bin/bash
echo "install ldap rpm"
# compat-openldap这个包与主从有很大的关系
yum install -y openldap openldap-* compat-openldap migrationtools
systemctl start slapd
systemctl enable slapd
[root@openldap-master ~]# chmod 755 installOpenldap.sh
[root@openldap-master ~]# sh -x installOpenldap.sh

查看安装的ldpa服务

[root@openldap-master ~]# rpm -qa|grep ldap
openldap-servers-2.4.44-13.el7.x86_64
openldap-devel-2.4.44-13.el7.x86_64
openldap-2.4.44-13.el7.x86_64
compat-openldap-2.3.43-5.el7.x86_64
openldap-clients-2.4.44-13.el7.x86_64
openldap-servers-sql-2.4.44-13.el7.x86_64

查看OpenLDAP版本

[root@openldap-master ~]# slapd -VV
@(#) $OpenLDAP: slapd 2.4.44 (Apr 12 2018 19:17:38) $
    mockbuild@x86-01.bsys.centos.org:/builddir/build/BUILD/openldap-2.4.44/openldap-2.4.44/servers/slapd

查看ldap启动状态

[root@openldap-master ~]# systemctl status slapd
● slapd.service - OpenLDAP Server Daemon
   Loaded: loaded (/usr/lib/systemd/system/slapd.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2018-05-17 18:24:39 CST; 18min ago
     Docs: man:slapd
           man:slapd-config
           man:slapd-hdb
           man:slapd-mdb
           file:///usr/share/doc/openldap-servers/guide.html
  Process: 26399 ExecStart=/usr/sbin/slapd -u ldap -h ${SLAPD_URLS} $SLAPD_OPTIONS (code=exited, status=0/SUCCESS)
  Process: 26366 ExecStartPre=/usr/libexec/openldap/check-config.sh (code=exited, status=0/SUCCESS)
 Main PID: 26402 (slapd)
   Memory: 10.4M
   CGroup: /system.slice/slapd.service
           └─26402 /usr/sbin/slapd -u ldap -h ldapi:/// ldap:///

May 17 18:35:19 openldap-master slapd[26402]: conn=1008 fd=11 ACCEPT from IP=192.168.10.214:37622 (IP=0.0.0.0:389)
May 17 18:35:19 openldap-master slapd[26402]: conn=1008 op=0 BIND dn="cn=manager,c=cn" method=128
May 17 18:35:19 openldap-master slapd[26402]: conn=1008 op=0 RESULT tag=97 err=49 text=
May 17 18:35:19 openldap-master slapd[26402]: conn=1008 op=1 UNBIND
May 17 18:35:19 openldap-master slapd[26402]: conn=1008 fd=11 closed
May 17 18:40:19 openldap-master slapd[26402]: conn=1009 fd=11 ACCEPT from IP=192.168.10.214:37624 (IP=0.0.0.0:389)
May 17 18:40:19 openldap-master slapd[26402]: conn=1009 op=0 BIND dn="cn=manager,c=cn" method=128
May 17 18:40:19 openldap-master slapd[26402]: conn=1009 op=0 RESULT tag=97 err=49 text=
May 17 18:40:19 openldap-master slapd[26402]: conn=1009 op=1 UNBIND
May 17 18:40:19 openldap-master slapd[26402]: conn=1009 fd=11 closed

查看openldap默认监听的389端口(centos7最小化安装默认没有netstat命令,需安装)

[root@openldap-master ~]# yum install net-tools -y
[root@openldap-master ~]# netstat -antup| grep 389
tcp        0      0 0.0.0.0:389             0.0.0.0:*               LISTEN      26195/slapd         
tcp6       0      0 :::389                  :::*                    LISTEN      26195/slapd

=============================================================== 温馨提示: 本案例测试时,已关闭了iptables防火墙。如果开启了iptables,则需要开放389端口 [root@openldap-master ~]# firewall-cmd --zone=public --add-port=389/tcp --permanent [root@openldap-master ~]# firewall-cmd --reload ==============================================================

3)配置OpenLDAP数据库(两个节点机器上都要操作)

[root@openldap-master ~]# cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
[root@openldap-master ~]# chown ldap:ldap -R /var/lib/ldap/
[root@openldap-master ~]# chmod 700 -R /var/lib/ldap
[root@openldap-master ~]# ll /var/lib/ldap/
total 19156
-rwx------. 1 ldap ldap     2048 May 17 17:42 alock
-rwx------. 1 ldap ldap  2351104 May 17 17:42 __db.001
-rwx------. 1 ldap ldap 17457152 May 17 17:42 __db.002
-rwx------. 1 ldap ldap  1884160 May 17 17:42 __db.003
-rwx------. 1 ldap ldap      845 May 17 17:49 DB_CONFIG
-rwx------. 1 ldap ldap     8192 Mar 13 18:38 dn2id.bdb
-rwx------. 1 ldap ldap    32768 Mar 13 18:38 id2entry.bdb
-rwx------. 1 ldap ldap 10485760 May 17 17:42 log.0000000001

配置ldap服务 设置OpenLDAP的管理员密码(这里密码为:kevin123456)

[root@openldap-master ~]# slappasswd             #或者使用命令"slappasswd -s kevin123456"
New password: 
Re-enter new password: 
{SSHA}p2PfTOW/rViNOvc2WkB5BHD05/m+LTux

编辑chrootpw.ldif文件

[root@openldap-master ~]# cd /opt/
[root@openldap-master opt]# vim chrootpw.ldif
# specify the password generated above for "olcRootPW" section
dn: olcDatabase={0}config,cn=config
changetype: modify
add: olcRootPW
olcRootPW: {SSHA}p2PfTOW/rViNOvc2WkB5BHD05/m+LTux

导入chrootpw.ldif

[root@openldap-master opt]# 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"

============================================================= 温馨提示: 如果上面的命令出现下面报错: [root@openldap-master opt]# 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" ldap_modify: Inappropriate matching (18) additional info: modify/add: olcRootPW: no equality matching rule

解决办法: 修改modify.ldif中对应选项的"add"为"replace"即可

即: [root@openldap-master opt]# cat chrootpw.ldif # specify the password generated above for "olcRootPW" section dn: olcDatabase={0}config,cn=config changetype: modify replace: olcRootPW olcRootPW: {SSHA}p2PfTOW/rViNOvc2WkB5BHD05/m+LTux

然后再次执行 [root@openldap-master opt]# 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" =============================================================

导入基本模式

[root@openldap-master opt]# vim ldapaddBaseSchema.sh
#!/bin/bash
ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/cosine.ldif
ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/nis.ldif
ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/inetorgperson.ldif

执行上面脚本

[root@openldap-master opt]# chmod 755 ldapaddBaseSchema.sh
[root@openldap-master opt]# sh -x ldapaddBaseSchema.sh
+ 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"

+ 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"

+ 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"

======================================================== 温馨提示: 如果上面的命令出现下面报错: ...... ldap_add: Other (e.g., implementation specific) error (80) additional info: olcAttributeTypes: Duplicate attributeType: "2.16.840.1.113730.3.1.1"

原因是: LDIF已经加载,所以尝试再次加载它们就报错这些信息,忽略这个步骤即可。 =======================================================

接着在ldap服务的DB中设置域名,即编辑chdomain.ldif文件

[root@openldap-master opt]# vim 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=Manager,c=cn" read by * none

dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcSuffix
olcSuffix: c=cn

dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcRootDN
olcRootDN: cn=Manager,c=cn

dn: olcDatabase={2}hdb,cn=config
changetype: modify
add: olcRootPW
olcRootPW: {SSHA}p2PfTOW/rViNOvc2WkB5BHD05/m+LTux

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

导入chdomain.ldif文件

[root@openldap-master opt]# 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"

============================================================= 温馨提示: 如果上面的命令出现下面报错: ....... ldap_modify: Inappropriate matching (18) additional info: modify/add: olcRootPW: no equality matching rule

解决办法:将chdomain.ldif文件中的"add"全部替换成"replace",然后重新执行上面命令即可! ============================================================

导入管理员基础数据

[root@openldap-master opt]# vim rootdn.ldif
dn: c=cn
objectclass: country
c: cn

dn: cn=Manager,c=cn
objectclass: organizationalRole
cn: Manager

执行下面命令,输入上面设置的密码:kevin123456

[root@openldap-master opt]# ldapadd -x -D cn=Manager,c=cn -W -f rootdn.ldif
Enter LDAP Password: 
adding new entry "c=cn"

adding new entry "cn=Manager,c=cn"

开启日志配置 查看OpenLDAP的日志级别,日志主要用于对OpenLDAP排查

[root@openldap-master opt]# slapd -d ?
Installed log subsystems:

    Any                            (-1, 0xffffffff)
    Trace                          (1, 0x1)
    Packets                        (2, 0x2)
    Args                           (4, 0x4)
    Conns                          (8, 0x8)
    BER                            (16, 0x10)
    Filter                         (32, 0x20)
    Config                         (64, 0x40)
    ACL                            (128, 0x80)
    Stats                          (256, 0x100)
    Stats2                         (512, 0x200)
    Shell                          (1024, 0x400)
    Parse                          (2048, 0x800)
    Sync                           (16384, 0x4000)
    None                           (32768, 0x8000)

NOTE: custom log subsystems may be later installed by specific code

编辑logLevel.ldif文件

[root@openldap-master opt]# vim logLevel.ldif
[root@openldap-master opt]# cat logLevel.ldif
dn: cn=config
changetype: modify
replace: olcLogLevel
olcLogLevel: stats

导入logLevel.ldif

[root@openldap-master opt]# ldapmodify -Y EXTERNAL -H ldapi:/// -f logLevel.ldif
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
modifying entry "cn=config"

创建slapd.log文件

[root@openldap-master opt]# touch /var/log/slapd.log
[root@openldap-master opt]# vim /etc/rsyslog.conf +73           #"+73"表示指定位到文件73行
.......
local4.*                                               /var/log/slapd.log

重启系统日志服务与ldap服务

[root@openldap-master opt]# systemctl restart rsyslog
[root@openldap-master opt]# systemctl restart slapd
[root@openldap-master opt]# systemctl status slapd

[root@openldap-master opt]# tail -f /var/log/slapd.log
May 17 18:24:38 openldap-master slapd[26195]: daemon: shutdown requested and initiated.
May 17 18:24:38 openldap-master slapd[26195]: slapd shutdown: waiting for 0 operations/tasks to finish
May 17 18:24:38 openldap-master slapd[26195]: slapd stopped.
May 17 18:24:38 openldap-master slapd[26399]: @(#) $OpenLDAP: slapd 2.4.44 (Apr 12 2018 19:17:38) $#012#011mockbuild@x86-01.bsys.centos.org:/builddir/build/BUILD/openldap-2.4.44/openldap-2.4.44/servers/slapd
May 17 18:24:39 openldap-master slapd[26402]: slapd starting

========================================================== 温馨提示: 还可以通过cn=config配置OpenLDAP日志 [root@openldap-master opt]# cat /etc/openldap/slapd.d/cn\=config.ldif | grep olcLogLevel olcLogLevel: stats [root@openldap-master opt]# cat << EOF | ldapmodify -Y EXTERNAL -H ldapi:/// > dn: cn=config > changetype: modify > add: olcLogLevel > olcLogLevel: 32 > EOF SASL/EXTERNAL authentication started SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth SASL SSF: 0 modifying entry "cn=config" =========================================================

4)OpenLDAP主从模式配置

a)openldap-master主节点配置 在master上启用添加syncprov模块来实现主从复制功能点,通过ldif文件来增加syncprov模块,无需重启ldap server。

[root@openldap-master opt]# vim mod_syncprov.ldif
dn: cn=module,cn=config
objectClass: olcModuleList
cn: module
olcModulePath: /usr/lib64/openldap
olcModuleLoad: syncprov.la

Ldap由于scheam的控制,文件里的属性有严格的控制,错误的或者不存在的属性将无法执行ldif文件,所以ldif文件的字体颜色会帮助我们配置ldif文件(属性:绿色,属性值:红色)

[root@openldap-master opt]# ldapadd -Y EXTERNAL -H ldapi:/// -f mod_syncprov.ldif
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
adding new entry "cn=module,cn=config"

[root@openldap-master opt]# vim syncprov.ldif
dn: olcOverlay=syncprov,olcDatabase={2}hdb,cn=config
objectClass: olcOverlayConfig
objectClass: olcSyncProvConfig
olcOverlay: syncprov
olcSpCheckpoint: 100 10
olcSpSessionLog: 100

[root@openldap-master opt]# ldapadd -Y EXTERNAL -H ldapi:/// -f syncprov.ldif
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
adding new entry "olcOverlay=syncprov,olcDatabase={2}hdb,cn=config"

b)openldap-slave从节点配置 同样,在slave上也需要配置syncrepl,因为syncrepl实现的主从复制是单向的,即master的所有操作都会同步到slave上,slave无法同步到master上,为了避免master与slave上的数据不一致,slave上禁止对ldap信息的增删改操作,只允许查询操作。因为是单向的,故slave需要一些master的认证信息,以便从master同步数据。

[root@openldap-slave opt]# vim syncrepl.ldif
dn: olcDatabase={2}hdb,cn=config
changetype: modify
add: olcSyncRepl
olcSyncRepl: rid=001
  provider=ldap://192.168.10.213:389/
  bindmethod=simple
  binddn="cn=Manager,c=cn"
  credentials=kevin123456          #上面设置的openldap的密码
  searchbase="c=cn"
  scope=sub
  schemachecking=on
  type=refreshAndPersist
  retry="5 5 300 +"
  attrs="*,+"
  interval=00:00:00:10

这里方便测试设置10秒同步

注意:一定要注意字体的颜色和上面一致,provider,bindmethod …… interval都是olcSyncRepl的属性值,不是与olcSyncRepl同级别,故其为红色。

[root@openldap-slave opt]# ldapadd -Y EXTERNAL -H ldapi:/// -f syncrepl.ldif
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
modifying entry "olcDatabase={2}hdb,cn=config"

5)OpenLDAP主从功能测试

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-05-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
日志服务
日志服务(Cloud Log Service,CLS)是腾讯云提供的一站式日志服务平台,提供了从日志采集、日志存储到日志检索,图表分析、监控告警、日志投递等多项服务,协助用户通过日志来解决业务运维、服务监控、日志审计等场景问题。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档