# systemctl stop firewalld.service //关闭防火墙 # vim /etc/selinux/config //关闭selinux # yum install -y openldap openldap-clients openldap-servers migrationtools2.密钥和授权
# slappasswd -s kylin -n > /etc/openldap/passwd //生成密钥 # cat /etc/openldap/passwd //查看密钥 # echo "172.30.202.24 instructor.kylin.com" >> /etc/hosts //地址解析 # openssl req -new -x509 -nodes -out /etc/openldap/certs/cert.pem -keyout /etc/openldap/certs/priv.pem -days 365 //按回车 # cd /etc/openldap/certs/ # chown ldap:ldap * //授权组 # chmod 600 priv.pem //授权3.复制配置模板,生成数据库文件,并修改ldap数据库属主组,systemctl启动服务
# cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_ConFIG //复制模板 # slaptest -u //生成 # chown ldap:ldap /var/lib/ldap/* //修改权限 # systemctl restart slapd //重启服务4.添加cosine和nis模块,创建/etc/openldap/changes.ldif文件
[root@kylin ~]# cd /etc/openldap/schema/ [root@kylin schema]# ldapadd -Y EXTERNAL -H ldapi:/// -D "cn=config" -f cosine.ldif [root@kylin schema]# ldapadd -Y EXTERNAL -H ldapi:/// -D "cn=config" -f nis.ldif [root@kylin ~]vim /etc/openldap/changes.ldif
dn: olcDatabase={2}mdb,cn=config
changetype: modify
replace: olcSuffix
olcSuffix: dc=kylin,dc=com
dn: olcDatabase={2}mdb,cn=config
changetype: modify
replace: olcRootDN
olcRootDN: cn=Manager,dc=kylin,dc=com
dn: olcDatabase={2}mdb,cn=config
changetype: modify
replace: olcRootPW
olcRootPW: {SSHA}6NLGrRgkIXH2RE/n7+atbG8BMks1CoiI //修改成前面生成的密钥
dn: cn=config
changetype: modify
replace: olcTLSCertificateFile
olcTLSCertificateFile: /etc/openldap/certs/cert.pem
dn: cn=config
changetype: modify
replace: olcTLSCertificateKeyFile
olcTLSCertificateKeyFile: /etc/openldap/certs/priv.pem
dn: cn=config
changetype: modify
replace: olcLogLevel
olcLogLevel: -1
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=kylin,dc=com" read by * none
更新到slapd服务程序
[root@kylin ~]# ldapmodify -Y EXTERNAL -H ldapi:/// -f /etc/openldap/changes.ldif //更新新的配置文件5.创建/etc/openldap/base.ldif文件
[root@kylin ~]# vim /etc/openldap/base.ldif
dn: dc=kylin,dc=com dc: kylin objectClass: top objectClass: domain dn: ou=People,dc=kylin,dc=com ou: People objectClass: top objectClass: organizationalUnit dn: ou=Group,dc=kylin,dc=com ou: Group objectClass: top objectClass: organizationalUnit
更新配置文件
[root@kylin ~]# ldapadd -x -w kylin -D cn=Manager,dc=kylin,dc=com -f /etc/openldap/base.ldif
6.添加账户,设置账户迁移报错 ldap_add: Protocol error (2) additional info: no attributes provided 去掉空行
[root@kylin ~]# useradd -d /home/ldap ldapuser //添加账户
创建用户 [root@localhost ~]# mkdir /home/guests [root@localhost ~]# useradd -d /home/guests/ldapuser01 ldapuser01 [root@localhost ~]# passwd ldapuser01 [root@localhost ~]# useradd -d /home/guests/ldapuser02 ldapuser02 [root@localhost ~]# passwd ldapuser02
设置账户迁移(修改第71与74行) # vim /usr/share/migrationtools/migrate_common.ph -------------------------------------------------------------- $DEFAULT_MAIL_DOMAIN = "kylin.com"; $DEFAULT_base = "dc=kylin,dc=com"; --------------------------------------------------------------
将当前系统中的用户迁移至目录服务 [root@localhost ~]# cd /usr/share/migrationtools [root@localhost migrationtools]# grep ":10[0-9][0-9]" /etc/passwd > passwd [root@localhost migrationtools]# ./migrate_passwd.pl passwd users.ldif [root@localhost migrationtools]# ldapadd -x -w kylin -D cn=Manager,dc=kylin,dc=com -f users.ldif adding new entry "uid=ldapuser01,ou=People,dc=kylin,dc=com" adding new entry "uid=ldapuser02,ou=People,dc=kylin,dc=com"
将当前系统中的用户组迁移至目录服务 [root@localhost migrationtools]# grep ":10[0-9][0-9]" /etc/group > group [root@localhost migrationtools]# ./migrate_group.pl group groups.ldif [root@localhost migrationtools]# ldapadd -x -w kylin -D cn=Manager,dc=kylin,dc=com -f groups.ldif adding new entry "cn=ldapuser01,ou=Group,dc=kylin,dc=com" adding new entry "cn=ldapuser02,ou=Group,dc=kylin,dc=com"服务控制 1.重启服务
# systemctl restart slapd.service2.修改ldap密码,生成新的密钥,修改配置文件
# slappasswd # vim /etc/openldap/changes.ldif # ldapmodify -Y EXTERNAL -H ldapi:/// -f /etc/openldap/changes.ldif3. 在线查询条目
# ldapsearch -x -b dc=kylin,dc=com4.离线查询条目
# slapcat5.离线添加数据库条目
# useradd ldaptest03 # passwd ldaptest03
生成user.ldif文件
# cd /usr/share/migrationtools # grep "ldaptest03" /etc/passwd > userinfo # ./migrate_passwd.pl userinfo > user.ldif
停止服务
# systemctl stop slapd.service
添加条目
# slapadd -l user.ldif
6.在线添加条目
# systemctl restart slapd.service # useradd ldaptest04 # passwd ldaptest04 # cd /usr/share/migrationtools # grep "ldaptest04" /etc/passwd > userinfo # ./migrate_passwd.pl userinfo > user.ldif # ldapadd -x -W -D "cn=Manager,dc=kylin,dc=com" -f user.ldif
修改条目
创建new-user.ldif文件
# vim new-user.ldif -------------------------------------------------------------- dn: uid=ldaptest04,ou=People,dc=kylin,dc=com changeType: modify replace: loginShell loginShell: /bin/sh add: description description: A new attribute for example. delete: userPassword --------------------------------------------------------------
执行ldapmodify命令,通过new-user.ldif文件修改指定条目
# ldapmodify -x -W -D cn=Manager,dc=kylin,dc=com -f new-user.ldif
查询loginShell是否已修改
# ldapsearch -x -b "dc=kylin,dc=com" '(uid=ldaptest04)'7.删除条目
# ldapdelete -x -D 'cn=Manager,dc=kylin,dc=com' -W 'uid=ldaptest04,ou=People,dc=kylin,dc=com'
查询结果为0
# ldapsearch -x -b "dc=kylin,dc=com" '(uid=ldaptest04)'8.配置LDAP图形界面
下载phpldapadmin软件包,安装php7版本
# yum -y install httpd php php-ldap php-gd php-mbstring php-fpm php-bcmath php-xml
修改httpd访问端口
# vim /etc/httpd/conf/httpd.conf
Listen 80 修改为 Listen 58100
配置httpd
# vim /etc/httpd/conf.d/phpldapadmin.conf 修改为:Require all granted
修改phpldapadmin配置文件
# vim /etc/phpldapadmin/config.php
取消第166行及169行注释,并将false改为true
$config->custom->appearance['hide_template_warning'] = true;
$config->custom->appearance['minimalMode'] = true;
在文件末尾增加:
$servers->setValue('login','attr','dn');
$servers->setValue('server','host','10.1.40.190');//10.1.40.190是本机ip
$servers->setValue('server','port',389);
$servers->setValue('server','base',array('dc=example,dc=com'));
$servers->setValue('login','auth_type','session');
$servers->setValue('server','tls',false);
$servers->setValue('unique','attrs',array('uid','sn'));
重启httpd服务
# systemctl restart httpd.service
打开浏览器,输入http://IP:51800/ldapadmin/
在打开的LDAP页面,点击登录,输入DN为cn=Manager,dc=example,dc=com,输入密码,点击认证;



