您的位置:首页 > 运维架构 > Linux

Linux基于LDAP进行用户认证

2017-06-18 22:00 417 查看

一、写在前面

前面我们也讲解了如果安装部署OpenLDAP服务,网上也有有很多Openldap安装部署的文章,往往这些文章都浓墨重彩的描述服务端(Server)的安装和部署,以及排错等过程及说明,但是作为一个C/S架构的软件,可能很多技术牛人认为客户端(Client)的安装就不过如此而已了(虽然有的博客贴出了在桌面环境下设置,不过我觉得生产服务器大部分都是没有桌面环境的),所以大多都寥寥几笔带过,于是就导致配置一个服务端可能有各种教程,反正轻易的就能完成,即使出错也有章可循,而客户端则恰恰相反。然而,配置Openldap的初衷就是用来管理客户端和用户、用户组,既然服务器端已经OK了,那么后续肯定要将众多的Linux服务器加入这个ldap域中来,从而实现统一身份认证,所以别的不多说,以CentOS 6.5为例配置Openldap的客户端。

二、开始配置

在正式配置之前,我们假定已经安装部署好了 OpenLDAP 环境。
1、准备工作
#停止iptables,并查看iptables状态
service iptables stop
service iptables status

#禁用SELinux,并查看SELinux状态
setenforce 0
getenforce

#同步系统时间
ntpdate time.nist.gov

#编辑/etc/hosts,添加openldap server的记录, 推荐
echo "192.168.1.35   ldapsrv01.test.com" >>/etc/hosts
#添加成功后,最好ping一下,看是否能解析正常


2、安装openldap客户端软件包

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


3、配置 /etc/sysconfig/authconfig
/etc/sysconfig/authconfig 文件由 authconfig 包自动创建。 配置该文件的目的是用来跟踪 LDAP 身份认证机制是否正确启用。
cp /etc/sysconfig/authconfig /etc/sysconfig/authconfig.$(date +%F)
sed -i '/USESYSNETAUTH/s/no/yes/' /etc/sysconfig/authconfig
sed -i '/USELDAPAUTH/s/no/yes/' /etc/sysconfig/authconfig
sed -i '/USEMKHOMEDIR/s/no/yes/' /etc/sysconfig/authconfig
sed -i '/PASSWDALGORITHM/s/md5/yes/' /etc/sysconfig/authconfig
sed -i '/USELDAP/s/no/yes/' /etc/sysconfig/authconfig

cat /etc/sysconfig/authconfig | grep yes


4、配置 /etc/nsswith.conf
/etc/nsswith.conf 文件由glibc包自动创建, 该文件用于名称转换服务。通常LINUX系统身份验证读取本地文件,要使身份验证查询通过LDAP服务器必须在该文件中找到passwd; shadow; group;三行在files后空格添加“ldap”
cp /etc/nsswitch.conf /etc/nsswitch.conf.$(date +%F)
sed -i '/^passwd:/s/files/files   ldap/' /etc/nsswitch.conf
sed -i '/^shadow:/s/files/files   ldap/' /etc/nsswitch.conf
sed -i '/^group:/s/files/files   ldap/' /etc/nsswitch.conf

egrep "^passwd|^shadow|^group" /etc/nsswitch.conf


5、配置 /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文件是CentOS的系统认证PAM文件。在该文件的auth,account,password,session四段中pam_unix.so模块后添加pam_ldap.so模块使身份验证先对本地的/etc/passwd文件检查用户帐号,然后再对LDAP服务器进行检查。同时因为是LDAP认证需要为用户创建根目录,所以还必须在会话(SESSION)阶段增加pam_mkhomedir.so模块,为用户登录自动创建宿主目录。# vi /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.soaccount 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.sopassword 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.sosession 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.sosession required pam_mkhomedir.so skel=/etc/skel/ umask=0077
session optional pam_ldap.so

# vi /etc/pam.d/password-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.soaccount 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.sopassword 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.sosession 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.sosession required pam_mkhomedir.so skel=/etc/skel/ umask=0077session optional pam_ldap.so
6、配置 /etc/openldap/ldap.conf
cp /etc/openldap/ldap.conf /etc/openldap/ldap.conf.$(date +%F)
echo "BASE    dc=test,dc=com" >>/etc/openldap/ldap.conf
echo "URI    ldap://192.168.1.35" >>/etc/openldap/ldap


7、使用 ldapsearch命令测试能否读取 openldap server 中的数据

ldapsearch -x -b "dc=test,dc=com"

补充:如果出现“ldap_sasl_bind(SIMPLE): Can't contact LDAP server (-1)”,那么解决该问题的方法是: 1)检查/etc/hosts文件,是否有ldapserver的记录,最好能ping一下,看能否解析成功 2)到openldap server上检查slapd服务是否正在运行,如果没有,启动slapd服务 3)检查/etc/openldap/ldap.conf文件,查看里面的BASE和URI是否正确

8、使用authconfig命令启动 nslcd
authconfig --enableldap --enableldapauth --ldapserver=192.168.1.35 --ldapbasedn="dc=test,dc=com" --enablemkhomedir --update
#该命令要包含ldapserver和basedn的信息


9、服务重启成功后,就可以使用LDAP服务了。
可以通过 su - user_name , 或者 ssh user_name@xxx 来测试。

10、测试ldap用户登录
[root@ldapclient ~]# getent passwd|grep charleslvcharleslv:{SSHA}ao0RHCtIEa8tc/QSt4B+Wt68rBlSeFBo:10000:10000:charles lv:/home/charleslv:/bin/bash[root@ldapclient ~]# id charleslvuid=10000(charleslv) gid=10000(sudoers) groups=10000(sudoers)[root@ldapclient ~]# id zhangsuid=47180(zhangs) gid=10000(sudoers) groups=10000(sudoers)[root@ldapclient ~]# id bryanzuid=1000(bryanz) gid=10000(sudoers) groups=10000(sudoers)[root@ldapclient ~]# id ericliuid=10001(ericli) gid=10000(sudoers) groups=10000(sudoers)[root@ldapclient ~]# su - zhangsCreating directory '/home/zhangs'.[zhangs@ldapclient ~]$ lltotal 0[zhangs@ldapclient ~]$ pwd/home/zhangs
11、通过NFS实现LDAP用户/home的自动挂载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息