OpenLDAP服务安装

 

参考:https://cloud.tencent.com/developer/article/1100819

众所周知Hadoop安全模块不存储用户和用户组信息,而是依赖Linux系统的用户和用户组。同时在集群开启安全认证模式之后,需要映射Kerberos Principle到Linux的用户以及映射用户到用户组。那么随之而来的问题是如何统一管理用户信息。这里统一管理用户信息的方式有多种(如OpenLDAP、AD等等),在前面Fayson介绍了如何在Redhat6中安装OpenLDAP,本篇文章主要讲述如何在ReadHat7中安装OpenLDAP并配置客户端。

  • 内容概述

1.OpenLDAP服务安装

2.导入根域及管理员账号

3.导入基础文件及用户和用户组

4.配置OpenLDAP客户端

  • 测试环境

1.Redhat7.3

2.OpenLDAP版本2.4.44

2.OpenLDAP安装及配置


在集群中选择一台服务器(ip-172-31-24-169.ap-southeast-1.compute.internal)作为OpenLDAP的Server

1.执行如下命令安装OpenLDAP服务

openldap:OpenLDAP服务端和客户端用的库文件

openldap-clients:客户端程序

openldap-servers:服务端程序

openldap-devel:开发包

compat-openldap:OpenLDAP兼容性库(主从依赖)

migrationtools: 用户转换依赖

[root@ip-172-31-24-169 ~]# yum -y install openldap openldap-clients openldap-servers migrationtools openldap-devel nss-pam-ldapd bind-dyndb-ldap compat-openldap perl-LDAP krb5-server-ldap php-ldap openssl

 

 

查看安装的RPM包

[root@ip-172-31-24-169 ~]# rpm -qa |grep openldap 

 

 

2.使用openssl生成TLS加密文件(如果不需要配置OpenLDAP的TLS则跳过此步)

使用如下命令生成服务器的RSA私钥

[root@ip-172-31-24-169 certs]# openssl genrsa -out ldap.key 1024

 

 

使用如下命令生成签名文件

[root@ip-172-31-24-169 certs]# openssl req -new -key ldap.key -out ldap.csr

 

 

注意:生成签名文件时必填的信息为“your server’s hostname”且为当前服务器的hostname。

使用如下文件生成公钥文件

[root@ip-172-31-24-169 certs]# openssl x509 -req -days 3653 -in ldap.csr -signkey ldap.key -out ldap.crt

 

 

将生成的公钥文件和私钥拷贝至/etc/openldap/certs目录下:

1[root@ip-172-31-24-169 certs]# scp ldap.crt ldap.key /etc/openldap/certs/
2[root@ip-172-31-24-169 certs]# ll /etc/openldap/certs/

 

 

3.修改OpenLDAP的slapd.ldif配置文件

安装OpenLDAP服务后默认的配置文件及数据库文件在/usr/share/openldap-servers目录下

 

将该目录下的slapd.ldif文件拷贝至/root或其他任一目录下

1[root@ip-172-31-24-169 openldap-servers]# cp slapd.ldif /root/
2[root@ip-172-31-24-169 openldap-servers]# cd /root/
3[root@ip-172-31-24-169 ~]# vim slapd.ldif 

 

修改slapd.ldif文件,将部分注释去掉,增加include的文件及配置管理员账号和OpenLDAP的根域信息,文件全部内容如下:

注意:配置文件中需要注意的TLS Settings配置,如果不启用则可以将相关的配置注释。

配置文件中多处配置dc=fayson,dc=com,由于我们OpenLDAP的域为fayson.com,如果LDAP的域为ldap.fayson.com则我们的配置为dc=ldap,dc=fayson,dc=com,根据自己LDAP的域名进行相应的修改。

 1#
 2# See slapd-config(5) for details on configuration options.
 3# This file should NOT be world readable.
 4#
 5dn: cn=config
 6objectClass: olcGlobal
 7cn: config
 8olcArgsFile: /var/run/openldap/slapd.args
 9olcPidFile: /var/run/openldap/slapd.pid
