openldap服务端安装配置
阅读目录
- 基础环境准备
- 安装openldap服务端
- 初始化openldap配置
- 启动OpenLDAP
- 重新生成配置文件信息
- 规划OpenLDAP目录树组织架构
- 使用GUI客户端登录查看
- openldap配置日志
- 通过migrationtools实现用户及用户组的添加
- OpenLDAP控制策略
1. 基础环境准备
1.1 系统环境
- 系统:centos 6.9
- 软件:openldap-2.4.23-32
表1:IP地址及主机名规划
| 主机 | 系统版本 | IP地址 | 主机名 | 时间同步 | 防火墙 | SElinux |
| --- | -------- | ------ | ----- | ------- | ----- | -------- |
| ldap服务端 | Centos 6.9最小化安装 | 192.168.244.17 | mldap01.gdy.com | 必须同步 | 关闭 | 关闭 |
| ldap客户端 | Centos 6.9最小化安装 | 192.168.244.18 | test01.gdy.com | 必须同步 | 关闭 | 关闭 |
表2:OpenLDAP相关软件包套件
| 软件包名称 | 软件包功能描述 |
| --------- | ------------- |
| openldap | OpenLDAP服务端和客户端必须用的库文件 |
| openldap-clients | 在LDAP服务端上使用, 用于查看和修改目录的命令行的包 |
| openldap-servers | 用于启动服务和设置,包含单独的LDAP后台守护程序 |
| openldap-servers-sql | 支持SQL模块 |
| compat-openldap | openldap兼容性库 |
将主机名添加至hosts文件中(两主机都要,本文主要为服务端)
[[email protected] ~]# cat >> /etc/hosts << EOF
> 192.168.244.17 mldap01.gdy.com mldap01
> 192.168.244.18 test01.gdy.com test01
> EOF
2. 安装openldap服务端
通过yum安装openldap组建,解决软件包的依赖关系。
建议使用yum安装,简单方便快捷。
[[email protected] ~]# yum -y install openldap openldap-servers openldap-clients
3. 初始化OpenLDAP配置
- 复制配置文件至/etc/openldap目录下
[[email protected] ~]# cp -a /usr/share/openldap-servers/slapd.conf.obsolete /etc/openldap/slapd.conf [[email protected] ~]# rm -rf /etc/openldap/slapd.d/*
- 创建数据库文件(从模板进行复制),生成DB_CONFIG及启动OpenLDAP进程slapd,命令如下。
[[email protected] ~]# cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG [[email protected] ~]# chown -R ldap.ldap /var/lib/ldap/*
- 生成密码待用
[[email protected] ~]# slappasswd New password: Re-enter new password: {SSHA}MPZ6cRiXmkjFylwNetRnZunefUEzuKVL
- 修改配置文件
除了loglevel新增,其他配置均为修改
[[email protected] ~]# vim /etc/openldap/slapd.conf access to * by dn.exact="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth" read by dn.exact="cn=Manager,dc=gdy,dc=com" read by * none suffix "dc=gdy,dc=com" rootdn "cn=Manager,dc=gdy,dc=com" rootpw {SSHA}MPZ6cRiXmkjFylwNetRnZunefUEzuKVL loglevel -1 # 新增在最后一行
- 修改相关文件属性
[[email protected] ~]# chown ldap.ldap -R /etc/openldap [[email protected] ~]# chown ldap.ldap -R /var/lib/ldap [[email protected] ~]# rm -rf /etc/openldap/slapd.d/*
4. 启动OpenLDAP,并设置成开机启动
[[email protected] ~]# /etc/init.d/slapd restart
Stopping slapd: [FAILED]
ls: cannot access /etc/openldap/slapd.d//cn=config/olcDatabase*.ldif: No such file or directory
Starting slapd: [ OK ]
[[email protected] ~]# ss -tnlp | grep slapd
LISTEN 0 128 :::389 :::* users:(("slapd",10497,8))
LISTEN 0 128 *:389 *:* users:(("slapd",10497,7))
[[email protected] ~]# chkconfig slapd on
[[email protected] ~]# chkconfig --list slapd
slapd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
5. 重新生成配置文件信息
每次修改了配置文件,都需要删除/etc/openldap/slapd.d/*,重新生成
[[email protected] ~]# ls -lh /etc/openldap/slapd.d/
total 0
[[email protected] ~]# slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d/
config file testing succeeded
[[email protected] ~]# chown ldap.ldap -R /etc/openldap/
[[email protected] ~]# ls -lh /etc/openldap/slapd.d/
total 8.0K
drwxr-x--- 3 ldap ldap 4.0K May 22 15:21 cn=config
-rw------- 1 ldap ldap 1.3K May 22 15:21 cn=config.ldif
6. 规划OpenLDAP目录树组织架构
- 通过LDIF添加目录树
[[email protected] ~]# cat << EOF | ldapadd -x -D "cn=Manager,dc=gdy,dc=com" -W > dn: dc=gdy,dc=com > dc: gdy > objectClass: top > objectClass: domain > > dn: ou=people,dc=gdy,dc=com > ou: people > objectClass: top > objectClass: organizationalUnit > > dn: ou=group,dc=gdy,dc=com > ou: group > objectClass: top > objectClass: organizationalUnit > EOF Enter LDAP Password: adding new entry "dc=gdy,dc=com" adding new entry "ou=people,dc=gdy,dc=com" adding new entry "ou=group,dc=gdy,dc=com"
- 通过ldapsearch查看目录树架构
[[email protected] ~]# ldapsearch -x -LLL dn: dc=gdy,dc=com dc: gdy objectClass: top objectClass: domain dn: ou=people,dc=gdy,dc=com ou: people objectClass: top objectClass: organizationalUnit dn: ou=group,dc=gdy,dc=com ou: group objectClass: top objectClass: organizationalUnit
如果报错
[[email protected] ~]# ldapsearch -x -LLL
No such object (32)
解决方法:
[[email protected] ~]# cat >> /etc/openldap/ldap.conf << EOF BASE dc=gdy,dc=com URI ldap://mldap01.gdy.com EOF
7. 使用GUI客户端登录查看
LDAP GUI客户端有如下:
- phpLDAPadmin:web管理客户端
- LDAPAdmin:window客户端
- LAM:web管理客户端
本文使用GUI客户端为LDAPAdmin,该工具是window下一个绿色程序。所以非常推荐
- LDAPAdmin安装
LDAPAdmin安装非常简单,可以从官网地址下载,然后解压即用
- LDAPAdmin程序使用
- LDAP配置界面选项介绍
- Host: 连接OpenLDAP服务端所使用的IP地址。
- Port: 连接OpenLDAP服务所使用的端口,默认使用389端口,加密使用636端口。
- Version:OpenLDAP服务端所使用的版本
- Base: OpenLDAP的根域,如dc=gdy,dc=com
- Username: OpenLDAP服务管理员,如cn=Manager,dc=gdy,dc=com
- Password: OpenLDAP管理员密码,通过rootpw指定。
- 连接至OpenLDAP服务端进行管理
- 其他使用(未写了,因为和普通的工具一样使用)
参考另一篇文档LDAPAdmin使用
8. openldap配置日志
- openldap的日志级别
[[email protected] ~]# slapd -d ? Installed log subsystems: Any (-1, 0xffffffff) //开启所有的dug信息 Trace (1, 0x1) //跟踪trace函数调用 Packets (2, 0x2) //与软件包的处理相关的dug信息 Args (4, 0x4) //全面的debug信息 Conns (8, 0x8) //链接数管理的相关信息 BER (16, 0x10) //记录包发送和接收的信息 Filter (32, 0x20) //记录过滤处理的过程 Config (64, 0x40) //记录配置文件的相关信息 ACL (128, 0x80) //记录访问控制列表的相关信息 Stats (256, 0x100) //记录链接、操作以及统计信息 Stats2 (512, 0x200) //记录向客户端响应的统计信息 Shell (1024, 0x400) //记录与shell后端的通信信息 Parse (2048, 0x800) //记录条目的分析结果信息 Sync (16384, 0x4000) //记录数据同步资源消耗的信息 None (32768, 0x8000) //不记录 NOTE: custom log subsystems may be later installed by specific code
- 创建目录及调整权限,用于存放日志文件
[[email protected] ~]# mkdir -pv /var/log/slapd mkdir: created directory `/var/log/slapd‘ [[email protected] ~]# chown ldap.ldap /var/log/slapd
- 修改日志文件,重启rsyslog使其加载OpenLDAP参数
[[email protected] ~]# cat >> /etc/rsyslog.conf << EOF > local4.* /var/log/slapd/slapd.log > EOF [[email protected] ~]# tail -1 /etc/rsyslog.conf local4.* /var/log/slapd/slapd.log [[email protected] ~]# /etc/init.d/rsyslog restart Shutting down system logger: [ OK ] Starting system logger: [ OK ]
- 查看日志配置是否生效
由于在2.4小节的时候已经设置记录了所有文件。所以没有配置slapd.conf也没有重启slapd进程。
使用tail -f 监听日志文件,然后使用LDAPAdmin刷新一下立马就会有日志出现,有日志出现就证明生效了。
[[email protected] ~]# tail -f /var/log/slapd/slapd.log May 22 16:24:38 mldap01 slapd[10497]: daemon: activity on 1 descriptor May 22 16:24:38 mldap01 slapd[10497]: daemon: activity on: May 22 16:24:38 mldap01 slapd[10497]: 14r May 22 16:24:38 mldap01 slapd[10497]: May 22 16:24:38 mldap01 slapd[10497]: daemon: read active on 14 May 22 16:24:38 mldap01 slapd[10497]: daemon: epoll: listen=7 a ...
- 利用系统logrotate实现自动切割日志
[[email protected] ~]# cat > /etc/logrotate.d/ldap << EOF > /var/log/slapd/slapd.log { > prerotate > /usr/bin/chattr -a /var/log/slapd/slapd.log > endscript > compress > delaycompress > notifempty > rotate 100 > size 10M > postrotate > /usr/bin/chattr +a /var/log/slapd/slapd.log > endscript > } > EOF
此时当/var/log/slapd/slapd.log日志大于10M就会自动切割了。
9. 通过migrationtools实现用户及用户组的添加
- 安装migrationtools工具
[[email protected] ~]# yum -y install migrationtools
- 修改migrationtools的配置文件
修改/usr/share/migrationtools/migrate_common.ph文件中的两个地方
- 使用migrationtools生成openldap根域条目和用户条目及组条目
[[email protected] ~]# /usr/share/migrationtools/migrate_base.pl > base.ldif [[email protected] ~]# /usr/share/migrationtools/migrate_passwd.pl /etc/passwd > passwd.ldif [[email protected] ~]# /usr/share/migrationtools/migrate_group.pl /etc/group > group.ldif
- 导入生成的ldif文件
[[email protected] ~]# ldapadd -x -W -D "cn=Manager,dc=gdy,dc=com" -c -f base.ldif 加-c是指跳过错误,继续导入 Enter LDAP Password: adding new entry "dc=gdy,dc=com" ldap_add: Already exists (68) adding new entry "ou=Hosts,dc=gdy,dc=com" ... adding new entry "ou=People,dc=gdy,dc=com" ldap_add: Already exists (68) adding new entry "ou=Group,dc=gdy,dc=com" ldap_add: Already exists (68) ... adding new entry "nisMapName=netgroup.byhost,dc=gdy,dc=com" [[email protected] ~]# ldapadd -x -W -D "cn=Manager,dc=gdy,dc=com" -c -f group.ldif Enter LDAP Password: adding new entry "cn=root,ou=Group,dc=gdy,dc=com" adding new entry "cn=bin,ou=Group,dc=gdy,dc=com" adding new entry "cn=daemon,ou=Group,dc=gdy,dc=com" ... adding new entry "cn=ntp,ou=Group,dc=gdy,dc=com" adding new entry "cn=ldap,ou=Group,dc=gdy,dc=com" adding new entry "cn=tomcat,ou=Group,dc=gdy,dc=com" [[email protected] ~]# ldapadd -x -W -D "cn=Manager,dc=gdy,dc=com" -c -f passwd.ldif Enter LDAP Password: adding new entry "uid=root,ou=People,dc=gdy,dc=com" adding new entry "uid=bin,ou=People,dc=gdy,dc=com" ... adding new entry "uid=ntp,ou=People,dc=gdy,dc=com" adding new entry "uid=ldap,ou=People,dc=gdy,dc=com" adding new entry "uid=tomcat,ou=People,dc=gdy,dc=com"
- 此时使用LDAPAdmin查看
10. OpenLDAP控制策略
默认情况下,不允许OpenLDAP用户自身修改密码,仅管理员具有修改权限。为了提高个人帐号的安全性,需要让用户自身可以修改并更新密码信息,不需要管理员干涉。具体步骤如下:
# 用户没有权限修改密码
[[email protected] ~]$ passwd
Changing password for user appman.
Enter login(LDAP) password:
New password:
Retype new password:
LDAP password information update failed: Insufficient access # 权限不足
passwd: Authentication token manipulation error
# 在openldap服务器上修改slapd.conf配置文件。
[[email protected] ~]# vim /etc/openldap/slapd.conf # 在database config行上面添加,否则无效
access to attrs=shadowLastChange,userPassword
by self write
by * auth
access to *
by * read
[[email protected] ~]# rm -rf /etc/openldap/slapd.d/*
[[email protected] ~]# slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d/
config file testing succeeded
[[email protected] ~]# chown -R ldap.ldap /etc/openldap
[[email protected] ~]# /etc/init.d/slapd restart
Stopping slapd: [ OK ]
Starting slapd: [ OK ]
# 配置后,用户有权限更改密码了。
[[email protected] ~]$ passwd
Changing password for user appman.
Enter login(LDAP) password:
New password:
Retype new password:
LDAP password information changed for appman
passwd: all authentication tokens updated successfully.
11. 本文系列通用ldif, 所有实验都删除后重新使用下面ldif
dn: dc=gdy,dc=com
dc: gdy
objectClass: top
objectClass: domain
dn: ou=people,dc=gdy,dc=com
ou: people
objectClass: top
objectClass: organizationalUnit
dn: ou=group,dc=gdy,dc=com
ou: group
objectClass: top
objectClass: organizationalUnit
dn: uid=user1,ou=people,dc=gdy,dc=com
uid: user1
cn: user1
objectClass: account
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
shadowMin: 0
shadowMax: 99999
shadowWarning: 7
loginShell: /bin/bash
uidNumber: 30001
gidNumber: 30001
homeDirectory: /home/user1
dn: uid=user2,ou=people,dc=gdy,dc=com
uid: user2
cn: user2
objectClass: account
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
shadowMin: 0
shadowMax: 99999
shadowWarning: 7
loginShell: /bin/bash
uidNumber: 30002
gidNumber: 30002
homeDirectory: /home/user2
dn: uid=user3,ou=people,dc=gdy,dc=com
uid: user3
cn: user3
objectClass: account
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
shadowMin: 0
shadowMax: 99999
shadowWarning: 7
loginShell: /bin/bash
uidNumber: 30003
gidNumber: 30003
homeDirectory: /home/user3
dn: uid=user4,ou=people,dc=gdy,dc=com
uid: user4
cn: user4
objectClass: account
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
shadowMin: 0
shadowMax: 99999
shadowWarning: 7
loginShell: /bin/bash
uidNumber: 30004
gidNumber: 30004
homeDirectory: /home/user4
dn: uid=user5,ou=people,dc=gdy,dc=com
uid: user5
cn: user5
objectClass: account
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
shadowMin: 0
shadowMax: 99999
shadowWarning: 7
loginShell: /bin/bash
uidNumber: 30005
gidNumber: 30005
homeDirectory: /home/user5
dn: uid=user6,ou=people,dc=gdy,dc=com
uid: user6
cn: user6
objectClass: account
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
shadowMin: 0
shadowMax: 99999
shadowWarning: 7
loginShell: /bin/bash
uidNumber: 30006
gidNumber: 30006
homeDirectory: /home/user6
dn: uid=user7,ou=people,dc=gdy,dc=com
uid: user7
cn: user7
objectClass: account
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
shadowMin: 0
shadowMax: 99999
shadowWarning: 7
loginShell: /bin/bash
uidNumber: 30007
gidNumber: 30007
homeDirectory: /home/user7
dn: uid=user8,ou=people,dc=gdy,dc=com
uid: user8
cn: user8
objectClass: account
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
shadowMin: 0
shadowMax: 99999
shadowWarning: 7
loginShell: /bin/bash
uidNumber: 30008
gidNumber: 30008
homeDirectory: /home/user8
dn: uid=user9,ou=people,dc=gdy,dc=com
uid: user9
cn: user9
objectClass: account
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
shadowMin: 0
shadowMax: 99999
shadowWarning: 7
loginShell: /bin/bash
uidNumber: 30009
gidNumber: 30009
homeDirectory: /home/user9
dn: uid=user10,ou=people,dc=gdy,dc=com
uid: user10
cn: user10
objectClass: account
objectClass: posixAccount
objectClass: top
objectClass: shadowAccount
shadowMin: 0
shadowMax: 99999
shadowWarning: 7
loginShell: /bin/bash
uidNumber: 30010
gidNumber: 30010
homeDirectory: /home/user10
dn: cn=user1,ou=group,dc=gdy,dc=com
objectClass: posixGroup
objectClass: top
cn: user1
gidNumber: 30001
dn: cn=user2,ou=group,dc=gdy,dc=com
objectClass: posixGroup
objectClass: top
cn: user2
gidNumber: 30002
dn: cn=user3,ou=group,dc=gdy,dc=com
objectClass: posixGroup
objectClass: top
cn: user3
gidNumber: 30003
dn: cn=user4,ou=group,dc=gdy,dc=com
objectClass: posixGroup
objectClass: top
cn: user4
gidNumber: 30004
dn: cn=user5,ou=group,dc=gdy,dc=com
objectClass: posixGroup
objectClass: top
cn: user5
gidNumber: 30005
dn: cn=user6,ou=group,dc=gdy,dc=com
objectClass: posixGroup
objectClass: top
cn: user6
gidNumber: 30006
dn: cn=user7,ou=group,dc=gdy,dc=com
objectClass: posixGroup
objectClass: top
cn: user7
gidNumber: 30007
dn: cn=user8,ou=group,dc=gdy,dc=com
objectClass: posixGroup
objectClass: top
cn: user8
gidNumber: 30008
dn: cn=user9,ou=group,dc=gdy,dc=com
objectClass: posixGroup
objectClass: top
cn: user9
gidNumber: 30009
dn: cn=user10,ou=group,dc=gdy,dc=com
objectClass: posixGroup
objectClass: top
cn: user10
gidNumber: 30010
原文地址:https://www.cnblogs.com/cishi/p/9127544.html