内容简介

      在centos6系统中安装配置openldap服务,通过migrationtools工具生成基于linux系统账户相关的ldap账户信息以及通过nfs共享方式在客户端自动挂接ldap用户的家目录


环境准备

1,准备两台及以上centos6操作系统主机,一台用于部署服务端其它用于部署客户端接入ldap认证 

2,关闭selinux

3,清空iptables规则 

4,配置好yum源,推荐使用阿里云yum源


ldap服务端安装与配置

[[email protected] ~]# yum list | grep openldap
[[email protected] ~]# yum install openldap openldap-clients openldap-servers openldap-devel -y

#openldap配置主目录在/etc/openldap下,默认文件目录如下

[[email protected] ~]# ll /etc/openldap/
total 28
drwxr-xr-x 2 root root 4096 Jan 10 11:07 certs
-rw-r----- 1 root ldap  121 Nov 10  2015 check_password.conf
-rw-r--r-- 1 root root  280 Nov 10  2015 ldap.conf
drwxr-xr-x 2 root root 4096 Jan 10 11:07 schema
drwx------ 2 ldap ldap 4096 Jan 10 11:10 slapd.d

#生成hash密码稍后用于配置ldap管理员密码,此处设置的密码为123456

[[email protected] ~]# slappasswd 
New password: 
Re-enter new password: 
{SSHA}SpiJ+uf0d1j7bRm6XoLJo9EX3Gk5uzzH

#拷贝服务端配置模版至/etc/openldap并配置,主要修改dc=my-domain,dc=com为自己的域名以及配置rootpw用于后续导入系统帐号信息,如下图所示

[[email protected] ~]# /bin/cp /usr/share/openldap-servers/slapd.conf.obsolete /etc/openldap/slapd.conf

ldap 安装

参考与

ldap 安装


#拷贝DB_SAMPLE模版文件文件至/var/lib/ldap

[[email protected] ~]# /bin/cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
[[email protected] ~]# chown ldap. /var/lib/ldap/ -R

#删除默认的ldap配置信息并重新生成相关配置

[[email protected] ~]# rm -rf /etc/openldap/slapd.d/*
[[email protected] ~]# slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d
config file testing succeeded
[[email protected] ~]# chown ldap. /etc/openldap/ -R

至此服务配置已经完成,启动ldap服务

[[email protected] ~]# service slapd start

Starting slapd:                                            [  OK  ]

[[email protected] ~]#


生成系统账户并导入ldap

创建/home/ldapuser为ldap用户家目录的上级目录

创建teacher及student组,gid分别为2000和3000(根据实际情况而定) 生成user01-user10归属teacher组

生成user11-users0归属student组


[[email protected] ~]# mkdir -p /home/ldapuser
[[email protected] ~]# groupadd teacher -g 2000
[[email protected] ~]# groupadd student -g 3000
[[email protected] ~]# for i in {01..10};do useradd -g teacher -d /home/ldapuser/user$i user$i;done
[[email protected] ~]# for i in {11..20};do useradd -g student -d /home/ldapuser/user$i user$i;done
[[email protected] ~]# for i in {01..20};do echo user$i | passwd --stdin user$i;done

提取上述用户在/etc/passwd及/etc/group中的信息至临时文件


[[email protected] ~]# mkdir /tmp/ldap
[[email protected] ~]# cat /etc/passwd | grep -E 'user[0-9]{2}' > /tmp/ldap/passwd
[[email protected] ~]# cat /etc/group | grep -E 'teacher|student' > /tmp/ldap/group
[[email protected] ~]# cat /tmp/ldap/passwd


安装migrationtools工具生成相应的ldap信息文件

[[email protected] ~]# yum install migrationtools -y


修改/usr/share/migrationtools/migrate_common.ph文件第71和74行信息为自己对应的域名信息,如下图所示

[[email protected] ~]#vim /usr/share/migrationtools/migrate_common.ph

ldap 安装

注意:

        根据自己的域名修改

通过migrate_bash.pl生成帐号基础信息文件

通过migrate_passwd.pl和/tmp/ldap/passwd文件生成用户信息文件

通过migrate_group.pl和/tmp/ldap/group文件生成用户信息文件

[[email protected] ~]# /usr/share/migrationtools/migrate_base.pl > /tmp/ldap/base.ldif
[[email protected] ~]# /usr/share/migrationtools/migrate_passwd.pl /tmp/ldap/passwd > /tmp/ldap/passwd.ldif
[[email protected] ~]# /usr/share/migrationtools/migrate_group.pl /tmp/ldap/group > /tmp/ldap/group.ldif

