Ceonts7 安装OpenLDAP
操作系统:Centos-7
[[email protected] ~]# uname -r
3.10.0-514.el7.x86_64
Openldap Version:
Name : openldap-serversVersion : 2.4.44
Release : 15.el7_5
Architecture: x86_64
Install Date: Wed 20 Jun 2018 11:58:33 PM EDT
主机规划:
172.16.91.254: ldap.example.com
172.16.92.254:client.example.com
部署步骤:
配置/etc/hosts对主机名进行解析:
vim /etc/hosts
172.16.92.254 ldap.example.com
172.16.91.254 client.example.com
将两台机器的防火墙与selinux全部关闭
iptables -F
systemclt stop firewalld
setenforce 0
保持机器时间同步:
步骤一:使用yum源在ldap.example.com安装软件
[[email protected] ~]# yum install openldap-servers openldap-clients openldap-devel
步骤二:
给管理员生成密码:
[[email protected] ~]# slappasswd
New password:
Re-enter new password:
{SSHA}Bf22UEHcFk1DvjqHGAlpGBV5bCfEHbk3
步骤三:
修改配置文件:
[[email protected] ~]# cd /etc/openldap/slapd.d/cn\=config/
[[email protected] cn=config]# vim olcDatabase\=\{1\}monitor.ldif
修改第六行内容,并将内容整理成为一行
6 olcAccess: {0}to * by dn.base="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth" read by dn.base="cn=Manager,dc=example,dc=com" read by * none
[[email protected] cn=config]# vim olcDatabase\=\{2\}hdb.ldif
7 olcDbDirectory: /var/lib/ldap
8 olcSuffix: dc=example,dc=com
9 olcRootDN: cn=Manager,dc=example,dc=com ----设置DN,DC 域为example.com
......
23 olcRootPW: {SSHA}Bf22UEHcFk1DvjqHGAlpGBV5bCfEHbk3 ----管理员密码
24 olcTLSCertificateFile: /etc/pki/tls/certs/example.crt ----tls证书
25 olcTLSCertificateKeyFile: /etc/pki/tls/certs/example.key ----tls私钥
步骤四:拷贝数据库配置文件
[[email protected] cn=config]# cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
[[email protected] cn=config]# chown ldap.ldap /var/lib/ldap/DB_CONFIG
步骤五:生成TLS认证证书:
[[email protected] cn=config]#cd /etc/pki/tls/certs
[[email protected] certs]# openssl req -new -x509 -nodes -out example.crt -keyout example.key
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:LN
Locality Name (eg, city) [Default City]:SY
Organization Name (eg, company) [Default Company Ltd]:chnhang
Organizational Unit Name (eg, section) []:edu
Common Name (eg, your name or your server's hostname) []:ldap.example.com
Email Address []:[email protected]
[[email protected] certs]# cp example.key ../private/
[[email protected] certs]# chown ldap:ldap example.*
[[email protected] certs]# chown ldap:ldap ../private/example.key
检测语法:
[[email protected] certs]# slaptest -u
5b2b6ae7 ldif_read_file: checksum error on "/etc/openldap/slapd.d/cn=config/olcDatabase={1}monitor.ldif"
5b2b6ae7 ldif_read_file: checksum error on "/etc/openldap/slapd.d/cn=config/olcDatabase={2}hdb.ldif"
config file testing succeeded
步骤六:启动服务
[[email protected] certs]# systemctl restart slapd
查看进程:
OpenLDAP监听的端口:
默认监听端口:389(明文数据传输)
加密监听端口:636(密文数据传输)
[[email protected] certs]# netstat -tnpl |grep slapd
tcp 0 0 0.0.0.0:389 0.0.0.0:* LISTEN 10359/slapd
tcp6 0 0 :::389 :::* LISTEN 10359/slapd
步骤七:给LDAP添加数据:
1)安装工具:
yum install migrationtools -y
2)修改工具配置文件
[[email protected] cn=config]# cd /usr/share/migrationtools/
[[email protected] migrationtools]# vim migrate_common.ph
61 $NAMINGCONTEXT{'group'} = "ou=Groups"; -----在Group后面添加一个s
71 $DEFAULT_MAIL_DOMAIN = "example.com"; ----指定域
74 $DEFAULT_BASE = "dc=example,dc=com"; ----指定dc
90 $EXTENDED_SCHEMA = 1; ----支持扩展插件
批量创建用户
vim /root/useradd.sh
#!/bin/bash
[ ! -d /ldaphome ] && mkdir /ldaphome
for i in `seq 10`
do
! id ldapuser$i && useradd ldapuser$i -d /ldaphome/ldapuser$i ldapuser$i && echo ldapuser$i |passwd --stdin ldapuser$i
done
执行添加用户脚本:
[[email protected] migrationtools]# bash /root/user.sh
测试查看用户是否添加:
[[email protected] migrationtools]# id ldapuser1
uid=1001(ldapuser1) gid=1001(ldapuser1) groups=1001(ldapuser1)
将用户账号、密码、组信息生成到导入文件中
[[email protected] migrationtools]#tail /etc/passwd > /tmp/user
[[email protected] migrationtools]# tail /etc/group > /tmp/group
[[email protected] migrationtools]# tail /etc/shadow > /tmp/shadow
修改密码获取的文件
[[email protected] migrationtools]# vim migrate_passwd.pl
188 open(SHADOW, "/tmp/shadow") || return;
生成导入数据类型的文件ldif
[[email protected] migrationtools]# ./migrate_base.pl > /tmp/base.ldif 先生成基本库文件
[[email protected] migrationtools]# ./migrate_passwd.pl /tmp/user > /tmp/user.ldif 再生成用户库文件
[[email protected] migrationtools]# ./migrate_group.pl /tmp/group > /tmp/group.ldif 再生成组库文件
将生成的数据库文件导入ldap库
[[email protected] migrationtools]# ldapadd -xWD "cn=Manager,dc=example,dc=com" -f /tmp/base.ldif
Enter LDAP Password:
adding new entry "dc=example,dc=com"
adding new entry "ou=Hosts,dc=example,dc=com"
adding new entry "ou=Rpc,dc=example,dc=com"
adding new entry "ou=Services,dc=example,dc=com"
adding new entry "nisMapName=netgroup.byuser,dc=example,dc=com"
adding new entry "ou=Mounts,dc=example,dc=com"
adding new entry "ou=Networks,dc=example,dc=com"
adding new entry "ou=People,dc=example,dc=com"
adding new entry "ou=Groups,dc=example,dc=com"
adding new entry "ou=Netgroup,dc=example,dc=com"
adding new entry "ou=Protocols,dc=example,dc=com"
adding new entry "ou=Aliases,dc=example,dc=com"
adding new entry "nisMapName=netgroup.byhost,dc=example,dc=com"
[[email protected] migrationtools]# ldapadd -xWD "cn=Manager,dc=example,dc=com" -f /tmp/user.ldif
Enter LDAP Password:
adding new entry "uid=ldapuser1,ou=People,dc=example,dc=com"
adding new entry "uid=ldapuse2,ou=People,dc=example,dc=com"
adding new entry "uid=ldapuse3,ou=People,dc=example,dc=com"
adding new entry "uid=ldapuse4,ou=People,dc=example,dc=com"
adding new entry "uid=ldapuse5,ou=People,dc=example,dc=com"
adding new entry "uid=ldapuse6,ou=People,dc=example,dc=com"
adding new entry "uid=ldapuse7,ou=People,dc=example,dc=com"
adding new entry "uid=ldapuse8,ou=People,dc=example,dc=com"
adding new entry "uid=ldapuse9,ou=People,dc=example,dc=com"
adding new entry "uid=ldapuse10,ou=People,dc=example,dc=com"
[[email protected] migrationtools]# ldapadd -xWD "cn=Manager,dc=example,dc=com" -f /tmp/group.ldif
Enter LDAP Password:
adding new entry "cn=ldapuse1,ou=Groups,dc=example,dc=com"
adding new entry "cn=ldapuse2,ou=Groups,dc=example,dc=com"
adding new entry "cn=ldapuse3,ou=Groups,dc=example,dc=com"
adding new entry "cn=ldapuse4,ou=Groups,dc=example,dc=com"
adding new entry "cn=ldapuse5,ou=Groups,dc=example,dc=com"
adding new entry "cn=ldapuse6,ou=Groups,dc=example,dc=com"
adding new entry "cn=ldapuse7,ou=Groups,dc=example,dc=com"
adding new entry "cn=ldapuse8,ou=Groups,dc=example,dc=com"
adding new entry "cn=ldapuse9,ou=Groups,dc=example,dc=com"
adding new entry "cn=ldapuse10,ou=Groups,dc=example,dc=com"
出现以上结果,即导库成功。
到client端测试:
yum provides /*bin/authconfig-gtk ----查看该命令所属软件包
authconfig-gtk-6.2.8-30.el7.x86_64 : Graphical tool for setting up authentication from network services
Repo : base
Matched from:
Filename : /usr/sbin/authconfig-gtk
Filename : /usr/bin/authconfig-gtk
yum install authconfig-gtk nss-pam-ldapd pam_krb5 -y
使用图形化指定LDAP验证用户:
authconfig-gtk ----开启图形化
在客户端测试:
[[email protected] ~]# id ldapuser10
uid=1010(ldap10) gid=1010(ldap10) groups=1010(ldap10)
导入数据时报错:
Enter LDAP Password: ldap_bind: Invalid credentials (49)
解决:是由于本机的/etc/hosts域名没有解析