Linux基於LDAP進行用戶認證

1、寫在前面

    前面咱們也講解了若是安裝部署OpenLDAP服務,網上也有有不少Openldap安裝部署的文章,每每這些文章都濃墨重彩的描述服務端(Server)的安裝和部署,以及排錯等過程及說明,可是做爲一個C/S架構的軟件,可能不少技術牛人認爲客戶端(Client)的安裝就不過如此而已了(雖然有的博客貼出了在桌面環境下設置,不過我以爲生產服務器大部分都是沒有桌面環境的),因此大多都寥寥幾筆帶過,因而就致使配置一個服務端可能有各類教程,反正輕易的就能完成,即便出錯也有章可循,而客戶端則偏偏相反。然而,配置Openldap的初衷就是用來管理客戶端和用戶、用戶組,既然服務器端已經OK了,那麼後續確定要將衆多的Linux服務器加入這個ldap域中來,從而實現統一身份認證,因此別的很少說,以CentOS 6.5爲例配置Openldap的客戶端。bash


2、開始配置

在正式配置以前,咱們假定已經安裝部署好了 OpenLDAP 環境。服務器

一、準備工做session

#中止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一下,看是否能解析正常


二、安裝openldap客戶端軟件包
架構

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


三、配置 /etc/sysconfig/authconfigssh

/etc/sysconfig/authconfig 文件由 authconfig 包自動建立。 配置該文件的目的是用來跟蹤 LDAP 身份認證機制是否正確啓用。ide

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


四、配置 /etc/nsswith.conf測試

/etc/nsswith.conf 文件由glibc包自動建立, 該文件用於名稱轉換服務。一般LINUX系統身份驗證讀取本地文件,要使身份驗證查詢經過LDAP服務器必須在該文件中找到passwd; shadow; group;三行在files後空格添加「ldap」ui

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


五、配置 /etc/pam.d/system-authspa

    身份驗證服務是實際向LDAP驗證用戶身份的服務。可插入身份驗證模塊(PAM)提供了本地Linux身份驗證服務。pam_unix.so模塊是通用模塊,使用PAM機制對本地的/etc/passwd文件檢查用戶賬號。PAMLDAP模塊能夠用來將身份驗證重定向到LDAP目錄上。身份驗證自己是由PAM程序執行的,它從身份驗證候選機制中獲取用戶名,將其綁定到openLDAP 服務器上。若是綁定成功,PAM會報告說這個用戶已經成功經過了pam_ldap.so提供的身份驗證測試。根據PAM的配置不一樣,在用戶看到命令提示符以前可能會執行其它測試。unix

    /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.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


# 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.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


六、配置 /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


七、使用 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是否正確


八、使用authconfig命令啓動 nslcd

authconfig --enableldap --enableldapauth --ldapserver=192.168.1.35 --ldapbasedn="dc=test,dc=com" --enablemkhomedir --update
#該命令要包含ldapserver和basedn的信息


九、服務重啓成功後,就可使用LDAP服務了。

能夠經過 su - user_name ,  或者  ssh  user_name@xxx 來測試。


十、測試ldap用戶登陸

[root@ldapclient ~]# getent passwd|grep charleslv

charleslv:{SSHA}ao0RHCtIEa8tc/QSt4B+Wt68rBlSeFBo:10000:10000:charles lv:/home/charleslv:/bin/bash

[root@ldapclient ~]# id charleslv

uid=10000(charleslv) gid=10000(sudoers) groups=10000(sudoers)

[root@ldapclient ~]# id zhangs

uid=47180(zhangs) gid=10000(sudoers) groups=10000(sudoers)

[root@ldapclient ~]# id bryanz

uid=1000(bryanz) gid=10000(sudoers) groups=10000(sudoers)

[root@ldapclient ~]# id ericli

uid=10001(ericli) gid=10000(sudoers) groups=10000(sudoers)

[root@ldapclient ~]# su - zhangs

Creating directory '/home/zhangs'.

[zhangs@ldapclient ~]$ ll

total 0

[zhangs@ldapclient ~]$ pwd

/home/zhangs


十一、經過NFS實現LDAP用戶/home的自動掛載

相關文章
相關標籤/搜索