什么是目录服务?

目录服务则是由目录数据库和一套能够访问和处理数据库信息的协议组成的服务协议,用于集中的管理主机帐号密码,员工名字等数据,大大的提升了管理工作效率。轻量级目录访问协议LDAP(Lightweight Directory Access Protocol)是在目录访问协议X.500的基础上研发的

主要的优势是:

X.500目录协议功能非常臃肿,消耗大量资源,无法做到快速查询且不支持TCP/IP协议网络。


什么是LDAP?

LDAP采用树状结构存储数据(类似于我们学习的DNS服务程序),用于在IP网络层面实现对分布式目录的访问和管理操作,条目是LDAP协议中最基本的元素,可以想象成字典中的单词或者数据库中的记录,通常对LDAP服务程序的添加、删除、更改、搜索都是以条目为基本对象的。


wKiom1ndF0TgmOLPAAFlKvoEpFs493.png

>dn:每个条目的唯一标识符,如上图中linuxprobe的dn值是


cn=linuxprobe,ou=marketing,ou=people,dc=mydomain,dc=org


>rdn:一般为dn值中最左侧的部分,如上图中linuxprobe的rdn值是


cn=linuxprobe


>base DN:此为基准DN值,表示顶层的根部,上图中的base DN值是


dc=mydomain,dc=org


每个条目可以有多个属性(如姓名、地址、电话等),每个属性中会保存着对象名称与对应值,LDAP已经为运维人员对常见的对象定义了属性:

 属性名称 属性别名 语法 描述值(举例)
commonName cnDirectory String名字 sean
surname sn Directory String 姓氏Chow
 organizationalUnitName  ouDirectory String 单位(部门)名称IT_SECTION
organization oDirectory String组织(公司)名称linuxprobe 
 telephoneNumber
Telephone Number电话号码110
objectClass 

内置属性organizationalPerson

目录服务实验


OpenLdap是基于LDAP协议的开源程序,它的程序名称叫做slapd。


实验环境:

主机名称 操作系统IP地址
LDAP服务端(node1.com) CentOS 7.0 192.168.9.203 
LDAP客户端 CentOS 7.0192.168.9.206 


配置LDAP服务端

配置LDAP服务器的主机名


[root@localhost ~]# vim /etc/hostname 


node1.com


安装openldap与相关的软件包

```

[root@node1 ~]# yum install -y openldap openldap-clients openldap-servers migrationtools

```

生成密钥文件

```

[root@node1 ~]# slappasswd -s node1 -n > /etc/openldap/passwd

[root@node1 ~]# cat /etc/openldap/passwd


{SSHA}DyfSP8lIHLUofsSlX3CSiM+J542KAU0n

```

写入一条主机与IP地址的解析记录到hosts文件

```

[root@node1 ~]# echo "192.168.9.203 node1.com" >> /etc/hosts

```

因为LDAP目录服务是以明文的方式在网络中传输数据的(包括密码),这样真的很不安全,所以我们采用TLS加密机制来解决这个问题,使用openssl工具生成X509格式的证书文件(有效期为365天)

```

[root@node1 ~]# openssl req -new -x509 -nodes -out /etc/openldap/certs/cert.pem -keyout /etc/openldap/certs/priv.pem -days 365

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]:敲击回车

State or Province Name (full name) []:敲击回车

Locality Name (eg, city) [Default City]:敲击回车

Organization Name (eg, company) [Default Company Ltd]:敲击回车

Organizational Unit Name (eg, section) []:敲击回车

Common Name (eg, your name or your server's hostname) []:node1.com

Email Address []:敲击回车


```

×××的所属与权限

```

[root@node1 ~]# cd /etc/openldap/certs/


[root@node1 certs]# chown ldap:ldap *


[root@node1 certs]# chmod 600 priv.pem


[root@node1 certs]# ls -al

总用量 96

drwxr-xr-x. 2 root root  4096 6月  10 19:43 .

drwxr-xr-x. 5 root root   100 6月  10 19:41 ..

-rw-r--r--. 1 ldap ldap 65536 6月  10 19:31 cert8.db

-rw-r--r--  1 ldap ldap  1318 6月  10 19:43 cert.pem

-rw-r--r--. 1 ldap ldap 16384 6月  10 19:31 key3.db

-r--r-----. 1 ldap ldap    45 12月  9 2016 password

-rw-------  1 ldap ldap  1704 6月  10 19:43 priv.pem

-rw-r--r--. 1 ldap ldap 16384 12月  9 2016 secmod.db

-rw-r--r--  1 root root     0 6月  10 19:31 .slapd-leave


```

复制一份LDAP的配置模板

```

[root@node1 ~]# cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG

```

生成数据库文件

>不用担心报错信息

```

[root@node1 ~]# slaptest

593bdc38 hdb_db_open: database "dc=my-domain,dc=com": db_open(/var/lib/ldap/id2entry.bdb) failed: No such file or directory (2).

593bdc38 backend_startup_one (type=hdb, suffix="dc=my-domain,dc=com"): bi_db_open failed! (2)

slap_startup failed (test would succeed using the -u switch)


```

修改LDAP数据库的所属主与组并启动slapd服务

```

[root@node1 ~]# chown ldap:ldap /var/lib/ldap/*


[root@node1 ~]# systemctl restart slapd

[root@node1 ~]# systemctl enable slapd

Created symlink from /etc/systemd/system/multi-user.target.wants/slapd.service to /usr/lib/systemd/system/slapd.service.


```

>在LDAP目录服务中使用LDIF(LDAP Interchange Format)格式来保存信息,而LDIF是一种标准的文本文件且可以随意的导入导出,所以我们需要有一种“格式”标准化LDIF文件的写法,这中格式叫做“schema”,schema用于指定一个目录中所包含对象的类型,以及每一个类型中的可选属性,我们可以将schema理解为面向对象程序设计中的“类”,通过“类”定义出具体的对象,因此其实LDIF数据条目则都是通过schema数据模型创建出来的具体对象。


ldapadd命令用于将LDIF文件导入到目录服务数据库中

参数作用
-x 进行简单认证 
-D 用于绑定服务器的dn
-h目录服务的地址 
-w 绑定dn的密码 
-f 使用LDIF文件进行条目添加的文件


添加cosine和nis模块


[root@node1 ~]# cd /etc/openldap/schema/


添加cosine模块

[root@node1 schema]# ldapadd -Y EXTERNAL -H ldapi:/// -D "cn=config" -f 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"


添加nis模块

[root@node1 schema]# ldapadd -Y EXTERNAL -H ldapi:/// -D "cn=config" -f 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"



```

创建/etc/openldap/changes.ldif文件,并将下面的信息复制进去

>在你做时,下面的olcRootPW:处一定要写你之前生成的密钥文件(如{SSHA}DyfSP8lIHLUofsSlX3CSiM+J542KAU0n)

```

[root@node1 ~]# vim /etc/openldap/changes.ldif


dn: olcDatabase={2}hdb,cn=config

changetype: modify

replace: olcSuffix

olcSuffix: dc=node1,dc=com


dn: olcDatabase={2}hdb,cn=config

changetype: modify

replace: olcRootDN

olcRootDN: cn=Manager,dc=node1,dc=com


dn: olcDatabase={2}hdb,cn=config

changetype: modify

replace: olcRootPW

olcRootPW: {SSHA}DyfSP8lIHLUofsSlX3CSiM+J542KAU0n


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=node1,dc=com" read by * none


```

将新的配置文件更新到slapd服务程序

```

[root@node1 ~]# ldapmodify -Y EXTERNAL -H ldapi:/// -f /etc/openldap/changes.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"


modifying entry "cn=config"


modifying entry "cn=config"


modifying entry "cn=config"


modifying entry "olcDatabase={1}monitor,cn=config"



```

创建/etc/openldap/base.ldif文件,并将下面的信息复制进去

```

[root@node1 ~]# vim /etc/openldap/base.ldif


dn: dc=node1,dc=com

dc: node1

objectClass: top

objectClass: domain


dn: ou=People,dc=node1,dc=com

ou: People

objectClass: top

objectClass: organizationalUnit


dn: ou=Group,dc=node1,dc=com

ou: Group

objectClass: top

objectClass: organizationalUnit

```

创建目录的结构服务

```

[root@node1 ~]# ldapadd -x -w node1 -D cn=Manager,dc=node1,dc=com -f /etc/openldap/base.ldif

adding new entry "dc=node1,dc=com"


adding new entry "ou=People,dc=node1,dc=com"


adding new entry "ou=Group,dc=node1,dc=com"



```

创建测试用的用户

```

[root@node1 ~]# useradd -d /home/ldap ldapuser


```

设置帐户的迁移

```

[root@node1 ~]# vim /usr/share/migrationtools/migrate_common.ph 


71 $DEFAULT_MAIL_DOMAIN = "node1.com";

74 $DEFAULT_BASE = "dc=node1,dc=com";

```

将当前系统中的用户迁移至目录服务

```

[root@node1 ~]# cd /usr/share/migrationtools/

[root@node1 migrationtools]# grep ":10[0-9][0-9]" /etc/passwd > passwd

[root@node1 migrationtools]# ./migrate_passwd.pl passwd users.ldif

[root@node1 migrationtools]# ldapadd -x -w node1 -D cn=Manager,dc=node1,dc=com -f users.ldif

adding new entry "uid=ldapuser,ou=People,dc=node1,dc=com"

```

将当前系统中的用户组迁移至目录服务

```

[root@node1 migrationtools]# grep ":10[0-9][0-9]" /etc/group > group

[root@node1 migrationtools]# ./migrate_group.pl group groups.ldif

[root@node1 migrationtools]# ldapadd -x -w node1 -D cn=Manager,dc=node1,dc=com -f groups.ldif

adding new entry "cn=ldapuser,ou=Group,dc=node1,dc=com"



```

测试linuxprobe用户的配置文件

```

[root@node1 ~]# ldapsearch -x cn=ldapuser -b dc=node1,dc=com

# extended LDIF

#

