公司需要新配置ldap服务器,在配置时遇到了以下问题,当服务端用centos6.0及以上版本,客户端用centos5.4版本,假如其中一个客户端的ip地址是172.16.16.31,当用server端设定用户登陆172.16.16.31时,通过在server端配置,能够自动为用户创建家目录;但是如果客户端的机器是用centos6.0及以上版本的话,就不能为登录用户自动创建家目录,不知道这个是为什么?还有就是用户的密码如果采用MD5SHA1加密的话,用户不能登录进去,只能采用crypt加密方式,如下为具体的配置:<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

一、ldap服务端配置:

#yum -y install openldap openldap-clients openldap-servers db4-* perl-*

然后在/etc/openldap下应该有一个slapd.conf。如果该目录下没有这个文件,则用find查找,找到这个文件然后拷贝到/etc/openldap目录下,打开这个文件,然后修改suffix为你的公司或域名的DNS表示,如我测试用example.com,那么这里便可以写成"dc=example,dc=com"rootdn则是openLDAP中最高权限用户,在这里我们把它写成"cn=root,dc=example,dc=com"。当然你也可以取其它的名字如"cn=Manager,dc=example,dc=com",但是cn=username后面那一串一定要和你的suffix一致。

接下来要设置rootdn的密码,如果你不希望密码使用明文保存的话,最好是用slappasswd来生成一个。直接运行slappasswd命令,然后两次键入想要生成的密码,一个加密后的字符串便显示了出来。使用任何办法复制这个字符串,并重新打开slapd.conf,在rootdn下一行添加

rootpw 刚才的字符串

保存退出即完成了密码的配置。在启动openLDAP服务器之前,把DB_CONFIG.example拷贝为/var/lib/ldap/DB_CONFIG,这个文件你可以用find命令来查找,而在我这里则是

#cp /usr/share/doc/openldap-servers-2.4.19/DB_CONFIG.example /var/lib/ldap/DB_CONFIG

#chown -R ldap:ldap /var/lib/ldap/

#chmod 700 /var/lib/ldap

然后,还要把/etc/openldap/slapd.d这个文件夹删除或者改名,否则slapd会无视你的slapd.conf配置文件的!

好了,可以启动LDAP服务器了,运行"service slapd start"。启动起来后,就可以添加记录文件openldap.ldif(特别要注意格式,每段红色部分在冒号之后有个空格,)

dn: dc=example,dc=com // 表示建立初始化记录文件,也相当于根域

dc: example

objectClass: top

objectClass: domain

dn: ou=People,dc=example,dc=com // 表示建立用户

ou: People

objectClass: top

objectClass: organizationalUnit

dn: ou=Group,dc=example,dc=com // 建立群组

ou: Group

objectClass: top

objectClass: organizationalUnit

dn: cn=IT,ou=Group,dc=example,dc=com // 表示在Group群下建立IT这个成员组

gidNumber: 61

userPassword:: e2NyeXB0fXg=

objectClass: posixGroup

objectClass: top

cn: IT

dn: cn=CC,ou=Group,dc=example,dc=com // 表示在Group群下建立CC这个成员组

gidNumber: 62

userPassword:: e2NyeXB0fXg=

objectClass: posixGroup

objectClass: top

cn: CC

dn: uid=leo,ou=People,dc=example,dc=com // 表示建立的leo这个用户属于People这个组且属于IT成员组

uid: leo
cn: leo
objectClass: account

objectClass: posixAccount

objectClass: top

userpassword: {crypt}izU9XMB+E1d2CHEfRgL8tw==

loginShell: /bin/bash

uidNumber: 600

gidNumber: 61

homeDirectory: /home/leo
dn: uid=tom,ou=People,dc=example,dc=com // 表示建立的tom这个用户属于People这个组且属于IT成员组

uid: tom
cn: tom
objectClass: account

objectClass: posixAccount

objectClass: top

userpassword: {crypt}izU9XMB+E1d2CHEfRgL8tw==

loginShell: /bin/bash

uidNumber: 601

gidNumber: 61

homeDirectory: /home/tom
保存后导入ldap,使用ldapadd命令

#cd /etc/openldap/

# ldapadd -x -D "cn=root,dc=example,dc=com" -W -f ./openldap.ldif ,执行后,会让你输入密码,这个密码就是在slapd.conf中那个密码,导入。

可以通过以下这个命令来查看导入情况

#ldapsearch -x -b 'dc=example,dc=com'

要增加用户到People下且属于CCIT或其他成员组,按照上面的配置文件来添加就可以,只是要注意格式,添加后导入ldap即可,已经添加了的不能再添加。这样ldap服务端就配置好了。

二、客户端配置(以centos 5.4为例 )

服务端配置好后,接下来配置客户端,也就是客户想要访问的主机。把用户配置在ldap服务器上,在客户机上做好相应的配置后,当用户需要去访问某台客户机时,在该客户机上输入密码后,会到ldap服务器来做验证。

LDAP 服务器的客户端命令RPM包为openldap-clients,或用yum -y install openldap-clients安装,安装后会有

/etc/openldap/ldap.conf 文件。 除此之外要实现LDAP服务客户端必须配置

/etc/nsswitch.conf, /etc/sysconfig/authing, /etc/openldap/ldap.conf, /etc/ldap.conf

/etc/pam.d/system-auth 五个文件,其中/etc/ldap.conf这个配置文件是nss_ldap这个软件生成的,另外需要的nss_ldapnscd两个软件在系统安装时就会自动安装。

接下来配置这些文件:

<1> 配置/etc/nsswith.conf

/etc/nsswith.conf 文件由glibc-2.5-24生成,CentOS5.2中缺省安装。该文件用

于名称转换服务。通常LINUX系统身份验证读取本地文件,要使身份验证查询

通过LDAP服务器必须在该文件中找到passwd;shadow;group;三行在files后空格添加ldap

passwd: files ldap

shadow: files ldap

group: files  ldap

<2> 配置/etc/sysconfig/authconfig文件提供身份验证支持LDAP功能

/etc/sysconfig/authconfig 文件由authconfig-5.3.21-3.e15RPM包生成系统默认安装。配置该文件用来跟踪LDAP身份认证机制是否正确启用。找到以下七行,

将值确定为“yes”。

USESYSNETAUTH=yes

USESHADOW=yes

USELOCAUTHORIZE=yes

USELDAP=yes

USELDAPAUTH=yes

USEMKHOMEDIR=yes

PASSWDALGORITHM=yes

<3> 配置/etc/pam.d/system-auth文件

身份验证服务是实际向LDAP验证用户身份的服务。可插入身份验证模块(PAM)提供了本地Linux身份验证服务。pam_unix.so模块是通用模块,使PAM机制对本地的/etc/passwd文件检查用户帐号。PAMLDAP模块可以用来将身份验证重定向到LDAP目录上。身份验证本身是由PAM程序执行的,它从身份验证候选机制中获取用户名,将其绑定到openLDAP 服务器上。如果绑定成功,PAM会报告说这个用户已经成功通过了pam_ldap.so提供的身份验证测试。根据PAM的配置不同,在用户看到命令提示符之前可能会执行其它测试。 /etc/pam.d/system-auth 文件是 CentOS5.2 的系统认证 PAM 文件。在该文件的 auth,account,password,session 四段中 pam_unix.so 模块后添加 pam_ldap.so 模块使身份验证先对本地的 /etc/passwd 文件检查用户帐号,然后再对 LDAP 服务器进行检查。同时因为是 LDAP 认证需要为用户创建根目录,所以还必须在会话( SESSION )阶段增加 pam_mkhomedir.so 模块,为用户登录自动创建宿主目录。

#cp /etc/pam.d/system-auth /etc/pam.d/system-auth.old

* 首先备份系统认证文件,同时PAM配置文件至关重要,稍有差池,用户可能就不能登录,所以需开两个控制台调试以防万一。

我的配置如下:

#%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_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 broken_shadow

account sufficient pam_succeed_if.so uid < 500 quiet

account sufficient pam_ldap.so

account required pam_permit.so

password requisite pam_cracklib.so try_first_pass retry=3

password sufficient pam_unix.so md5 shadow nullok try_first_pass use_authtok

password sufficient pam_ldap.so use_authtok md5

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=0022

session optional pam_ldap.so<4>/etc/openldap/ldap.conf.( 该文件可不配置)

该文件是LDAP服务器的客户端搜索工具文件,由openldap_clientsRPM包生成。

配置如下:

BASE dc=boy dc=com   -> 搜索路径

URI ldap:// 主服务器名 ( ), ldap:// 辅助服务器名 ( )

我的配置如下:

BASE dc=example,dc=com

URI ldap://172.16.16.222:389/

<5>/etc/ldap.conf 文件。

该文件也是LDAP服务器客户端文件,但是与/etc/openldap/ldap.conf文件有不同功能,两者不可混淆。该文件由nss_ldap-253-12.e15RPM生成,系统默认安装。/lib/security/pam_ldap.so也是由该RPM包生成。

Rpm nss_ldap-253说明

nss_ldap-253: 包括两个LDAP访问客户机:nss_ldappam_ldapnss_ldap是一组C库扩展,提供系统命名服务(NSS),也叫名称转换服务。需要配置为使用LDAP来解析诸如用户和组帐号资源。

#vi /etc/ldap.conf shift+g跳到最后,加上下面几句:

uri ldap://172.16.16.223:389/

ssl off

tls_cacertdir /etc/openldap/cacerts

pam_password crypt
<6> 开启名称缓存服务 ns cd.

通过网络方式查询用户占用带宽且有时延,开启名称缓存服务可以节省网络资源提高查询效率。

service nscd restart

chkconfig --level 3 5 nscd on

现在可以在客户端进行登录认证调试。

在客户端使用getent passwd, getent group 命令会显示所有的用户和组包括本地/etc/passwd/下的和LDAP服务器端数据库上的。

至此在centos5.4版本测试上成功。

三、客户端配置:以centos 6.0版本为例

#yum -y install openldap-clients nss-pam-ldapd

#vi /etc/openldap/ldap.conf

# add at the last line

# LDAP server's URI

#URI ldap://10.0.0.39/

# specify Suffix
#BASE dc=server,dc=world
#TLS_CACERTDIR /etc/openldap/cacerts

BASE dc=idc,dc=timanetwork,dc=com

URI ldap://172.16.16.223:389

ssl no

tls_cacertdir /etc/openldap/cacerts

pam_password crypt

#vi /etc/nslcd.conf 跳到最后加上如下内容

# line 131: specify URI, Suffix

uri ldap://172.16.16.223:389/
base dc=idc,dc=timanetwork,dc=com

ssl no
tls_cacertdir /etc/openldap/cacerts

#vi /etc/pam_ldap.conf 跳到最后加上如下内容

# line 17: make it comment

#host 127.0.0.1

# line 20: specify Suffix

base dc=server,dc=world

# add at the last line

uri ldap://172.16.16.223:389/
ssl no
tls_cacertdir /etc/openldap/cacerts
pam_password crypt
#vi /etc/pam.d/system-auth

# add like follows

#%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 use_first_pass
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 md5 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 optional pam_ldap.so

session optional pam_mkhomedir.so skel=/etc/skel umask=077

#vi /etc/nsswitch.conf

passwd   files  ldap

shadow:   files  ldap

group:    files  ldap

netgroup:  ldap

automount:files  ldap

#vi  /etc/sysconfig/authconfig

useldap=yes

#service nslcd start

#chkconfig nslcd on

#shutdown -r now

#service nscd stop  // 这个服务需要停掉,不然不能访问

至此在centos6.0上客户端配置好了,但是当用户去访问这台机器时,能够登录进去,但是不能自动创建家目录。由于公司现在很多机器都是安装的centos6.0redhat6.0版本,不能自动创建家目录,所以我只有用nfs文件共享/home目录,具体方法如下:

ldap服务端配置nfs服务器,步骤如下:

#yum -y install nfs-utils

#vi /etc/idmapd.conf   加入

/home *(rw,sync,no_root_squash,no_all_squash) 保存。解释一下意思

/home              共享的目录

*                 表示允许所有来共享

rw                 表示可写

no_root_squash    表示有root特权

no_all_squash      表示能够用户认证

#/etc/init.d/rpcbind start

#/etc/init.d/nfslock start

#/etc/init.d/nfs start

#chkconfig rpcbind on

#chkconfig  nfslock on

#chkconfig  nfs on

这样就可以在/home目录下手动建立要登陆的用户的家目录。

客户端配置nfs

#yum -y install nfs-utils

# /etc/init.d/rpcbind start

#/etc/init.d/nfslock start

#/etc/init.d/netfs start

#chkconfig rpcbind on

#chkconfig nfslock on

#chkconfig netfs on

/etc/fstab中增加一行:

172.16.16.223:/home /home nfs defaults 1 1

#mount -t nfs 172.16.16.223:/home /home

这样当用户登录进去后,就可以共享server端手动建立的家目录,但是如果用户过多的话,不是很方便,不知道有没有哪位朋友遇到过这种情况,如果有的话,请给些建议,谢谢!