ldap单机

环境规划

ip 主机名 安装软件
192.168.199.118 yfm18 ldap server
192.168.199.117 yfm17 ldap client
在yfm18上执行

安装 OpenLDAP 服务端,设置数据库配置文件
yum -y install openldap-servers openldap-clients 
cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
chown ldap.ldap /var/lib/ldap/DB_CONFIG

启动
systemctl start slapd
开机自启
systemctl enable slapd

设置 OpenLDAP 的管理员用户 root 的密码123456
[root@yfm18 ldap]# slappasswd
New password: 
Re-enter new password: 
{SSHA}C1vMXU5d5YaC2/tJKVlczBNZA9pWTBCw

将生成的密码添加至 OpenLDAP 的 ldif 文件中。LDIF 是修改 OpenLDAP 内容的标准文本格式
vi chrootpw.ldif
dn: olcDatabase={0}config,cn=config
changetype: modify
add: olcRootPW
olcRootPW: {SSHA}C1vMXU5d5YaC2/tJKVlczBNZA9pWTBCw

[root@yfm18 ldap]# 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"

添加几个基础的 Schema,此处添加的 Schema 主要用于记录人员信息。如果搭建该 OpenLDAP 的目的为用户认证,一般都会导入以下 Schema
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


在 LDAP 数据库中设置根域和数据库超级管理员
这里的“根域”可以和这台服务器 FQDN 中的根域不同,此处我们设置为 dc=colinlee,dc=fish。
数据库管理员和上面设置过的 OpenLDAP 超级管理员并非同一管理员,而且这里设置的管理员目前尚未创建。此处的设置同样需要一个用 slappasswd 命令生成的密码,为了方便管理,我们使用刚刚生成的密码,不再重新生成

vi chdomain.ldif
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=admin,dc=colinlee,dc=fish" read
  by * none

dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcSuffix
olcSuffix: dc=colinlee,dc=fish

dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcRootDN
olcRootDN: cn=admin,dc=colinlee,dc=fish

dn: olcDatabase={2}hdb,cn=config
changetype: modify
add: olcRootPW
olcRootPW: {SSHA}C1vMXU5d5YaC2/tJKVlczBNZA9pWTBCw

dn: olcDatabase={2}hdb,cn=config
changetype: modify
add: olcAccess
olcAccess: {0}to attrs=userPassword,shadowLastChange
  by dn="cn=admin,dc=colinlee,dc=fish" write
  by anonymous auth
  by self write
  by * none
olcAccess: {1}to dn.base=""
  by * read
olcAccess: {2}to *
  by dn="cn=admin,dc=colinlee,dc=fish" write
  by * read

[root@yfm18 ldap]# 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"
modifying entry "olcDatabase={2}hdb,cn=config"

创建用户节点、组节点和数据库超级管理员
vi basedomain.ldif
dn: dc=colinlee,dc=fish
objectClass: top
objectClass: dcObject
objectclass: organization
o: Example Inc.
dc: colinlee

dn: ou=user,dc=colinlee,dc=fish
objectClass: organizationalUnit
ou: user

dn: ou=group,dc=colinlee,dc=fish
objectClass: organizationalUnit
ou: group

dn: cn=admin,dc=colinlee,dc=fish
objectClass: organizationalRole
cn: admin
description: Directory Administrator

[root@yfm18 ldap]# ldapadd -x -D cn=admin,dc=colinlee,dc=fish -W -f basedomain.ldif
Enter LDAP Password: 123456
adding new entry "dc=colinlee,dc=fish"
adding new entry "ou=user,dc=colinlee,dc=fish"
adding new entry "ou=group,dc=colinlee,dc=fish"
adding new entry "cn=admin,dc=colinlee,dc=fish"

添加一个用户
vi ldapuser.ldif
dn: uid=yuanfan,ou=user,dc=colinlee,dc=fish
objectClass: inetOrgPerson
objectClass: posixAccount
objectClass: shadowAccount
cn: yuanfan
sn: Linux
userPassword: {SSHA}C1vMXU5d5YaC2/tJKVlczBNZA9pWTBCw
loginShell: /bin/bash
uidNumber: 1000
gidNumber: 1000
homeDirectory: /home/yuanfan

dn: cn=yuanfan,ou=group,dc=colinlee,dc=fish
objectClass: posixGroup
cn: yuanfan
gidNumber: 1000
memberUid: yuanfan

[root@yfm18 ldap]# ldapadd -x -D cn=admin,dc=colinlee,dc=fish -W -f ldapuser.ldif
Enter LDAP Password: 
adding new entry "uid=yuanfan,ou=user,dc=colinlee,dc=fish"
adding new entry "cn=yuanfan,ou=group,dc=colinlee,dc=fish"


在yfm17上执行
yum -y install openldap-clients nss-pam-ldapd

authconfig --enableldap --enableldapauth --ldapserver=192.168.199.118 --ldapbasedn="dc=colinlee,dc=fish" --enablemkhomedir --update

接下来用上面创建的LDAP用户登录
在这里插入图片描述

在yfm18上执行
yum install -y httpd
mv /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/welcome.conf_bak
vi /etc/httpd/conf/httpd.conf
95行下面加一行
     95 #ServerName www.example.com:80
     96 ServerName www.walkingcloud.cn
在151行处改为
     AllowOverride All
164行处改为
     DirectoryIndex index.html index.php index.cgi
在最下面添加如下两行
ServerTokens Prod
KeepAlive On


systemctl start httpd
systemctl enable httpd.service

安装php
yum install php php-mbstring php-pear
systemctl restart httpd

yum --enablerepo=epel -y install phpldapadmin

vi /etc/phpldapadmin/config.php
397行取消注释,398行注释掉
$servers->setValue('login','attr','dn');
//$servers->setValue('login','attr','uid');

vi /etc/httpd/conf.d/phpldapadmin.conf 
12行处添加访问的网段信息,例如192.168.199.0/24
Require ip 192.168.199.0/24

然后重启httpd服务systemctl start httpd

访问http://192.168.199.118/ldapadmin/

在这里插入图片描述

在这里插入图片描述

LDAP Admin下载

下载地址:http://www.ldapadmin.org/download/index.html,点击Program Files
我这里下载的是LdapAdminExe-w64-1.8.3.zip

在这里插入图片描述

参考:

OpenLDAP 在 CentOS 7 上的极速搭建步骤

CentOS7下搭建OpenLDAP服务器

猜你喜欢

转载自blog.csdn.net/yfm081616/article/details/115101232