centos 6.5 配置LDAP服務器+客戶端!


各種度娘!各種歌哥!網上教程參差不齊,歷時1天,終於完成,不敢獨享,遂,總結分享之,有問題可以留言,知無不言。。。開始吧

Note:

本次配置的服務器環境是<redhat enterprise linux 6.5>, Centos/Suse類似, Debian/Ubuntu安裝包可能略有不同,但是大致過程應該相似。

PS:LDAP 是C/S結構的,所以接下來我們要設計好哪台機器上搭建LDAP Server,剩下的機器上安裝LDAP Client.

LDAP Server Machine:192.168.1.10 (此IP為機器的實際IP,自己配置時根據實際情況而定)

LDAP Client   Machine:192.168.1.11 (此IP為機器的實際IP,自己配置時根據實際情況而定)

LDAP Client Machine N...... (其余N多的client機器不一一列舉,完成一個client配置后,其他機器Clone整個過程即可 :-))


××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××

 

1.LDAP Server Setup:
a.安裝LDAP服務(使用YUM本地光盤安裝)

rpm -qa | grep openldap

查看是否包含這四個包:

openldap-devel-2.4.23-26.el6.x86_64
openldap-clients-2.4.23-26.el6.x86_64
openldap-2.4.23-26.el6.x86_64
openldap-servers-2.4.23-26.el6.x86_64

若不包含,那就需要安裝了。保證你機器能連外網,然后運行如下命令:

# yum install openldap-* -y
會提示安裝以下幾個必須的包,另外鑒於依賴,可能還會安裝一些其他的包,所以我們選擇-y:


openldap-devel-2.4.23-26.el6.x86_64
openldap-clients-2.4.23-26.el6.x86_64
openldap-2.4.23-26.el6.x86_64
openldap-servers-2.4.23-26.el6.x86_64
....

OpenLDAP 使用Berkely-DB來作為數據庫存儲信息,我們可以去官網下載解壓到本地安裝。But,用Yum的話,應該會幫我們做好這一切事情。 我們只要在安裝完后檢查一下是否安裝了db4*相關的rpm包就可以了。If true,那恭喜,我們可以進行下一步了。If not,請用yum命令安裝。

查看rpm安裝包的命令:rpm -qa | grep db4

b.拷貝LDAP配置文件到LDAP目錄(redhat6.3):
# cd /etc/openldap/
# cp /usr/share/openldap-servers/slapd.conf.obsolete slapd.conf


Note: redhat6.0或6.1版本配置文件在主目錄有備份:

# cd /etc/openldap/
# cp slapd.conf.bak slapd.conf


c.創建LDAP管理員密碼:
# slappasswd
這里我輸入的密碼是redhat,輸入完密碼后,返回一串密文,先保存到剪貼板,之后要復制到LDAP配置文件中使用:
{SSHA}pfAJm+JJa4ec2y8GjTc8uMEJpoR5YKLy


d.編譯配置文件
# vi /etc/openldap/slapd.conf
....

# enable server status monitoring (cn=monitor)
database monitor
access to *
        by dn.exact="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth" read
        by dn.exact="cn=admin,dc=test,dc=com" read
        by * none


#######################################################################
# database definitions
#######################################################################


database        bdb
suffix          "dc=test,dc=com"
checkpoint      1024 15
rootdn          "cn=admin,dc=test,dc=com"
# Cleartext passwords, especially for the rootdn, should
# be avoided.  See slappasswd(8) and slapd.conf(5) for details.
# Use of strong authentication encouraged.
# rootpw                secret
# rootpw                {crypt}ijFYNcSNctBYg
rootpw {SSHA}pfAJm+JJa4ec2y8GjTc8uMEJpoR5YKMn 

.......

保存退出。

