备注:若大家只是想直接装完这个,理论部分不需要看,直接走安装步骤1、2、3等就可以了,可以装完之后再回过头来理解理解;
注意,不同linux操作系统版本之间的差异很大,我用的是centos7;
其实啊,openldap的安装真的是非常简单,直接用yum装一个server端和client端,然后启动服务就OK了。难的是修改它的配置文件以及导入我们需要的shema(这个其实理解了它的概念也就不难了)。
安装步骤1.yum 安装
#!/bin/bash
echo "install ldap rpm"
[root@dlp ~]yum install -y openldap-servers openldap-clients
[root@dlp ~]cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
[root@dlp ~]chown ldap. /var/lib/ldap/DB_CONFIG
systemctl start slapd
systemctl enable slapd
安装步骤2.修改原始配置
# generate encrypted password
[root@dlp ~]# slappasswd
New password:
Re-enter new password: {SSHA}xxxxxxxxxxxxxxxxxxxxxxxx
[root@dlp ~]# vi chrootpw.ldif
# specify the password generated above for "olcRootPW" section dn:
olcDatabase={0}config,cn=config
changetype: modify
add: olcRootPW
olcRootPW: {SSHA}xxxxxxxxxxxxxxxxxxxxxxxx
[root@dlp ~]# 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"
理论: backend & database
ldap的后台进程slapd接收、响应请求,但实际存储数据、获取数据的操作是由Backends做的,而数据是存放在database中,所以你可以看到往往你可以看到backend和database指令是一样的值如moduleload back_bdb指令。
bdb是一个高性能的支持事务和故障恢复的数据库后端,可以满足绝大部分需求。许多旧文档里(包括官方)说建议将bdb作为首选后端服务(primary backend),但2.4版文档明确说hdb才是被首先推荐使用的,这从slapd.backends。
另外config是特殊的backend,用来在运行时管理slapd的配置,它只能有一个实例,甚至无需显式在slapd.conf中配置。
原始配置都存放在/etc/openldap/slapd.d/cn=config下,
这里面有这么几个文件:cn=schema文件夹、olcDatabase={0}config.ldif、olcDatabase={1}monitor.ldif(监控)、olcDatabase={2}hdb.ldif(数据库)、olcDatabase={-1}frontend.ldif;
前面带个{},应该是约定好的规则;其中cn=schema文件夹中存放的是我们导入的schema;剩下的几个都是配置文件,其中我们可以直接修改olcDatabase={0}config.ldif的密码;我们修改域名时需要修改olcDatabase={1}monitor.ldif、olcDatabase={2}hdb.ldif、文件;
我们有两种方式来修改默认的example配置,一是直接在配置文件中修改(不建议),另一个是调用客户端的命令进行修改;我这儿介绍第二种
修改(要注意空行或者是空格)
安装步骤4.修改域名
# generate directory manager's password
[root@dlp ~]# slappasswd
New password:
Re-enter new password: {SSHA}xxxxxxxxxxxxxxxxxxxxxxxx
[root@dlp ~]# vi 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,dc=srv,dc=world" read by * none
dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcSuffix
olcSuffix: dc=srv,dc=world
dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcRootDN
olcRootDN: cn=Manager,dc=srv,dc=world
dn: olcDatabase={2}hdb,cn=config
changetype: modify
add: olcRootPW
olcRootPW: {SSHA}xxxxxxxxxxxxxxxxxxxxxxxx
dn: olcDatabase={2}hdb,cn=config
changetype: modify
add: olcAccess
olcAccess: {0}to attrs=userPassword,shadowLastChange by dn="cn=Manager,dc=srv,dc=world" write by anonymous auth by self write by * none olcAccess: {1}to dn.base="" by * read
olcAccess: {2}to * by dn="cn=Manager,dc=srv,dc=world" write by * read
[root@dlp ~]# 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"
理论:同步骤3
这些文件或文件夹的命令都有点儿奇怪,但你要是理解ldap本身的规则,就见怪不怪了
安装步骤5.开放端口:
# systemctl stop firewalld
# systemctl disable firewalld
# setenforce 0
安装步骤6.添加用户
现在我们就可以添加用户了
# generate encrypted password
[root@dlp ~]# slappasswd
New password:
Re-enter new password:
{SSHA}xxxxxxxxxxxxxxxxx
[root@dlp ~]# vi ldapuser.ldif
# create new # replace to your own domain name for "dc=***,dc=***" section
dn:uid=cent,ou=People,dc=srv,dc=world
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount
cn: Cent
sn: Linux
userPassword: {SSHA}xxxxxxxxxxxxxxxxx
loginShell: /bin/bash
uidNumber: 1000
gidNumber: 1000
homeDirectory: /home/cent
dn: cn=cent,ou=Group,dc=srv,dc=world
objectClass: posixGroup
cn: Cent
gidNumber: 1000
memberUid: cent
[root@dlp ~]# ldapadd -x -D cn=Manager,dc=srv,dc=world -W -f ldapuser.ldif
Enter LDAP Password:
adding new entry "uid=cent,ou=People,dc=srv,dc=world"
adding new entry "cn=cent,ou=Group,dc=srv,dc=world"
安装步骤7.客户端连接
配置完我们可以直接用客户端连接,比如:Apache Directory、ldapadministartor、ldapBrowser等客户端工具,大家可以自己下载
常用命令:
ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/cosine.ldif
ldapmodify -Y EXTERNAL -H ldapi:/// -f chdomain.ldif (注意需要在修改的条目第二行添加changetype:modify)
dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcSuffix
olcSuffix: dc=srv,dc=world
不管是修改系统文件,亦或是引入模式都可以使用该命令
添加一般用户:ldapadd -x -D cn=Manager,dc=srv,dc=world -W -f ldapuser.ldif
修改应该也是这样
路径:ldap://192.168.101.89/dc=srv,dc=world
参考:
https://jingyan.baidu.com/article/1709ad80616d864634c4f0b7.html