ldap首先我们要知道这个ldap的概念,
LDAP是轻量目录访问协议(Lightweight Directory Access Protocol)的缩写
目录是一个为查询、浏览和搜索而优化的专业分布式数据库,它呈树状结构组织数据,就好象Linux/Unix系统中的文件目录一样。目录数据库和关系数据库不同,它有优异的读性能,但写性能差,并且没有事务处理、回滚等复杂功能,不适于存储修改频繁的数据。所以目录天生是用来查询的,就好象它的名字一样。
目录服务是由目录数据库和一套访问协议组成的系统。类似以下的信息适合储存在目录中:
- 企业员工信息,如姓名、电话、邮箱等;
- 公用证书和安全密钥;
- 公司的物理设备信息,如服务器,它的IP地址、存放位置、厂商、购买时间等;
2. LDAP特点
- LDAP的结构用树来表示,而不是用表格。正因为这样,就不能用SQL语句了
- LDAP可以很快地得到查询结果,不过在写方面,就慢得多
- LDAP提供了静态数据的快速查询方式
- Client/server模型,Server 用于存储数据,Client提供操作目录信息树的工具
- 这些工具可以将数据库的内容以文本格式(LDAP 数据交换格式,LDIF)呈现在您的面前
- LDAP是一种开放Internet标准,LDAP协议是跨平台的Interent协议
ldap的搭建也是分为单模式配置,或者主从模式的配置,也有主主模式的配置
下面搭建单模式
参考:https://cloud.tencent.com/developer/article/1155424
首先要关闭服务器防火墙
systemctl stop firewalld.service systemctl disable firewalld.service firewall-cmd --state
OpenLDAP安装
下载ldap:
yum install -y openldap yum install -y openldap openldap-* 启动:
systemctl start slapd
systemctl enable slapd
然后选择一个你要存储的配置文件的目录 ,我的习惯是再/opt下
vim installOpenldap.sh 写入:
#!/bin/bash
echo "install ldap rpm" 执行:
chmod 755 installOpenldap.sh
sh -x installOpenldap.sh
查看安装的ldpa服务:
查看OpenLDAP版本;
查看ldap启动状态
systemctl status slapd
[root@cloud01-ops-tools- ~]# systemctl status slapd
● slapd.service - OpenLDAP Server Daemon
Loaded: loaded (/usr/lib/systemd/system/slapd.service; disabled; vendor preset: disabled)
Active: active (running) since Mon -- :: CST; 9h ago
Docs: man:slapd
man:slapd-config
man:slapd-hdb
man:slapd-mdb
file:///usr/share/doc/openldap-servers/guide.html
Process: ExecStart=/usr/sbin/slapd -u ldap -h ${SLAPD_URLS} $SLAPD_OPTIONS (code=exited, status=/SUCCESS)
Process: ExecStartPre=/usr/libexec/openldap/check-config.sh (code=exited, status=/SUCCESS)
Main PID: (slapd)
Memory: 9.8M
CGroup: /system.slice/slapd.service
└─ /usr/sbin/slapd -u ldap -h ldapi:/// ldap:/// Mar :: cloud01-ops-tools- slapd[]: conn= op= SRCH base="c=cn" scope= deref= filter="(objectClass=*)"
Mar :: cloud01-ops-tools- slapd[]: conn= op= SRCH attr=objectclass
Mar :: cloud01-ops-tools- slapd[]: conn= op= SEARCH RESULT tag= err= nentries= text=
Mar :: cloud01-ops-tools- slapd[]: conn= op= SRCH base="ou=People,c=cn" scope= deref= filter="(objectClass=*)"
Mar :: cloud01-ops-tools- slapd[]: conn= op= SRCH attr=objectclass
Mar :: cloud01-ops-tools- slapd[]: conn= op= SEARCH RESULT tag= err= nentries= text=
Mar :: cloud01-ops-tools- slapd[]: conn= op= SRCH base="cn=Manager,c=cn" scope= deref= filter="(objectClass=*)"
Mar :: cloud01-ops-tools- slapd[]: conn= op= SRCH attr=objectclass
Mar :: cloud01-ops-tools- slapd[]: conn= op= SEARCH RESULT tag= err= nentries= text=
Mar :: cloud01-ops-tools- slapd[]: conn= fd= closed (connection lost)
查看openldap默认监听的389端口(centos7最小化安装默认没有netstat命令,需安装)
[root@openldap-master ~]# yum install net-tools -y
[root@openldap-master ~]# netstat -antup| grep
tcp 0.0.0.0: 0.0.0.0:* LISTEN /slapd
tcp6 ::: :::* LISTEN /slapd
温馨提示: 本案例测试时,已关闭了iptables防火墙。如果开启了iptables,则需要开放389端口
[root@openldap-master ~]# firewall-cmd --zone=public --add-port=/tcp --permanent
[root@openldap-master ~]# firewall-cmd --reload
======================================
配置OpenLDAP数据库
cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
[root@cloud01-ops-tools- openldap-servers]# cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
[root@cloud01-ops-tools- openldap-servers]# chown ldap:ldap -R /var/lib/ldap/
[root@cloud01-ops-tools- openldap-servers]# chmod -R /var/lib/ldap
[root@cloud01-ops-tools- openldap-servers]# ll /var/lib/ldap/
total
-rwx------ ldap ldap Mar : alock
-rwx------ ldap ldap Mar : __db.
-rwx------ ldap ldap Mar : __db.
-rwx------ ldap ldap Mar : __db.
-rwx------ ldap ldap Mar : DB_CONFIG
-rwx------ ldap ldap Mar : dn2id.bdb
-rwx------ ldap ldap Mar : id2entry.bdb
-rwx------ ldap ldap Mar : log.
配置ldap服务 设置OpenLDAP的管理员密码(这里密码为:123456)
[root@cloud01-ops-tools- openldap-servers]# slappasswd
New password:
Re-enter new password:
{SSHA}ago8nKNyfjhYa/btKgHDIpyEPxSBZrMm
然后进入你要保存配置文件的目录,我的目录是/opt
编辑chrootpw.ldif文件
vim chrootpw.ldif [root@openldap-master ~]# cd /opt/
[root@openldap-master opt]# vim chrootpw.ldif
# specify the password generated above for "olcRootPW" section
dn: olcDatabase={}config,cn=config
changetype: modify
add: olcRootPW
olcRootPW: {SSHA}ago8nKNyfjhYa/btKgHDIpyEPxSBZrMm # 这里是存放你的上面生成的密码
导入chrootpw.ldif
ldapadd -Y EXTERNAL -H ldapi:/// -f chrootpw.ldif [root@cloud01-ops-tools- opt]# ldapadd -Y EXTERNAL -H ldapi:/// -f chrootpw.ldif
SASL/EXTERNAL authentication started
SASL username: gidNumber=+uidNumber=,cn=peercred,cn=external,cn=auth
SASL SSF:
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=+uidNumber=,cn=peercred,cn=external,cn=auth
SASL SSF:
modifying entry "olcDatabase={0}config,cn=config"
ldap_modify: Inappropriate matching ()
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={}config,cn=config
changetype: modify
replace: olcRootPW
olcRootPW: {SSHA}ago8nKNyfjhYa/btKgHDIpyEPxSBZrMm
然后再次执行:
[root@openldap-master opt]# ldapadd -Y EXTERNAL -H ldapi:/// -f chrootpw.ldif
SASL/EXTERNAL authentication started
SASL username: gidNumber=+uidNumber=,cn=peercred,cn=external,cn=auth
SASL SSF:
modifying entry "olcDatabase={0}config,cn=config"
导入基本模式:
vim ldapaddBaseSchema.sh
[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 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=+uidNumber=,cn=peercred,cn=external,cn=auth
SASL SSF:
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=+uidNumber=,cn=peercred,cn=external,cn=auth
SASL SSF:
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=+uidNumber=,cn=peercred,cn=external,cn=auth
SASL SSF:
adding new entry "cn=inetorgperson,cn=schema,cn=config"
======================================================== 温馨提示: 如果上面的命令出现下面报错:
......
ldap_add: Other (e.g., implementation specific) error ()
additional info: olcAttributeTypes: Duplicate attributeType: "2.16.840.1.113730.3.1.1"
原因是: LDIF已经加载,所以尝试再次加载它们就报错这些信息,忽略这个步骤即可。 =======================================================
接着在ldap服务的DB中设置域名,即编辑chdomain.ldif文件
# replace to your own domain name for "dc=***,dc=***" section
# specify the password generated above for "olcRootPW" section
dn: olcDatabase={}monitor,cn=config
changetype: modify
replace: olcAccess
olcAccess: {}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={}hdb,cn=config
changetype: modify
replace: olcSuffix
olcSuffix: c=cn dn: olcDatabase={}hdb,cn=config
changetype: modify
replace: olcRootDN
olcRootDN: cn=Manager,c=cn dn: olcDatabase={}hdb,cn=config
changetype: modify
replace: olcRootPW
olcRootPW: {SSHA}ago8nKNyfjhYa/btKgHDIpyEPxSBZrMm # 你上面生成的密码密钥 dn: olcDatabase={}hdb,cn=config
changetype: modify
replace: olcAccess
olcAccess: {}to attrs=userPassword,shadowLastChange by
dn="cn=Manager,c=cn" write by anonymous auth by self write by * none
olcAccess: {}to dn.base="" by * read
olcAccess: {}to * by dn="cn=Manager,c=cn" write by * read
导入chdomain.ldif文件
ldapmodify -Y EXTERNAL -H ldapi:/// -f chdomain.ldif
[root@cloud01-ops-tools- opt]# ldapmodify -Y EXTERNAL -H ldapi:/// -f chdomain.ldif
SASL/EXTERNAL authentication started
SASL username: gidNumber=+uidNumber=,cn=peercred,cn=external,cn=auth
SASL SSF:
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 ()
additional info: modify/add: olcRootPW: no equality matching rule
解决办法:将chdomain.ldif文件中的"add"全部替换成"replace",然后重新执行上面命令即可! ============================================================
导入管理员基础数据
vim rootdn.ldif
#vim rootdn.ldif
dn: c=cn
objectclass: country
c: cn dn: cn=Manager,c=cn
objectclass: organizationalRole
cn: Manager
执行下面命令,输入上面设置的密码:123456 (就是输入你上面设置的密码)
[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 (-, 0xffffffff)
Trace (, 0x1)
Packets (, 0x2)
Args (, 0x4)
Conns (, 0x8)
BER (, 0x10)
Filter (, 0x20)
Config (, 0x40)
ACL (, 0x80)
Stats (, 0x100)
Stats2 (, 0x200)
Shell (, 0x400)
Parse (, 0x800)
Sync (, 0x4000)
None (, 0x8000) NOTE: custom log subsystems may be later installed by specific code
编辑logLevel.ldif文件:
dn: cn=config
changetype: modify
replace: olcLogLevel
olcLogLevel: stats
[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=+uidNumber=,cn=peercred,cn=external,cn=auth
SASL SSF:
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行
.......
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 :: openldap-master slapd[]: daemon: shutdown requested and initiated.
May :: openldap-master slapd[]: slapd shutdown: waiting for operations/tasks to finish
May :: openldap-master slapd[]: slapd stopped.
May :: openldap-master slapd[]: @(#) $OpenLDAP: slapd 2.4. (Apr ::) $##[email protected]:/builddir/build/BUILD/openldap-2.4./openldap-2.4./servers/slapd
May :: openldap-master slapd[]: slapd starting
然后下载ldapadmin就可以进行连接了
ldapadmin:http://www.ldapadmin.org/download/ldapadmin.html
然后打开你的连接设置
名字就是你的管理员配置的时候设置的:
cn=Manager,c=cn密码就是123456