文檔信息
目 的:搭建一套完整的OpenLDAP系統,實現帳號的統一管理。
1:OpenLDAP服務端的搭建
2:PhpLDAPAdmin的搭建
3:OpenLDAP的打開日誌信息
4:OpenLDAP與migrationtools實現導入系統帳號的相關信息
5:OpenLDAP客戶端的配置
6:OpenLDAP與SSH
7:OpenLDAP限制用戶登陸系統
8:OpenLDAP強制用戶一登陸系統更改密碼
9:OpenLDAP與系統帳號結合Samba
10:OpenLDAP的主從
11:OpenLDAP的雙主
操做系統:CentOS release 6.7
基礎的環境準備:
關閉防火牆:/etc/init.d/iptables stop && chkconfig iptables off
關閉NetworkManager:/etc/init.d/NetworkManager stop && chkconfig NetworkManager off
SeLinux設爲disabled:getenforce 是否爲Disabled,若不是,則修改:
1:臨時的生效 setenforce 0,再getenforce的時候爲permissive
2:修改配置文件,而後重啓 vim /etc/sysconfig/selinux 把SELINUX=disabled
yum源倉庫的配置:
1)mkdir /yum
2)vim /etc/yum.repos.d/ll.repo
[local]
name = local
baseurl = file:///yum
gpgcheck = 0
enabled = 1
3)掛載 mount /mnt/hgfs/軟件/CentOS-6.7-x86_64-bin-DVD1to2/CentOS-6.7-x86_64-bin-DVD1.iso /yum -o loop
4)yum clean all 清除緩存
5)yum makecache 建立緩存
一:OpenLDAP服務器的搭建
1)安裝OpenLDAP的相關
yum -y install openldap openldap-servers openldap-clients openldap-devel compat-openldap 其中compat-openldap這個包與主從有很大的關係
安裝完後,能夠看到自動建立了ldap用戶:

能夠經過rpm -qa |grep openldap查看安裝了哪些包:

