“在Hadoop集群配置中,通常LDAP用于账号管理,使用Kerberos进行认证,本来是两套独立的系统,两套独立的账户,通过配置kereros使用LDAP作为数据库,配置LDAP账户使用SASL方式进行kerberos认证,能将两套账户进行关联统一,结合CAS等方案,可实现一套账户统一认证。”
1. 环境准备
机器: centos 7,设置hostname:ldap.test.com
下载jxplorer用于访问ldap服务。
2. OpenLDAP安装配置
2.1 安装OpenLDAP
[root@kdc ~]# yum install -y openldap-servers openldap-clients
[root@kdc ~]# slapd -VVV
@(#) $OpenLDAP: slapd 2.4.44 (Jan 29 2019 17:42:45) $
mockbuild@x86-01.bsys.centos.org:/builddir/build/BUILD/openldap-2.4.44/openldap-2.4.44/servers/slapd
Included static backends:
config
ldif
monitor
bdb
hdb
mdb
2.2 数据库配置
rm -rf /var/lib/ldap/*
cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
chown -R ldap.ldap /var/lib/ldap
2.3 主配置
在2.4以前的版本中,OpenLDAP 使用 slapd.conf 配置文件来进行服务器的配置,而2.4开始则使用 slapd.d 目录保存细分后的各种配置,,其数据存储位置即目录 /etc/openldap/slapd.d。尽管该系统的数据文件是透明格式的,还是建议使用 ldapadd, ldapdelete, ldapmodify 等命令来修改而不是直接编辑。
默认配置文件保存在 /etc/openldap/slapd.d,将其备份:
cp -rf /etc/openldap/slapd.d /etc/openldap/slapd.d.bak
添加一些基本配置,引入 openldap 的 schema:
$ vi /etc/openldap/slapd.conf
#!导入schema
include /etc/openldap/schema/core.schema
include /etc/openldap/schema/cosine.schema
include /etc/openldap/schema/duaconf.schema
include /etc/openldap/schema/dyngroup.schema
include /etc/openldap/schema/inetorgperson.schema
include /etc/openldap/schema/java.schema
include /etc/openldap/schema/misc.schema
include /etc/openldap/schema/nis.schema
include /etc/openldap/schema/openldap.schema
include /etc/openldap/schema/ppolicy.schema
#pid文件和args文件路径
pidfile /var/run/openldap/slapd.pid
argsfile /var/run/openldap/slapd.args
#sasl-host localhost
#sasl-secprops none
#日志级别设置
loglevel 296
# 模块设置
modulepath /usr/lib64/openldap
moduleload syncprov.la
#开启sasl设置,默认关闭
#TLSCACertificatePath /etc/openldap/certs
#TLSCertificateFile etc/openldap/certs/cert
#TLSCertificateKeyFile /etc/openldap/certs/password
#设置serverID,采用mirror mode模式进行部署
#serverID 1 ldap://kdc.test.com
#serverID 2 ldap://kdc2.test.com
#数据库权限控制
#数据库通用权限配置,会配置到olcDatabase={-1}frontend.ldif该文件中,访问所有数据库的通用权限
#access to *
# by anonymous auth
# by self write
# by users read
#config数据库配置
database config
#权限设置
access to *
by dn.exact=gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth manage
by * none
#进行配置同步备份
#syncrepl rid=001
# provider=ldap://kylin-203-122
# bindmethod=simple
# binddn="cn=config"
# credentials=1234
# searchbase="cn=config"
# schemachecking=on
# type=refreshAndPersist
# retry="60 +"
#syncrepl rid=002
# provider=ldap://kylin-203-130
# bindmethod=simple
# binddn="cn=config"
# credentials=1234
# searchbase="cn=config"
# schemachecking=on
# type=refreshAndPersist
# retry="60 +"
#设置同步模块
#overlay syncprov
#开启mirrormode设置
#mirrormode on
#监控数据库配置,设置访问监控数据库的权限,开启该模块会对访问openldap服务的相关信息进行监控
database monitor
#设置访问monitor数据库的用户权限
access to *
by dn.base="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth" read
by dn.base="cn=admin,dc=test,dc=com" read
by * none
###################################数据库权限控制#################################
###################################数据库配置#################################
#设置数据库类型为lmdb,官方推荐
database mdb
#进行权限设置
#数据库匹配的前缀
suffix "dc=test,dc=com"
checkpoint 1024 15
#数据库管理员账户
rootdn "cn=admin,dc=test,dc=com"
#数据库管理员密码,使用slappasswd -s 123456命令生成
rootpw {SSHA}Ds8p4kHsufzjt7J+EOi42WJFmv/XYtYj
#数据库存储数据路径
directory /var/lib/ldap/mdb
#数据库存储最大值
maxsize 1048576
#数据库索引设置,索引objectclass、cn、uid
index objectclass,entryCSN,entryUUID eq
#数据库索引设置,索引linux账户
index uid,uidNumber,gidNumber eq,pres
#数据库索引设置,索引kerberos账户,未配置kerberos可省略
#index ou,krbPrincipalName eq,pres,sub
#设置同步模块
#overlay syncprov
#syncprov-checkpoint 100 10
#syncprov-sessionlog 100
#mirror mode相关设置
#rid:保证每台服务器的rid是一样的
#provider:指向另外一台服务的ldap地址
#bindmethod:制定简单的鉴权模式,表示未开启sasl或者ssl模式
#binddn:设置进行同步的账户,默认等同于数据库账户
#credentials:设置同步账户的密码,默认等同于数据库账户
#searchbase:设置同步的根路径
#schemachecking:采用refreshAndPersist
#retry:重试次数,如果同步失败,每隔60s同步一次
#syncrepl rid=101
# provider=ldap://kylin-203-122
# bindmethod=simple
# binddn="cn=admin,dc=bigdata,dc=ly"
# credentials=1234
# searchbase="dc=bigdata,dc=ly"
# schemachecking=on
# type=refreshAndPersist
# retry="60 +"
#syncrepl rid=102
# provider=ldap://kylin-203-130
# bindmethod=simple
# binddn="cn=admin,dc=bigdata,dc=ly"
# credentials=1234
# searchbase="dc=bigdata,dc=ly"
# schemachecking=on
# type=refreshAndPersist
# retry="60 +"
#开启mirror mode模式
#mirrormode on
根据slapd.conf使用slaptest命令生成ldif格式的配置文件,更新slapd.d:
$ slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d
$ chown -R ldap:ldap /etc/openldap/slapd.d && chmod -R 700 /etc/openldap/slapd.d
$ systemctl start slapd
说明:slaptest报错,不用管
2.4 组织结构配置
- 添加组织结构(base.ldif)
dn: dc=test,dc=com
dc: test
objectClass: dcObject
objectClass: organizationalUnit
ou: test.com
dn: ou=accounts,dc=test,dc=com
ou: accounts
objectClass: organizationalUnit
dn: ou=services,ou=accounts,dc=test,dc=com
ou: services
objectClass: organizationalUnit
dn: ou=users,ou=accounts,dc=test,dc=com
ou: users
objectClass: organizationalUnit
dn: ou=groups,ou=accounts,dc=test,dc=com
ou: groups
objectClass: organizationalUnit
#kerberos subtree
dn: cn=kerberos,dc=test,dc=com
cn: kerberos
objectClass: krbContainer
使用ldapadd命令进行添加:
ldapadd -x -D "cn=admin,dc=test,dc=com" -W -f base.ldif
添加完成后进行测试:
[root@kdc ~]# ldapsearch -x -D "cn=admin,dc=test,dc=com" -W -b "dc=test,dc=com"
Enter LDAP Password:
# extended LDIF
#
# LDAPv3
# base <dc=test,dc=com> with scope subtree
# filter: (objectclass=*)
# requesting: ALL
#
# test.com
dn: dc=test,dc=com
dc: test
objectClass: dcObject
objectClass: organizationalUnit
ou: test.com
# accounts, test.com
dn: ou=accounts,dc=test,dc=com
ou: accounts
objectClass: organizationalUnit
# services, accounts, test.com
dn: ou=services,ou=accounts,dc=test,dc=com
ou: services
objectClass: organizationalUnit
# users, accounts, test.com
dn: ou=users,ou=accounts,dc=test,dc=com
ou: users
objectClass: organizationalUnit
# groups, accounts, test.com
dn: ou=groups,ou=accounts,dc=test,dc=com
ou: groups
objectClass: organizationalUnit
# kerberos, test.com
dn: cn=kerberos,dc=test,dc=com
cn: kerberos
objectClass: krbContainer
# search result
search: 2
result: 0 Success
# numResponses: 7
# numEntries: 6
- 添加测试用户
dn: cn=test,ou=users,ou=accounts,dc=test,dc=com cn: test sn: test objectclass: person objectclass: inetOrgPerson ou: users userPassword: 123456
ldapmodify -D "cn=admin,dc=test,dc=com" -w 123456 -x -a -f user.ldif
ldapsearch -D "cn=admin,dc=test,dc=com" -w 123456 -b "dc=test,dc=com"
- 修改密码
用户添加好以后,需要给其设定初始密码,运行命令如下:
$ ldappasswd -x -D 'cn=admin,dc=test,dc=com' -w 123456 "cn=test,ou=users,dc=example,dc=com" -S
- 修改用户所属组
cat > add_user_to_groups.ldif << “EOF”
dn: cn=ldapgroup1,ou=users,dc=test,dc=com
changetype: modify
add: memberuid
memberuid: ldapuser1
EOF
- 删除
删除用户或组条目:
$ ldapdelete -x -w 12345678 -D'cn=ldapadmin,ou=people,dc=example,dc=com' "cn=test,ou=people,dc=example,dc=com"
$ ldapdelete -x -w 12345678 -D'cn=ldapadmin,ou=people,dc=example,dc=com' "cn=test,ou=group,dc=example,dc=com"