e.拷貝DB_CONFIG文件到指定目錄
# cp /usr/share/openldap-servers/DB_CONFIG.example  /var/lib/ldap/DB_CONFIG
刪除默認/etc/openldap/slapd.d下面的所有內容,否則后面在使用ldapadd的時候會報錯:
# rm -rf /etc/openldap/slapd.d/*
f.啟動LDAP的slapd服務,並設置自啟動:
# service slapd restart
# chkconfig slapd on
賦予配置目錄相應權限:
# chown -R ldap:ldap /var/lib/ldap
# chown -R ldap:ldap /etc/openldap/

g.測試並生成配置文件:
slaptest  -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d
返回config file testing succeeded,則配置成功。
賦予生成的配置文件予權限並重啟:
# chown -R ldap:ldap /etc/openldap/slapd.d
# service slapd restart
h.創建一個賬號,以備客戶端測試登陸
# useradd ldapuser1
# passwd ldapuser1
至此,這些用戶僅僅是系統上存在的用戶(存儲在/etc/passwd和/etc/shadow上),並沒有在LDAP數據庫里,所以要把這些用戶導入到LDAP里面去。但LDAP只能識別特定格式的文件 即后綴為ldif的文件(也是文本文件),所以不能直接使用/etc/passwd和/etc/shadow。 需要migrationtools這個工具把這兩個文件轉變成LDAP能識別的文件。
i.安裝配置migrationtools
# yum install migrationtools -y

j.進入migrationtool配置目錄
# cd /usr/share/migrationtools/
首先編輯migrate_common.ph

# vi  migrate_common.ph

...

# Default DNS domain
$DEFAULT_MAIL_DOMAIN = "test.com";


# Default base
$DEFAULT_BASE = "dc=test,dc=com";

.......

保存退出。:-)

K.下面利用pl腳本將/etc/passwd 和/etc/shadow生成LDAP能讀懂的文件格式,保存在/tmp/下
# ./migrate_base.pl > /tmp/base.ldif
# ./migrate_passwd.pl  /etc/passwd > /tmp/passwd.ldif
# ./migrate_group.pl  /etc/group > /tmp/group.ldif
L.下面就要把這三個文件導入到LDAP,這樣LDAP的數據庫里就有了我們想要的用戶
# ldapadd -x -D "cn=admin,dc=example,dc=com" -W -f /tmp/base.ldif
# ldapadd -x -D "cn=admin,dc=example,dc=com" -W -f /tmp/passwd.ldif
# ldapadd -x -D "cn=admin,dc=example,dc=com" -W -f /tmp/group.ldif
過程若無報錯,則LDAP服務端配置完畢

M.重啟slapd完成配置
# service slapd restart

N.現安裝NFS,並把ldapuser1的家目錄做NFS共享.
默認REDHAT已安裝
# yum install nfs* -y
配置NFS共享:
# vi /etc/exports
--------------
/home/ldapuser1         *(rw,no_root_squash)
--------------
重啟nfs服務:
# service rpcbind restart
# service nfs restart
PS.本地需要做ldap控制的賬號都要導入到LDAP DB中,否則客戶端配置無法正常識別登錄。

 

2.LDAP Client Setup:


a.安裝LDAP client認證需要的pam包:
yum install nss-pam-ldapd pam_ldap -y 

安裝完成后,配置ldap 客戶端配置文件:
vim /etc/openldap/ldap.conf

添加

BASE:dc=test,dc=com

URL:192.168.1.10    --->此處為LDAP Server IP

 

b.配置客戶端,是機器使用LDAP進行賬戶驗證:
LANG=C authconfig-tui


vim /etc/pam.d/system-auth   注釋掉創建家目錄,重啟sssd服務。


c. vim /etc/sssd/sssd.conf 
 添加enumerate=true,(因該不是必須的,但是加上為妙)


d. service sssd restart        並重啟服務。        
e.進行客戶端測試,看能否讀到LDAP user 相關信息
 getent passwd |grep ldapuser1
 