通过ldapadd命令添加上述生成的信息,主要用过下述参数;其中

-D 参数即为/etc/openldap/slapd.conf中定义的rootdn信息

-w password 中的password为上述使用slappasswd命令时的明文密码(能从命令行看见),不能与-W同时使用

-W 后不跟参数密码在敲下命令后根据提示输入(别人看不见),不能与-w同时使用

-x 使用不加密的协议与ldap服务端通信

-X 使用加密的协议与ldap服务端通信

-f 指定要添加的ldif文件路径

[[email protected] ~]#ldapadd -D "cn=admin,dc=bj,dc=uplooking,dc=com" -W -x -f /tmp/ldap/base.ldif
[[email protected] ~]#ldapadd -D "cn=admin,dc=bj,dc=uplooking,dc=com" -W -x -f /tmp/ldap/passwd.ldif
[[email protected] ~]#ldapadd -D "cn=admin,dc=bj,dc=uplooking,dc=com" -W -x -f /tmp/ldap/group.ldif

注意:

        如果有密码可以这样执行

       ldapadd -D "cn=admin,dc=bj,dc=uplooking,dc=com" -w 123456 -f /tmp/ldap/group.ldif


通过ldapsearch查询ldap中的用户组信息

[[email protected] ~]#ldapsearch -b "dc=bj,dc=uplooking,dc=com" -x
或者
[[email protected] ~]# ldapsearch -b "ou=Group,dc=uplooking,dc=com" -x

安装nfs设置ldap用户家目录为共享

[[email protected] ~]# yum install nfs-utils -y
[[email protected] ~]# cat /etc/exports
/home/ldapuser *(rw,no_root_squash)
[[email protected] ~]# service rpcbind start
Starting rpcbind:                                          [  OK  ]
[[email protected] ~]# service nfs start
Starting NFS services:                                     [  OK  ]
Starting NFS quotas:                                       [  OK  ]
Starting NFS mountd:                                       [  OK  ]
Starting NFS daemon:                                       [  OK  ]
[[email protected] ~]# chkconfig nfs on
[[email protected] ~]# chkconfig rpcbind on
#配置防火墙
vi /etc/sysconfig/iptables
-A INPUT –p tcp –m state --state NEW –m tcp --dport 111 –j ACCEPT
-A INPUT –p udp –m state --state NEW –m udp --dport 111 –j ACCEPT
-A INPUT –p tcp –m state --state NEW –m tcp --dport 662 –j ACCEPT
-A INPUT –p udp –m state --state NEW –m udp --dport 662 –j ACCEPT
-A INPUT –p tcp –m state --state NEW –m tcp --dport 892 –j ACCEPT
-A INPUT –p udp –m state --state NEW –m udp --dport 892 –j ACCEPT
-A INPUT –p tcp –m state --state NEW –m tcp --dport 2049 –j ACCEPT
-A INPUT –p udp –m state --state NEW –m udp --dport 2049 –j ACCEPT
-A INPUT –p tcp –m state --state NEW –m tcp --dport 32803 –j ACCEPT
-A INPUT –p udp –m state --state NEW –m udp --dport 32769 –j ACCEPT

ldap客户端安装与配置

安装openldap客户端软件包并配置/etc/openldap/ldap.conf

[[email protected] ~]# yum install openldap-clients nss-pam-ldapd pam_ldap -y
[[email protected] ~]# vim /etc/openldap/ldap.conf 
#
# LDAP Defaults
#
# See ldap.conf(5) for details
# This file should be world readable but not world writable.
#BASE   dc=example,dc=com
#URI    ldap://ldap.example.com ldap://ldap-master.example.com:666
BASE    dc=uplooking,dc=com
URI     ldap://192.168.92.130   #此处为ldap服务端ip
#SIZELIMIT      12
#TIMELIMIT      15
#DEREF          never
TLS_CACERTDIR   /etc/openldap/certs


#测试搜索ldap中的用户组信息

[[email protected] ~]# ldapsearch -h 192.168.30.192 -x -b "ou=Group,dc=uplooking,dc=com"


配置客户端启动ldap验证

配置/etc/sysconfig/authconfig

[[email protected] ~]# sed -i '/USESYSNETAUTH/s/no/yes/' /etc/sysconfig/authconfig
[[email protected] ~]# sed -i '/USELDAPAUTH/s/no/yes/' /etc/sysconfig/authconfig
[[email protected] ~]# sed -i '/USEMKHOMEDIR/s/no/yes/' /etc/sysconfig/authconfig
[[email protected] ~]# sed -i '/PASSWDALGORITHM/s/md5/yes/' /etc/sysconfig/authconfig
[[email protected] ~]# sed -i '/USELDAP/s/no/yes/' /etc/sysconfig/authconfig


配置/etc/nsswitch.conf,如下图所示

ldap 安装

配置pam新增ldap认证

[[email protected] ~]# vim /etc/pam.d/system-auth
#%PAM-1.0
# This file is auto-generated.
# User changes will be destroyed the next time authconfig is run.
auth        required      pam_env.so
auth        sufficient    pam_fprintd.so
auth        sufficient    pam_unix.so nullok try_first_pass
auth        requisite     pam_succeed_if.so uid >= 500 quiet
auth        sufficient    pam_ldap.so
auth        required      pam_deny.so
account     required      pam_unix.so
account     sufficient    pam_localuser.so
account     sufficient    pam_succeed_if.so uid < 500 quiet
account     [default=bad success=ok user_unknown=ignore] pam_ldap.so
account     required      pam_permit.so
password    requisite     pam_cracklib.so try_first_pass retry=3 type=
password    sufficient    pam_unix.so sha512 shadow nullok try_first_pass use_authtok
password    sufficient    pam_ldap.so  use_authtok
password    required      pam_deny.so
session     optional      pam_keyinit.so revoke
session     required      pam_limits.so
session     [success=1 default=ignore] pam_succeed_if.so service in crond quiet use_uid
session     required      pam_unix.so
session     required      pam_mkhomedir.so skel=/etc/skel/ umask=0077
session     optional      pam_ldap.so

ldap 安装

使用autoconfig命令配置nscd服务,注意替换命令中的ldapserver的IP及basedb信息

[[email protected] ~]# authconfig --enableldap --enableldapauth --ldapserver=192.168.92.130 --ldapbasedn="dc=uplooking,dc=com" --enablemkhomedir --update
Starting nslcd:                                            [  OK  ]
[[email protected] ~]#service nslcd start

或者

 

#在字符界面下执行
[[email protected] ~]#authconfig-tui
1,步

ldap 安装

2,

ldap 安装

3,启动服务

[[email protected] ~]#service nslcd start

检查:

authconfig-tui会修改很多配置文件,先来检查下

[[email protected] ~]# grep -v "#" /etc/nslcd.conf |grep -v "^$"

ldap 安装


[[email protected] ~]# grep -v "#" /etc/nslcd.conf |grep -v "^$"

ldap 安装


[[email protected] ~]# grep -v "#" /etc/pam_ldap.conf |grep -v "^$"

ldap 安装


[[email protected] ~]#  grep -v "#" /etc/nsswitch.conf |grep -v "^$"

ldap 安装


[[email protected] ~]# grep "USELDAP" /etc/sysconfig/authconfig

ldap 安装

安装autofs挂载ldapserver端的用户目录;按图示操作添加相关配置

[[email protected] ~]# yum install autofs -y
[[email protected] ~]# vim /etc/auto.master 
#
# Sample auto.master file
# This is a 'master' automounter map and it has the following format:
# mount-point [map-type[,format]:]map [options]
# For details of the format look at auto.master(5).
#
/misc   /etc/auto.misc
/home/ldapuser  /etc/auto.ldapuser
#
# NOTE: mounts done from a hosts map will be mounted with the
#       "nosuid" and "nodev" options unless the "suid" and "dev"
#       options are explicitly given.
#
/net    -hosts
#
# Include central master map if it can be found using
# nsswitch sources.
#
# Note that if there are entries for /net or /misc (as
# above) in the included master map any keys that are the
# same will not be seen as the first read key seen takes
# precedence.
#
+auto.master

ldap 安装

[[email protected] ~]# vim /etc/auto.ldapuser
*               -rw,soft,intr          192.168.92.130:/home/ldapuser/&
[[email protected] ~]# service autofs start
Loading autofs4:                                           [  OK  ]
Starting automount:                                        [  OK  ]
[[email protected] ~]# ls /home/ldapuser/
[[email protected] ~]# cd /home/ldapuser/user01
[[email protected] user01]# pwd
/home/ldapuser/user01
[[email protected] ~]#

测试使用ldapuser登录

ldap 安装