10#
11# TLS settings
12#
13olcTLSCACertificatePath: /etc/openldap/certs
14olcTLSCertificateFile: /etc/openldap/certs/ldap.crt
15olcTLSCertificateKeyFile: /etc/openldap/certs/ldap.key
16#
17# Schema settings
18#
19dn: cn=schema,cn=config
20objectClass: olcSchemaConfig
21cn: schema
22include: file:///etc/openldap/schema/corba.ldif
23include: file:///etc/openldap/schema/core.ldif
24include: file:///etc/openldap/schema/cosine.ldif
25include: file:///etc/openldap/schema/duaconf.ldif
26include: file:///etc/openldap/schema/dyngroup.ldif
27include: file:///etc/openldap/schema/inetorgperson.ldif
28include: file:///etc/openldap/schema/java.ldif
29include: file:///etc/openldap/schema/misc.ldif
30include: file:///etc/openldap/schema/nis.ldif
31include: file:///etc/openldap/schema/openldap.ldif
32include: file:///etc/openldap/schema/ppolicy.ldif
33include: file:///etc/openldap/schema/collective.ldif
34#
35# Frontend settings
36#
37dn: olcDatabase=frontend,cn=config
38objectClass: olcDatabaseConfig
39objectClass: olcFrontendConfig
40olcDatabase: frontend
41#
42# Configuration database
43#
44dn: olcDatabase=config,cn=config
45objectClass: olcDatabaseConfig
46olcDatabase: config
47olcAccess: to * by dn.base="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth" manage by * none
48#
49# Server status monitoring
50#
51dn: olcDatabase=monitor,cn=config
52objectClass: olcDatabaseConfig
53olcDatabase: monitor
54olcAccess: to * by dn.base="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth" read by dn.base="cn=Manager,dc=fayson,dc=com" read by * none
55#
56# Backend database definitions
57#
58dn: olcDatabase=hdb,cn=config
59objectClass: olcDatabaseConfig
60objectClass: olcHdbConfig
61olcDatabase: hdb
62olcSuffix: dc=fayson,dc=com
63olcRootDN: cn=Manager,dc=fayson,dc=com
64olcRootPW: 123456
65olcDbDirectory: /var/lib/ldap
66olcDbIndex: objectClass eq,pres
67olcDbIndex: ou,cn,mail,surname,givenname eq,pres,sub
68olcDbIndex: uidNumber,gidNumber,loginShell eq,pres
69olcDbIndex: uid,memberUid eq,pres,sub
70olcDbIndex: nisMapName,nisMapEntry eq,pres,sub

 

 

 

 

 

 

4.使用如下命令,重新生成OpenLDAP的配置(-n 0指定0号数据库,-F指定生成到的目标目录,-l以哪个文件进行生成)

1[root@ip-172-31-24-169 ~]# rm -rf /etc/openldap/slapd.d/*
2[root@ip-172-31-24-169 ~]# slapadd -F /etc/openldap/slapd.d -n 0 -l /root/slapd.ldif

 

 

 

测试配置文件是否正确,返回“config file testing succeeded”则表示配置文件正确

1[root@ip-172-31-24-169 ~]# slaptest -u -F /etc/openldap/slapd.d

 

 

修改配置文件的属主,操作如下:

1[root@ip-172-31-24-169 ~]# chown -R ldap. /etc/openldap/slapd.d/
2[root@ip-172-31-24-169 ~]# ll /etc/openldap/slapd.d/

 

 

5.安装OpenLDAP的数据库文件

将/usr/share/openldap-servers/目录下的DB_CONFIG.example文件拷贝至/var/lib/ldap目录下并重命名为DB_CONFIG,操作如下:

1[root@ip-172-31-24-169 ~]# cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
2[root@ip-172-31-24-169 ~]# ll /var/lib/ldap/

 

 

修改数据库文件属主

1[root@ip-172-31-24-169 ~]# chown -R ldap. /var/lib/ldap/
2[root@ip-172-31-24-169 ~]# ll /var/lib/ldap/

 

 

6.完成上述操作后,执行如下命令将slapd服务添加到系统自启动服务并启动slapd服务,查看服务启动状态

1[root@ip-172-31-24-169 ~]# systemctl enable slapd
2[root@ip-172-31-24-169 ~]# systemctl start slapd
3[root@ip-172-31-24-169 ~]# systemctl status slapd

 

 

至此OpenLDAP服务安装成功。

3.导入根域及管理员账号


1.创建root.ldif文件,内容如下

1[root@ip-172-31-24-169 ldap]# vim root.ldif
2dn: dc=fayson,dc=com
3dc: fayson
4objectClass: top
5objectClass: domain
6dn: cn=Manager,dc=fayson,dc=com
7objectClass: organizationalRole
8cn: Manager

 

 

2.导入根域及管理员信息到OpenLDAP服务中(-D是绑定到哪个dn上,-x是做一个简单的认证,-W交互式密码不输入密码,会交互式的提示用户输入密码,与-w二者选一,-w直接指定密码,-f执行哪个文件)

1[root@ip-172-31-24-169 ldap]# ldapadd -D "cn=Manager,dc=fayson,dc=com" -W -x -f root.ldif

 

 

3.查看是否导入成功(-h指定ldap服务地址,-b是指定从哪个地方开始搜寻)

1[root@ip-172-31-24-169 ldap]# ldapsearch -h ip-172-31-24-169.ap-southeast-1.compute.internal -b "dc=fayson,dc=com" -D "cn=Manager,dc=fayson,dc=com" -W

 

 

4.导入基础文件及用户和用户组


在安装OpenLDAP服务是安装了migrationtools服务,这里我们可以通过该服务生成OpenLDAP的基础文件、用户和用户组的ldif文件

1.进入/usr/share/migrationtools/目录修改migrate_common.ph文件,将文件中的$DEFAULT_MAIL_DOMAIN和$DEFAULT_BASE修改为自己OpenLDAP的域

1# Default DNS domain
2$DEFAULT_MAIL_DOMAIN = "fayson.com";
3# Default base 
4$DEFAULT_BASE = "dc=fayson,dc=com";

 

 

