[ 总结 ] RHEL6/Centos6 使用OpenLDAP集中管理用户帐号

  使用轻量级目录访问协议(LDAP)构建集中的身份验证系统可以减少管理成本,增强安全性,避免数据复制的问题,并提供数据的一致性。

          用户帐号信息使用的目录信息树

 

配置LDAP服务器

server : 192.168.0.110

client : 1921.68.0.111

[root@super ~]# yum install openldap-*  -y    # 安装openldap所需包
openldap-clients-2.4.19-15.el6.x86_64
openldap-devel-2.4.19-15.el6.x86_64
openldap-2.4.19-15.el6.x86_64
openldap-servers-2.4.19-15.el6.x86_64

[root@super ~]# cd /etc/openldap/
[root@super openldap]# ls
certs  check_password.conf  ldap.conf  schema  slapd.d   
[root@super openldap]# mv slapd.d slapd.d-bak    # 不需要ldap.d的扩展。

[root@super openldap]# cp -a /usr/share/openldap-servers/slapd.conf.obsolete ./slapd.conf    # 拷贝slapd.conf模板到配置文件目录
[root@super openldap]# slappasswd -h {MD5}  # 生成MD5加密的rootpw
[root@super openldap]# vim slapd.conf
114 database        bdb
115 suffix          "dc=super,dc=com"
116 checkpoint      1024 15
117 rootdn          "cn=Manager,dc=super,dc=com"
118 # Cleartext passwords, especially for the rootdn, should
119 # be avoided.  See slappasswd(8) and slapd.conf(5) for details.
120 # Use of strong authentication encouraged.
121 rootpw          {MD5}4QrcOUm6Wau+VuBX8g+IPg==

[root@super openldap]# slaptest -u -f slapd.conf   # 测试配置文件修改是否正确。
config file testing succeeded

  

创建数据库文件:

 

[root@super openldap]# cd /var/lib/ldap/
[root@super ldap]# cp -a /usr/share/openldap-servers/DB_CONFIG.example  ./DB_CONFIG  # 复制模板文件,不然重启的时候会报错valid(49)说是不合法的密码的错误。
[root@super ldap]# chown ldap:ldap DB_CONFIG
[root@super ldap]# service slapd restart
Stopping slapd:                                            [FAILED]
Starting slapd:                                            [  OK  ]
[root@super ldap]# chkconfig --list slapd
slapd              0:off    1:off    2:off    3:off    4:off    5:off    6:off
[root@super ldap]# chkconfig slapd on


创建待认证的用户:

[root@super ldap]# mkdir /mnt/ldapusers  # 标准化ldapuser用户家目录

[root@super ldap]# useradd -d /mnt/ldapusers/ldapuser1 ldapuser1
[root@super ldap]# useradd -d /mnt/ldapusers/ldapuser2 ldapuser2
[root@super ldap]# useradd -d /mnt/ldapusers/ldapuser3 ldapuser3

[root@super ldap]# echo 123456 | passwd --stdin ldapuser1
Changing password for user ldapuser1.
passwd: all authentication tokens updated successfully.
[root@super ldap]# echo 123456 | passwd --stdin ldapuser2
Changing password for user ldapuser2.
passwd: all authentication tokens updated successfully.
[root@super ldap]# echo 123456 | passwd --stdin ldapuser3
Changing password for user ldapuser3.
passwd: all authentication tokens updated successfully.



安装migrationtools工具。

[root@super ldap]# yum install migrationtools -y
[root@super ldap]# cd /usr/share/migrationtools/
[root@super migrationtools]# vim migrate_common.ph
 70 # Default DNS domain
 71 $DEFAULT_MAIL_DOMAIN = "super.com";
 72
 73 # Default base
 74 $DEFAULT_BASE = "dc=super,dc=com";

几个主要的概念 

dn:一条记录的位置 

dc:一条记录所属区域 

ou:一条记录所属组织 

cn/uid:一条记录的名字/ID

 

创建基本的数据库文件 
[root@super migrationtools]# ./migrate_base.pl > base.ldif
[root@super migrationtools]# vim base.ldif  # 删除该文件多余部分,剩下如下部分:
  1 dn: dc=super,dc=com
  2 dc: super
  3 objectClass: top
  4 objectClass: domain
  5
  6 dn: ou=People,dc=super,dc=com
  7 ou: People
  8 objectClass: top
  9 objectClass: organizationalUnit
 10
 11 dn: ou=Group,dc=super,dc=com
 12 ou: Group
 13 objectClass: top
 14 objectClass: organizationalUni
[root@super migrationtools]# ./migrate_passwd.pl /etc/passwd ./user.ldif
[root@super migrationtools]# vim user.ldif  # 删除文件多余部分。留下如下三段。
dn: uid=ldapuser1,ou=People,dc=super,dc=com
......
dn: uid=ldapuser2,ou=People,dc=super,dc=com
......
dn: uid=ldapuser3,ou=People,dc=super,dc=com
......
[root@super migrationtools]# ./migrate_group.pl /etc/group group.ldif
[root@super migrationtools]# vim group.ldif   # 删除文件多余部分。留下如下三段。
dn: cn=ldapuser1,ou=Group,dc=super,dc=com
......
dn: cn=ldapuser2,ou=Group,dc=super,dc=com
......
dn: cn=ldapuser3,ou=Group,dc=super,dc=com
......

检查产生的三个文件:

[root@super migrationtools]# ll *.ldif
-rw-r--r-- 1 root root  247 Mar 21 18:24 base.ldif
-rw-r--r-- 1 root root  408 Mar 21 18:31 group.ldif
-rw-r--r-- 1 root root 1344 Mar 21 18:30 user.ldif
[root@super migrationtools]# ldapadd -D "cn=Manager,dc=super,dc=com" -W -x -f base.ldif
Enter LDAP Password:
adding new entry "dc=super,dc=com"

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

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

[root@super migrationtools]# ldapadd -D "cn=Manager,dc=super,dc=com" -W -x -f group.ldif
Enter LDAP Password:
adding new entry "cn=ldapuser1,ou=Group,dc=super,dc=com"

adding new entry "cn=ldapuser2,ou=Group,dc=super,dc=com"

adding new entry "cn=ldapuser3,ou=Group,dc=super,dc=com"

[root@super migrationtools]# ldapadd -D "cn=Manager,dc=super,dc=com" -W -x -f user.ldif
Enter LDAP Password:
adding new entry "uid=ldapuser1,ou=People,dc=super,dc=com"

adding new entry "uid=ldapuser2,ou=People,dc=super,dc=com"

adding new entry "uid=ldapuser3,ou=People,dc=super,dc=com"

三组数据都添加至目录数据库中。

[root@super migrationtools]# ldapsearch -x -b "dc=super,dc=com" # 查看数据是否添加完成。


搭建web环境,我这里使用yum安装 http+php

[root@super config]# yum install httpd php php-ldap -y
[root@super src]# ls
phpldapadmin-1.2.2.tgz  # 已下载好的phpldapadmin  下载地址:http://sourceforge.net/projects/phpldapadmin/files/phpldapadmin-php5/1.2.2
[root@super www]# tar xf phpldapadmin-1.2.2.tgz -C /var/www/html/
[root@super html]# mv phpldapadmin-1.2.2/ ldap
[root@super html]# chown -R root:apache ldap/
[root@super html]# cd ldap/config/
[root@super config]# ls
config.php.example
[root@super config]# mv config.php.example config.php

到这里服务器端的ldap和phpldapadmin 已经配置完成。

使用web访问:

这里的用户名和密码是在/etc/openldap/slapd.conf里配置的。


这样就表示添加成功。


配置autofs自动挂载:

服务器端配置:

[root@super config]# vim /etc/exports 

/mnt/ldapusers  *(rw,sync)

[root@super config]# service nfs restart

[root@super config]# showmount -e 192.168.0.110
Export list for 192.168.0.110:
/mnt/ldapusers *

客户端配置:

setup

选择项如上图

 

尝试远程登录ldapuser1


登录成功。但是没有家目录。使用autofs实现自动挂载。
[root@localhost ~]# rpm -qa | grep autofs
autofs-5.0.5-113.el6.x86_64
[root@localhost ~]# vim /etc/auto.master
 20 # same will not be seen as the first read key seen takes
 21 # precedence.
 22 /mnt/ldapusers  auto.nfs
保存退出
[root@localhost ~]# vim /etc/auto.nfs
* -fstype=nfs,rw,sync   192.168.0.110:/mnt/ldapusers/&
[root@localhost ~]# service autofs start
Starting automount:                                        [  OK  ]
再次尝试登录,出现家目录。
[root@localhost ~]# su - ldapuser1
[ldapuser1@localhost ~]$ pwd
/mnt/ldapusers/ldapuser1

到此,配置完毕。



posted @ 2016-04-18 15:18  hukey  阅读(965)  评论(0编辑  收藏  举报