# LDAPv3

# base <dc=node1,dc=com> with scope subtree

# filter: cn=ldapuser

# requesting: ALL

#


# ldapuser, People, node1.com

dn: uid=ldapuser,ou=People,dc=node1,dc=com

uid: ldapuser

cn: ldapuser

objectClass: account

objectClass: posixAccount

objectClass: top

objectClass: shadowAccount

userPassword:: e2NyeXB0fSEh

shadowLastChange: 17327

shadowMin: 0

shadowMax: 99999

shadowWarning: 7

loginShell: /bin/bash

uidNumber: 1000

gidNumber: 1000

homeDirectory: /home/ldap


# ldapuser, Group, node1.com

dn: cn=ldapuser,ou=Group,dc=node1,dc=com

objectClass: posixGroup

objectClass: top

cn: ldapuser

userPassword:: e2NyeXB0fXg=

gidNumber: 1000


# search result

search: 2

result: 0 Success


# numResponses: 3

# numEntries: 2


```

安装httpd服务程序

```

[root@node1 ~]# yum install -y httpd


```

将密钥文件上传至网站目录并启动httpd服务

```

[root@node1 ~]# cp /etc/openldap/certs/cert.pem /var/www/html/


[root@node1 ~]# systemctl restart httpd

[root@node1 ~]# systemctl enable httpd


```

配置防火墙和SELinux

```

关闭防火墙


[root@node1 ~]# systemctl stop firewalld


关闭SELinux


[root@node1 ~]# setenforce 0

```

在日志记录服务的配置文件中追加下面语句,并重启日志服务

```

[root@node1 ~]# vim /etc/rsyslog.conf 

local4.* /var/log/ldap.log


[root@node1 ~]# systemctl restart rsyslog

```

配置LDAP客户端

将LDAP服务端主机名与IP地址的解析记录写入到hosts文件里

```

[root@localhost ~]# echo "192.168.9.203 node1.com" >> /etc/hosts

```

安装相关的软件包

```

[root@localhost ~]# yum install openldap-clients nss-pam-ldapd authconfig-gtk pam_krb5


```

运行系统认证工具,并填写LDAP服务信息

```

[root@localhost ~]# system-config-authentication

```

wKiom1ndHt7RoQ4NAAYLLuuk1ms898.png

wKioL1ncr9HxHz70AACrsjgjUWU149.png

wKioL1ncr9GCQnDsAAA9CMx7OOA635.png


稍等片刻后,验证本地是否已经有了ldapuser用户

```

[root@localhost ~]# id ldapuser

uid=1000(ldapuser) gid=1000(ldapuser) 组=1000(ldapuser)

```

wKiom1ndHxewLjWTAABdmrIuSIY628.png



>此时说明已经可以通过LDAP服务端验证了,并且ldapuser用户的帐号信息也不会保存在您本地的/etc/passwd文件中~


自动挂载用户目录

>虽然在客户端已经能够使用LDAP验证帐户了,但是当切换到ldapuser用户时会提示没有该用户的家目录

```

[root@localhost ~]# su - ldapuser

上一次登录:三 5月 31 16:24:16 CST 2017:0 上

su: 警告:无法更改到 /home/ldap 目录: 没有那个文件或目录

-bash-4.2$ 


```

wKiom1ndHzbCJZM9AAAmAcqpryg903.png


>原因是本机并没有该用户的家目录,我们需要配置NFS服务将用户的家目录自动挂载过来


在LDAP服务端添加共享信息

```

[root@node1 ~]# vim /etc/exports


/home/ldap 192.168.9.206 (rw,sync,root_squash)


```

重启nfs-server服务程序

```

[root@node1 ~]# systemctl restart nfs-server


```

注:如果出现如下错误

nfs-server服务起不来

```

[root@node1 ~]# systemctl restart nfs-server

Failed to restart nfs-server.service: Unit nfs-server.service failed to load: No such file or directory.


```

解决办法

安装autofs和nfs-utils

```

[root@node1 ~]# yum install -y autofs

[root@node1 ~]# yum install -y nfs-utils


```

再重启nfs-server服务

```

[root@node1 ~]# systemctl restart rpcbind

[root@node1 ~]# systemctl enable rpcbind

[root@node1 ~]# systemctl restart nfs-server


```

在LDAP客户端查看共享信息

```

[root@localhost ~]# showmount -e 192.168.9.203

Export list for 192.168.9.203:

/home/ldap (everyone)


```

将共享目录挂载到本地

```

[root@localhost ~]# mkdir /home/ldap

[root@localhost ~]# mount -t nfs 192.168.9.203:/home/ldap/ /home/ldap/


```

再次尝试切换到ldapuser用户,这样非常顺利

```

[root@localhost ~]# su - ldapuser

上一次登录:一 6月 12 16:16:49 CST 2017pts/0 上

[ldapuser@localhost ~]$ exit

登出

[root@localhost ~]# 

```

设置为开机自动挂载

```

[root@localhost ~]# vim /etc/fstab

192.168.9.203:/home/ldap /home/ldap nfs defaults 0 0

```


#完