2)OpenLDAP的相關配置文件信息
/etc/openldap/slapd.conf:OpenLDAP的主配置文件,記錄根域信息,管理員名稱,密碼,日誌,權限等
/etc/openldap/slapd.d/*:這下面是/etc/openldap/slapd.conf配置信息生成的文件,每修改一次配置信息,這裏的東西就要從新生成
/etc/openldap/schema/*:OpenLDAP的schema存放的地方
/var/lib/ldap/*:OpenLDAP的數據文件
/usr/share/openldap-servers/slapd.conf.obsolete 模板配置文件
/usr/share/openldap-servers/DB_CONFIG.example 模板數據庫配置文件
OpenLDAP監聽的端口:
默認監聽端口:389(明文數據傳輸)
加密監聽端口:636(密文數據傳輸)
3)初始化OpenLDAP的配置
cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
cp /usr/share/openldap-servers/slapd.conf.obsolete /etc/openldap/slapd.conf
4)修改配置文件
首先,slappasswd生成密文密碼123456,拷貝這個到/etc/openldap/slapd.conf裏

這裏的rootpw必須頂格寫,並且與後面的密碼文件用
Tab鍵隔開

修改對應的


5)從新生成配置文件信息文件
先檢測/etc/openldap/slapd.conf是否有錯誤:slaptest -f /etc/openldap/slapd.conf

這裏報錯是由於在第三步後沒有從新生成配置文件,啓動slapd。而是直接修改配置文件去了。先啓動slapd:/etc/init.d/slapd restart

這裏又報錯,這是由於沒有給/var/lib/ldap受權,受權後chown -R ldap.ldap /var/lib/ldap/,再重啓slapd,/etc/init.d/slapd restart,能夠看到成功的

接着回到檢測/etc/openldap/slapd.conf是否有錯誤:slaptest -f /etc/openldap/slapd.conf

能夠看到沒問題,而後從新生成配置文件的配置信息:
先刪除最早的配置文件生成的信息:rm -rf /etc/openldap/slapd.d/*
從新生成:slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d/
查看是否生成的是本身修改的配置文件信息:cat /etc/openldap/slapd.d/cn\=config/olcDatabase\=\{2\}bdb.ldif

受權:chown -R ldap.ldap /etc/openldap/slapd.d/
重啓:/etc/init.d/slapd restart

到這裏爲止,OpenLDAP服務端基本上完成了,咱們能夠經過PhpLDAPAdmin來登陸看一下,那先得安裝PhpLDAPAdmin
二:PhpLDAPAdmin的搭建
1)安裝EPEL倉庫,鏡像裏沒有PhpLDAPAdmin這個的安裝包,因此得安裝EPEL倉庫
rpm -ivh
http://mirrors.ukfast.co.uk/sites/dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
yum clean all
yum makecache
2)安裝PhpLDAPAdmin
yum install -y phpldapadmin
3)修改phpldapadmin的配置文件,訪問控制權限vim /etc/httpd/conf.d/phpldapadmin.conf,容許誰訪問

4)修改配置文件:vim /etc/phpldapadmin/config.php
$servers->setValue('login','attr','dn'); 這一行的註釋去掉
//$servers->setValue('login','attr','uid'); 這一行註釋掉

5)重啓httpd服務/etc/init.d/httpd restart
6)在瀏覽器輸入OpenLDAP服務端的IP 10.0.0.138/ldapadmin

7)登陸,輸入管理員的DN,也就是配置文件裏配置的

8)認證,報錯

這是由於在第一步搭建OpenLDAP服務端的時候,並無把管理員的帳號信息導入,編輯root.ldif,而後導入
dn: dc=lemon,dc=com
objectclass: dcObject
objectclass: organization
o: Yunzhi,Inc.
dc: lemon
dn: cn=Captain,dc=lemon,dc=com
objectclass: organizationalRole
cn: Captain
這裏得注意每個屬性:
後必須有空格,可是值的後面不能有任何空格
而後導入:ldapadd -x -D "cn=Captain,dc=lemon,dc=com" -W -f root.ldif

而後再經過瀏覽器去訪問的話:

也能夠經過命令行查詢:
ldapsearch -x -b "cn=Captain,dc=lemon,dc=com"

到這裏,PhpLDAPAdmin搭建完了,接下來,我們得把
日誌打開,這樣的話好排錯,嘿嘿嘿
三:OpenLDAP的打開日誌信息
1:如今配置文件里加上日誌行 ,這裏的日誌級別有不少種,-1的話會記錄不少日誌信息
vim /etc/openldap/slapd.conf 加上
loglevel -1

這裏修改了配置文件,全部得從新生成配置文件的信息
rm -rf /etc/openldap/slapd.d/*
slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d/
chown -R ldap.ldap /etc/openldap/slapd.d/
2:在 vim /etc/rsyslog.conf加上
local4.* /var/log/slapd/slapd.log

而後重啓/etc/init.d/rsyslog restart
3:建立日誌文件目錄,受權
mkdir /var/log/slapd
chmod 755 /var/log/slapd/
chown ldap.ldap /var/log/slapd/
4:重啓slapd服務,/etc/init.d/slapd restart
5:就能夠看到日誌信息了cat /var/log/slapd/slapd.log
四
:OpenLDAP與migrationtools實現導入系統帳號的相關信息
1:安裝migrationtools
yum -y install migrationtools
2:修改migrationtools的配置文件,在/usr/share/migrationtools/這個目錄下有不少migrationtools的文件
vim /usr/share/migrationtools/migrate_common.ph 修改如下的兩個地方

3:生成基礎的數據文件,能夠本身修改這個生成的base.ldif文件,把不須要的去掉
/usr/share/migrationtools/migrate_base.pl > base.ldif

4:把base.ldif導入OpenLDAP
ldapadd -x -D "cn=Captain,dc=lemon,dc=com" -W -f base.ldif

這裏會報錯,咱們
能夠經過-c參數強制加入

導入以後,經過PhpLdapAdmin能夠看到已經導入進來了:

5:把系統的用戶生成ldif文件
cd /usr/share/migrationtools
./migrate_passwd.pl /etc/passwd passwd.ldif
./migrate_group.pl /etc/group group.ldif

能夠看到生成的文件,而後根據本身須要修改這兩個ldif文件:
passwd.ldif只留一個test1測試用戶:

group.ldif留對應的test1:

把用戶導入進去:
ldapadd -x -D "cn=Captain,dc=lemon,dc=com" -W -f passwd.ldif

把組導進去:
ldapadd -x -D "cn=Captain,dc=lemon,dc=com" -W -f group.ldif

而後就能夠看到:

在這裏就已經完成把系統的帳號屬性導入了OpenLDAP,而後就經過添加OpenLDAP用戶,來進行驗證,因此得先作好客戶端的設置
五:OpenLDAP客戶端的配置
1:停掉sssd服務 service sssd stop && chkconfig sssd off
2:安裝nslcd服務 yum install nss-pam-ldapd
3:修改vim /etc/nslcd.conf這個配置文件

4:修改vim /etc/pam_ldap.conf


5: vim /etc/pam.d/system-auth 修改,把sss行的註釋掉,改爲ldap的

6:vim /etc/nsswitch.conf 修改nsswitch.conf配置文件,修改後,默認登陸的用戶經過本地配置文件進行查找並匹配。當匹配不到用戶信息時,會經過後端配置的LDAP認證服務進行匹配

7:vim /etc/sysconfig/authconfig 確保標記的已打開爲yes
USESHADOW=yes 啓用密碼驗證
USELDAPAUTH=yes 啓用OpenLDAP驗證
USELOCAUTHORIZE=yes 啓用本地驗證
USELDAP=yes 啓用LDAP認證協議

8:重啓nslcd服務
/etc/init.d/nslcd restart
9:驗證,先經過OpenLDAP增長一個用戶,在test1的基礎上,複製一個test2的條目


後面的根據本身的修改

能夠看到已經成功的添加了test2的用戶,這是OpenLDAP添加的,在本地是沒有的,用cat /etc/passwd 看是沒有test2用戶的
測試:su - test2

在/etc/pam.d/system-auth配置文件裏添加這一行:
session optional pam_mkhomedir.so skel=/etc/skel/ umask=0022

重啓 /etc/init.d/nslcd restart
在進行測試:就能夠了

查看系統用戶列表:
服務端查詢:
ldapsearch -x -b "ou=People,dc=lemon,dc=com" |grep dn
客戶端查詢:
ldapsearch -H ldap://10.0.0.138 -x -b "ou=People,dc=lemon,dc=com" |grep dn

查詢單個用戶:
ldapsearch -x -b "uid=test1,ou=People,dc=lemon,dc=com" |grep dn

客戶端的配置到這裏ok啦。有帳號確定要能經過ssh登陸系統
六:OpenLDAP與SSH
1:vim /etc/ssh/sshd_config

2:vim /etc/pam.d/sshd 用於第一次登錄的帳戶自動建立家目錄

3:vim /etc/pam.d/password-auth

4:重啓sshd
七:OpenLDAP限制用戶登陸系統
在帳號中,不能讓每一個用戶都能登陸系統,因此要限制用戶登陸
1:vim /etc/pam.d/sshd 在這裏加上pam_access.so模塊

2:vim /etc/security/access.conf 這裏限制test2用戶ssh登陸系統

測試:能夠看到就只有test2登陸不上

八:OpenLDAP強制用戶一登陸系統更改密碼
1:修改配置文件
在前面打開註釋
moduleload ppolicy.la
modulepath /usr/lib/openldap
modulepath /usr/lib64/openldap
還要在database config前面加上這兩段
access to attrs=userPassword
by self write
by anonymous auth
by dn="cn=Captain,dc=lemon,dc=com" write
by * none
access to *
by self write
by dn="cn=Captain,dc=lemon,dc=com" write
by * read

在文件的末尾添加:
overlay ppolicy
ppolicy_default cn=Captain,ou=pwpolicies,dc=lemon,dc=com

2:從新生成配置文件數據庫:
[root@lele openldap]# vim /etc/openldap/slapd.conf
[root@lele openldap]# rm -rf /etc/openldap/slapd.d/*
[root@lele openldap]# slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d
config file testing succeeded
[root@lele openldap]# chown -R ldap.ldap /etc/openldap/slapd.d/
[root@lele openldap]# /etc/init.d/slapd restart
Stopping slapd: [ OK ]
Starting slapd: [ OK ]
能夠經過配置文件的數據信息看到ppolicy模塊已經加進來了
cat /etc/openldap/slapd.d/cn\=config/cn\=module\{0\}.ldif

3:編輯
cat 1.ldif
dn: ou=pwpolicies,dc=lemon,dc=com
objectClass: organizationalUnit
ou: pwpolicies
4:ldapadd -x -D "cn=Captain,dc=lemon,dc=com" -W -f 1.ldif
添加進去

能夠在PhpLdapAdmin上看到:

5:
添加cn=Captain,ou=pwpolicies,dc=lemon,dc=com這個的一些屬性值
[root@ll ~]# cat 2.ldif
dn: cn=Captain,ou=pwpolicies,dc=lemon,dc=com cn: Captain objectClass: pwdPolicy objectClass: person pwdAllowUserChange: TRUE pwdAttribute: userPassword pwdExpireWarning: 259200 pwdFailureCountInterval: 0 pwdGraceAuthNLimit: 5 pwdInHistory: 5 pwdLockout: TRUE pwdLockoutDuration: 300 pwdMaxAge: 2592000 pwdMaxFailure: 5 pwdMinAge: 0 pwdMinLength: 8 pwdMustChange: TRUE pwdSafeModify: TRUE sn: dummy value
把屬性值添加進去

在PhpLdapAdmin能夠看到:

6:在vim /etc/pam_ldap.conf中的末尾添加:使得客戶端能識別服務端的密碼策略
pam_password md5
bind_policy soft
pam_lookup_policy yes
pam_password clear_remove_old
7:重啓nslcd
/etc/init.d/nslcd restart
8:測試
修改用戶的屬性,用test3作測試
[root@ll ~]# cat modify.ldif
dn: uid=test3,ou=people,dc=lemon,dc=com
changetype: modify
replace: pwdReset
pwdReset: TRUE
ldapmodify -x -D "cn=Captain,dc=le,dc=com" -W -f modify.ldif 導入

ldapwhoami -x -D uid=test3,ou=people,dc=lemon,dc=com -W -e ppolicy -v 查看test3用戶的策略信息

這裏顯示輸入test3 的原始密碼,而後輸入新修改的密碼

當修改完後,就沒有必須改變密碼的那一句話了

這裏能夠啦
九:OpenLDAP與系統帳號結合Samba
默認的Samba服務器支持本地系統用戶(smbpasswd添加後)訪問Samba資源,不支持OpenLDAP服務器帳號訪問Samba共享資源,配置完後,OpenLDAP每新增一個用戶,就自動支持Samba,就能夠用這個帳號直接訪問Samba,不須要存在於本地用戶,不用smbpasswd用戶
1:安裝samba
yum -y install samba
2:把Samba.schema文件拷貝到LDAP的schema目錄下,把原來的覆蓋掉
cp /usr/share/doc/samba-3.6.23/LDAP/samba.schema /etc/openldap/schema/
3:修改配置文件vim /etc/openldap/slapd.conf
在include的地方,加上Samba的schema

3:修改了配置文件,就有從新生成配置文件數據
rm -rf /etc/openldap/slapd.d/*
slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d/
config file testing succeeded
chown -R ldap.ldap /etc/openldap/slapd.d/
/etc/init.d/slapd restart
4:修改Samba的配置文件
添加:
security = user
passdb backend = ldapsam:ldap://10.0.0.138
ldap suffix = "dc=lemon,dc=com"
ldap group suffix = "cn=group"
ldap user suffix = "ou=people"
ldap admin dn = "cn=Captain,dc=lemon,dc=com"
ldap delete dn = no
pam password change = yes
ldap passwd sync = yes
ldap ssl = no

對應這裏的

在最後的時候添加共享的文件:
[public]
comment = Public Stuff
path = /tmp/lile
public = yes
writable = yes
printable = no
5:建立共享文件夾,而且受權
mkdir /tmp/lile
chmod 777 /tmp/lile/
6:把OpenLDAP的密碼傳給Samba,
smbpasswd -w 123456 這裏的密碼是OpenLDAP的管理員密碼

若不加,會報錯:

7:重啓smb
/etc/init.d/smb restart
/etc/init.d/nmb restart
8: Samba開通以後,能夠看到這裏的開關也打開了

9:測試
先把系統用戶test1用smbpasswd -a test1 加到Samba的用戶下,就能夠看到:
test1用戶下多了Samba的特性,原來是沒有的

而後基於test1,在PhpLdapAdmin添加test2用戶,不用smbpasswd,就只是OpenLDAP用戶,複製的時候必定要從新改一下這裏的密碼,要否則登不進,


而後,就能夠用windos去訪問了,這裏有一個概念就是
OpenLDAP添加了的用戶,不要再用smbpasswd去添加了,能夠直接登陸Samba
十:OpenLDAP的主從
1:作主從和雙主的時候,必定要確認安裝了
compat-openldap這個包
2:
在主上的配置文件 10.0.0.138:
備份原來的配置文件:cp /etc/openldap/slapd.conf /etc/openldap/slapd.bak
先停掉服務 /etc/init.d/slapd stop
vim /etc/openldap/slapd.conf 修改配置文件
添加 index entryCSN,entryUUID eq

這裏的註釋去掉:


在文件的最後添加:
overlay syncprov 後端工做再overlay模式
syncprov-checkpoint 100 10 當知足修改100個條目或者10分鐘的條件時主動以推的方式執行
syncprov-sessionlog 100 會話日誌條目的最大數量

而後從新生成配置文件的數據文件:
rm -rf /etc/openldap/slapd.d/*
slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d/
chown -R ldap.ldap /etc/openldap/slapd.conf
chown -R ldap.ldap /etc/openldap/slapd.d
/etc/init.d/slapd restart
3:
導出主的數據文件 ldapsearch -x -b 'dc=lemon,dc=com' > root.ldif,拷貝到從上
scp scp root.ldif 10.0.0.140:~/
4:
把主的配置文件slapd.conf 拷貝到從10.0.0.140上 用scp /etc/openldap/slapd.conf 10.0.0.140:~/
5:從上從主上拷貝了配置文件,
去掉:
overlay syncprov
syncprov-checkpoint 100 10
syncprov-sessionlog 100
而後再加上
syncrepl rid=003
provider=ldap://10.0.0.138:389/
type=refreshOnly
retry="60 10 600 +" 嘗試時間
interval=00:00:00:10 設置同步更新時間(日:時:分:秒)
searchbase="dc=lemon,dc=com"
scope=sub 匹配根域全部條目
schemachecking=off 同步更新時是否開啓schema語法檢測
bindmethod=simple 同步驗證模式爲簡單模式(即明文)
binddn="cn=Captain,dc=lemon,dc=com" 使用Captain用戶讀取目錄樹信息
attrs="*,+" 同步全部屬性信息
credentials=123456 管理員密碼
從新生成數據配置文件
rm -rf /etc/openldap/slapd.d/*
slaptest -f /etc/openldap/slapd.conf -F /etc/openldap/slapd.d/
chown -R ldap.ldap /etc/openldap/slapd.conf
chown -R ldap.ldap /etc/openldap/slapd.d
/etc/init.d/slapd restart
6:測試
在主的10.0.0.138上添加一個test7的用戶,在從上刷新一下,是同步到的


十一:OpenLDAP的雙主
在主從的基礎上,修改配置,
這是主的

serverID 2
overlay syncprov
syncrepl rid=001
(這裏的格式必定要注意,中間這一段要用Tab鍵Tab一下,若是不的話會報錯以下)
provider=ldap://10.0.0.140
type=refreshAndPersist
searchbase="dc=lemon,dc=com"
schemachecking=simple
binddn="cn=Captain,dc=lemon,dc=com"
credentials=123456
retry="60 +"
mirrormode on

這是從的:

serverID 1
overlay syncprov
syncrepl rid=001
(這裏的格式必定要注意,中間這一段要用Tab鍵Tab一下)
provider=ldap://10.0.0.138:389/
retry="60 10 600 +"
searchbase="dc=lemon,dc=com"
schemachecking=off
bindmethod=simple
binddn="cn=Captain,dc=lemon,dc=com"
credentials=123456
mirrormode on
測試:在兩臺機上分別新建一個用戶,看是否在對方能刷新到,主從與雙主都只是備份的關係,若一臺掛了,當即切換到另外一臺,則需作高可用和負載均衡