f.編輯系統認證文件,保證使用LDAP來認證:
 vim /etc/pam.d/system-auth           update: pam_sss.so  --> pam_ldap.so  可以實現用戶su之間切換 
 ******Please Makre sure all this updated*****************************************************************
 其中/etc/pam.d/
 
 vim /etc/pam.d/system-auth
 vim /etc/pam.d/password-auth
 

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


 配置文件是RHEL6實現用戶名驗證機制配置文件,大多數服務的配置將都會調用它們。
 vim /etc/pam.d/system-auth           update: pam_sss  --> pam_ldap  可以實現用戶su之間切換
 *********************************************************************************************************

 service nslcd restart

服務重啟之后因該就可以使用LDAP 服務了。

可以 su ldapsuer1或者ssh ldapuser1@xxxxx來使用。

 

客戶端配置完成!

 

3. LDAP數據庫相關增刪查改操作:
操作LDAP的方式有很多,下面以bash下用openldap-clients 套件提供的工具來進行。

 

1、初始化數據
這一步通常是由特定的應用程序根據其自身使用的需要創建的。包括兩部分的內容:
a、創建.schema文件,該文件定義了后面ldap 中存放對象的類型和屬性,這已經在上面sldapd.conf文件中定義;
b、初始化數據結構,其結構必須由上面的.schema文件已經定義的,並由應用程序讀取和使用。


初始化文件通常為.ldif 結尾,稱為LDIF數據交換格式。這種格式是行界定、冒號分隔的屬性-值對。例如:


引用
dn: dc=ldapuser1,ou=People,dc=test,dc=com
objectClass: top
objectClass: dcObject
objectClass: organization
....


導入時,執行:
# ldapadd -x -h 192.168.1.10 -D 'cn=admin,dc=test,dc=com' -W -f info.ldif


ldapadd 命令各參數含義如下:
-x 為使用簡單密碼驗證方式
-D 指定管理員DN(與slapd.conf中一致)
-W 為管理員密碼,可以使用-w password 直接輸入密碼
-f 為初始化數據LDIF的文件名
-h 為操作的服務器IP地址

2、搜索操作
LDAP是讀優化數據庫,因此,讀的速度很快,也很常用。但與關系數據庫不同,其以樹結構形式讀取數據,若不添加過濾,會顯示匹配節點下所有節點的內容。若以ldif 的形式表達,剛開始可能不太習慣。
# ldapsearch -x -b 'dc=test,dc=com'


首先要留意的是,ldapsearch 不需要提供驗證信息。因為正如前面提到的,LDAP 默認供任何人可讀。
-b 后面定義搜索節點位置,即從該節點往其子節點進行搜索


3、身份驗證
修改或添加內容需進行用戶驗證,可通過下面的命令確認驗證信息:
# ldapwhoami -x -D 'cn=ldapuser1,dc=test,dc=com' -w 'yourpasswd'
dn:cn=admin,dc=test,dc=com
Result: Success (0)


4、修改操作
修改內容通常由LDIF 文件提供。因此,可先用ldapsearch 導出節點內容:


# ldapsearch -x -LLL -b 'dc=test,dc=com'
-LLL 表示不輸出注釋內容,以便后續重新導入。

其實也可以使用文件進行批量修改,我們只要把需要修改信息寫入文件即可,比如:
dn: uid=ldapuser1,ou=People,dc=test,dc=com
changetype: modify
replace: uidNumber
uidNumber: 1000

把以上內容寫入test.ldif中,運行如下命令:

ldapmodify -x -D "cn=admin,dc=test,dc=com" -w yourpasswd -f test.ldif


a、ldapadd 與ldapmodify -a 作用相同
b、如果在添加或修改時,報Naming violation等錯誤,則說明添加或修改的內容不符合schema中定義的對象屬性規范,需修改后才能重新操作。

 

5、刪除操作
刪除時,給出DN即可:
# ldapdelete -x -D 'cn=admin,dc=test,dc=com' -w yourpasswd -r 'dc=test,dc=com'
-r 表示以遞歸模式刪除,即刪除該節點下面的所有子節點。


OK,到這里基本上完整的LDAP配置和使用方法已經比較完整了,大家可以按照步驟自由的去嘗試,遇到新問題也可以去google上找答案(Google is a great teacher! :-)).

Good Luck!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM