OpenLDAP设置

OpenLDAP设置

以 root 用户登录,安装OpenLDAP服务器及客户端软件包:

[root@fs01]# yum -y install openldap compat-openldap openldap-clients openldap-servers openldap-devel sssd

从模板创建数据库,并将属主更改为ldap:ldap:

#复制样本数据库
[root@fs01]# cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG

#测试配置
[root@fs01]# slaptest -u
config file testing succeeded

# 更改文件属主
[root@fs01]# chown ldap:ldap /var/lib/ldap/*

启动并启用 slapd 服务:

#使能开机启动slapd服务
[root@fs01]# systemctl enable slapd
ln -s '/usr/lib/systemd/system/slapd.service' '/etc/systemd/system/multi-user.target.wants/slapd.service'

#手动启动slapd服务
[root@fs01]# systemctl start slapd

在防火墙中允许 ldap 端口:

#在防火墙配置中为openldap服务使能端口
[root@fs01]# firewall-cmd --permanent --add-service=ldap
success

#重启防火墙
[root@fs01]# firewall-cmd --reload
success

配置 OpenLDAP 系统日志,编辑文件/etc/rsyslog.conf,在文件中增加两行:

#编辑日志配置文件
[root@fs01]# vi /etc/rsyslog.conf

#在rsyslog.conf文件中增加下面两行
-----------------------------------
#LDAP Logging
local4.* /var/log/openldap.log
-----------------------------------

#重启服务
[root@fs01]# systemctl restart rsyslog

将所需的schema添加到我们的 OpenLDAP 目录:

[root@fs01]# ldapadd -Y EXTERNAL -H ldapi:/// -D "cn=config" -f /etc/openldap/schema/cosine.ldif
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
adding new entry "cn=cosine,cn=schema,cn=config"

[root@fs01]# ldapadd -Y EXTERNAL -H ldapi:/// -D "cn=config" -f /etc/openldap/schema/nis.ldif
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
adding new entry "cn=nis,cn=schema,cn=config"

为目录管理器生成密码哈希,复制生成的密码哈希,在后面的配置中需要用到:

[root@fs01]# slappasswd
New password:
Re-enter new password:
{SSHA}xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

添加初始数据库配置:

#在工作目录中创建db.ldif文件
[root@fs01]# vi db.ldif

#文件内容如下
#其中域名(dc)更改为实际使用的域名
#olcRootPW域的内容为上面生成的密码哈希
----------------------------------
dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcSuffix
olcSuffix: dc=richz,dc=tech

dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcRootDN
olcRootDN: cn=Manager,dc=richz,dc=tech

dn: olcDatabase={2}hdb,cn=config
changetype: modify
replace: olcRootPW
olcRootPW: {SSHA}xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
----------------------------------

#将db.ldif文件指定的内容更新到数据库
[root@fs01]# ldapmodify -Y EXTERNAL  -H ldapi:/// -f db.ldif
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
modifying entry "olcDatabase={2}hdb,cn=config"
modifying entry "olcDatabase={2}hdb,cn=config"
modifying entry "olcDatabase={2}hdb,cn=config"

为TLS 模式下运行此服务创建安全证书:

#创建安全证书,注意服务器hostname(fs01.richz.tech)需要与ldap服务器的域名一致
#使用证书时域名也需与产生证书时使用的域名一致
[root@fs01]# openssl req -new -x509 -nodes -out /etc/openldap/certs/cert.pem -keyout /etc/openldap/certs/priv.pem -days 3650
Generating a 2048 bit RSA private key
.....+++
...+++
writing new private key to '/etc/openldap/certs/priv.pem'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:GD
Locality Name (eg, city) [Default City]:SZ
Organization Name (eg, company) [Default Company Ltd]:richz
Organizational Unit Name (eg, section) []:it
Common Name (eg, your name or your server's hostname) []:fs01.richz.tech
Email Address []:[email protected]

#更改证书文件的属主及读写属性
[root@fs01]# cd /etc/openldap/certs/
[root@fs01]# chown ldap:ldap *
[root@fs01]# chmod 600 priv.pem

将安全证书和私钥添加到 LDAP 配置

#创建cert.ldif文件
[root@fs01]# vi cert.ldif
#文件内容如下
-----------------------------------
dn: cn=config
changetype: modify
replace: olcTLSCertificateFile
olcTLSCertificateFile: /etc/openldap/certs/cert.pem
-
replace: olcTLSCertificateKeyFile
olcTLSCertificateKeyFile: /etc/openldap/certs/priv.pem

dn: cn=config
changetype: modify
replace: olcLogLevel
olcLogLevel: -1
-----------------------------------

#添加配置
[root@fs01]# ldapmodify -Y EXTERNAL  -H ldapi:/// -f cert.ldif
SASL/EXTERNAL authentication started
SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth
SASL SSF: 0
modifying entry "cn=config"
modifying entry "cn=config"
modifying entry "cn=config"

创建目录、LDAP 管理用户和两个用于保存用户和组条目的组织单位。

#创建base.ldif文件
[root@fs01]# vi base.ldif
#文件内容如下
-----------------------------------
dn: dc=richz,dc=tech
dc: richz
objectClass: top
objectClass: domain

dn: cn=Manager ,dc=richz,dc=tech
objectClass: organizationalRole
cn: Manager
description: LDAP Manager

dn: ou=People,dc=richz,dc=tech
objectClass: organizationalUnit
ou: People

dn: ou=Group,dc=richz,dc=tech
objectClass: organizationalUnit
ou: Group
-----------------------------------

#添加配置
[root@fs01]# ldapmodify -Y EXTERNAL  -H ldapi:/// -f base.ldif

安装migrationtools,并使用migrationtools添加用户账号

#安装migrationtools软件包
[root@fs01]# yum install migrationtools

#修改migrate_common.ph文件
[root@fs01]# cd /usr/share/migrationtools
[root@fs01]# vi migrate_common.ph
# 修改文件中下面两行的dc为实际使用的域名
------------------------------------
$DEFAULT_MAIL_DOMAIN = "richz.tech";
$DEFAULT_BASE = "dc=richz,dc=tech";
------------------------------------

#在工作目录下运行如下命令提取用户组信息
[root@fs01]# grep ":10[0-9][0-9]" /etc/group > group
#将用户组信息转换为ldif格式文件
[root@fs01]# /usr/share/migrationtools/migrate_group.pl group group.ldif

#在工作目录下运行如下命令提取用户账户信息
[root@fs01]# grep ":10[0-9][0-9]" /etc/passwd > passwd
#将用户信息转换为ldif格式文件
[root@fs01]# /usr/share/migrationtools/migrate_passwd.pl passwd user.ldif

#导入用户组信息
[root@fs01]# ldapadd -x -W -D cn=Manager,dc=richz,dc=tech -f group.ldif
Enter LDAP Password:
adding new entry "cn=xxxxx,ou=Group,dc=richz,dc=tech"

adding new entry "cn=xxx,ou=Group,dc=richz,dc=tech"

adding new entry "cn=xx,ou=Group,dc=richz,dc=tech"


#导入用户信息
[root@fs01]# ldapadd -x -W -D cn=Manager,dc=richz,dc=tech -f user.ldif
Enter LDAP Password:
adding new entry "uid=xxxxx,ou=People,dc=richz,dc=tech"

adding new entry "uid=xx,ou=People,dc=richz,dc=tech"

adding new entry "uid=xx,ou=People,dc=richz,dc=tech"

在客户端配置LDAP

#配置域名解析,或者在客户端的/etc/hosts文件中增加ldap服务器域名解析
192.168.0.20  fs01.richz.tech

#客户端安装openldap及sssd软件包
[root@fs01]# yum -y install openldap* sssd nss-pam-ldapd

#复制sssd.conf到/etc/sssd/sssd.conf,并将属性改为600(属性必须为600才能为启动sssd服务)
[root@fs01]# cp /usr/share/doc/sssd-common/examples/sssd-example.conf /etc/sssd/sssd.conf
[root@fs01]# chmod 600 /etc/sssd/sssd.conf

#启动sssd服务,并使能开机启动
[root@fs01]# systemctl start sssd
[root@fs01]# systemctl enable sssd

#将自签名证书复制到我们的客户端机器上
[root@fs01]# scp [email protected]:/etc/openldap/certs/cert.pem /etc/openldap/cacerts/cert.pem

#在客户端机器上配置LDAP远程身份验证
[root@fs01]# authconfig --update --enableldap --enableldapauth --enableldaptls --ldapserver=fs01.richz.tech --ldapbasedn="dc=richz,dc=tech"

#验证LDAP连接
getent passwd <username>

# 强制使用TLS搜索服务器
ldapsearch -x -ZZ -H ldap://fs01.richz.tech -b dc=richz,dc=tech

# 不使用TLS
ldapsearch -x -H ldap://fs01.richz.tech -b dc=richz,dc=tech
#centos7下不创建本地用户须禁止初始设置
yum remove gnome-initial-setup

以 root 用户登录,安装OpenLDAP服务器及客户端软件包: 从模板创建数据库,并将属主更改为ldap: […]