2.使用如下命令导出OpenLdap的base.ldif文件

1[root@ip-172-31-24-169 ldap]# /usr/share/migrationtools/migrate_base.pl >base.ldif
2[root@ip-172-31-24-169 ldap]# ll
3[root@ip-172-31-24-169 ldap]# vim base.ldif

 

 

根据自己的需求保留需要的基础域配置,此处是Fayson的配置

1dn: ou=People,dc=fayson,dc=com
2ou: People
3objectClass: top
4objectClass: organizationalUnit

5dn: ou=Group,dc=fayson,dc=com
6ou: Group
7objectClass: top
8objectClass: organizationalUnit
(第三步导入过再次导入会报错,如已经导入过,这里可以删掉)

 

3.执行如下命令导出操作系统的group.ldif文件

1[root@ip-172-31-24-169 ldap]# /usr/share/migrationtools/migrate_group.pl /etc/group > group.ldif
2[root@ip-172-31-24-169 ldap]# ll
3[root@ip-172-31-24-169 ldap]# vim group.ldif

 

 

根据需要删除不需要导入OpenLDAP服务的group,如下是Fayson的配置

 1dn: cn=root,ou=Group,dc=fayson,dc=com
 2objectClass: posixGroup
 3objectClass: top
 4cn: root
 5userPassword: {crypt}x
 6gidNumber: 0

 7dn: cn=fayson,ou=Group,dc=fayson,dc=com
 8objectClass: posixGroup
 9objectClass: top
10cn: fayson
11userPassword: {crypt}x
12gidNumber: 1001

 

 

4.使用如下命令导出操作系统用户的ldif文

1[root@ip-172-31-24-169 ldap]# /usr/share/migrationtools/migrate_passwd.pl /etc/passwd >user.ldif
2[root@ip-172-31-24-169 ldap]# ll
3[root@ip-172-31-24-169 ldap]# vim user.ldif

 

 

根据需要保留user.ldif文件中需要导入OpenLDAP服务的用户信息,注意用户信息与group.ldif中组的对应,否则会出现用户无相应组的问题,如下是Fayson的配置

 1dn: uid=root,ou=People,dc=fayson,dc=com
 2uid: root
 3cn: root
 4objectClass: account
 5objectClass: posixAccount
 6objectClass: top
 7objectClass: shadowAccount
 8userPassword: {crypt}!!
 9shadowLastChange: 17094
10shadowMin: 0
11shadowMax: 99999
12shadowWarning: 7
13loginShell: /bin/bash
14uidNumber: 0
15gidNumber: 0
16homeDirectory: /root
17gecos: root
18dn: uid=fayson,ou=People,dc=fayson,dc=com
19uid: fayson
20cn: fayson
21objectClass: account
22objectClass: posixAccount
23objectClass: top
24objectClass: shadowAccount
25userPassword: {crypt}!!
26shadowLastChange: 17566
27shadowMin: 0
28shadowMax: 99999
29shadowWarning: 7
30loginShell: /bin/bash
31uidNumber: 1001
32gidNumber: 1001
33homeDirectory: /home/fayson

 

 

使用slapadd命令将基础文件及用户和组导入OpenLDAP

ldapadd -D "cn=Manager,dc=fayson,dc=com" -W -x -f base.ldif
ldapadd -D "cn=Manager,dc=fayson,dc=com" -W -x -f group.ldif
ldapadd -D "cn=Manager,dc=fayson,dc=com" -W -x -f user.ldif

 

 

4.查看是否导入成功

ldapsearch -h ip-172-31-24-169.ap-southeast-1.compute.internal -b "dc=fayson,dc=com" -D "cn=Manager,dc=fayson,dc=com" -W|grep dn

 

 

5.OpenLDAP客户端配置


1.在ip-172-31-30-69节点安装OpenLDAP的客户端软件包

[root@ip-172-31-30-69 ~]# yum -y install openldap-clients

 

 

2.修改/etc/openldap/ldap.conf文件,内容如下:

[root@ip-172-31-30-69 ~]# yum -y install openldap-clients
[root@ip-172-31-30-69 ~]# vim /etc/openldap/ldap.conf 
#
# LDAP Defaults
#
# See ldap.conf(5) for details
# This file should be world readable but not world writable.
#BASE   dc=example,dc=com
#URI    ldap://ldap.example.com ldap://ldap-master.example.com:666
#SIZELIMIT      12
#TIMELIMIT      15
#DEREF          never
TLS_CACERTDIR   /etc/openldap/certs
URI ldap://ip-172-31-24-169.ap-southeast-1.compute.internal
BASE dc=fayson,dc=com
# Turning this off breaks GSSAPI used with krb5 when rdns = false
SASL_NOCANON    on

 

 

3.测试客户端是否配置成功

[root@ip-172-31-30-69 ~]# ldapsearch -D "cn=Manager,dc=fayson,dc=com" -W |grep dn

 

 

注意:如果未配置/etc/openldap/ldap.conf文件则需要在ldapsearch命令后加-hip-172-31-24-169.ap-southeast-1.compute.internal -b"dc=fayson,dc=com"参数。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值