搭建ldap分别通過web和lam管理,ldap+apache
LDAP安裝:
# yum install -y openldap openldap-clientsopenldap-servers
# ls /etc/openldap/
slapd.conf 主配置檔案
lapd.conf Client配置檔案
看是否已經有定義好的架構,關于描述linux系統使用者和系統的組的對象類和屬性類
# cat /etc/openldap/schema/nis.schema
MUST( cn $ uid $ uidNumber $ gidNumber $ homeDirectory ) #字段值不能為空
MAY( userPassword $ loginShell $ gecos $ description ) ) #字段值可以為空
加密rootdn密碼
# slappasswd -h {MD5}
New password:
Re-enter new password:
{MD5}ICy5YqxZB1uWSwcVLSNLcA==
# vim /etc/openldap/slapd.conf
include /etc/openldap/schema/nis.schema #包含定義好的架構
allow bind_v2 #定義版本,向下相容
pidfile /var/run/openldap/slapd.pid #服務啟動後的pid檔案
... ...
database bdb #b伯克利 db資料庫
suffix "dc=hn,dc=ny,dc=com"
rootdn "cn=admin,dc=hn,dc=ny,dc=com" #管理dn (ldap管理者賬号 cn(common name),不要寫OU,管理所有的OU)
# Cleartext passwords, especially for therootdn, should
# be avoided. See slappasswd(8) andslapd.conf(5) for details.
# Use of strong authentication encouraged.
# rootpw secret
# rootpw {crypt}ijFYNcSNctBYg
rootpw {MD5}ICy5YqxZB1uWSwcVLSNLcA== #管理dn密碼
# The database directory MUST exist priorto running slapd AND
# should only be accessible by the slapdand slap tools.
# Mode 700 recommended.
directory /var/lib/ldap #真正資料庫的位置,700權限,所有者及組為ldap
# Indices to maintain for this database #優化ldap的索引
index objectClass eq,pres
index ou,cn,mail,surname,givenname eq,pres,sub
index uidNumber,gidNumber,loginShell eq,pres
index uid,memberUid eq,pres,sub
index nisMapName,nisMapEntry eq,pres,sub
**************************************************
access toattrs=shadowLastChange,userPassword by self write by * auth
access to * by * read
**************************************************
#access 行定義了誰可以通路目錄中的内容。我們希望使用者可以修改自己的密碼,并更新自己的shadow 資訊來反映密碼的變化。希望身份驗證程式能夠檢索使用者的密碼。還希望使用者能夠讀取所有其他條目。注意密碼條目是不可讀的,shadow 屬性的惟一用處就是管理密碼的過期問題。
啟動LDAP服務
cacerts DB_CONFIG.example ldap.conf schemaslapd.conf
# cp /etc/openldap/DB_CONFIG.example/var/lib/ldap/DB_CONFIG
# chown ldap /var/lib/ldap/DB_CONFIG
# service ldap start
Checking configuration files for slapd:config file testing succeeded [ OK ]
Starting slapd: [ OK ]
ldap庫模闆定義生成ldap目錄結構的變量/usr/share/openldap/migration/ 中執行遷移。這些Perl 腳本的配置資訊包含在migrate_common.ph檔案的開頭。對于我們的目的來說,隻需要修改命名字首的變量來使用條目的識别名就足夠了
[root@localhost migration]# vimmigrate_common.ph
$DEFAULT_MAIL_DOMAIN = "hn.ny.com";
$DEFAULT_BASE = "dc=hn,dc=ny,dc=com";
[root@localhost migration]#./migrate_base.pl > /tmp/uplooking.ldif #perl腳本調用migrate_common.ph生成可以導入ldap的ldif檔案
[root@localhost migration]# vim /tmp/ny.ldif
dn: dc=hn,dc=ny,dc=com
dc: hn
objectClass: top
objectClass: domain
dn: ou=People,dc=hn,dc=ny,dc=com
ou: People
objectClass: organizationalUnit
dn: ou=Group,dc=hn,dc=ny,dc=com
ou: Group
建立測試使用者
[root@localhost ~]# for i in {1..5} ; douseradd ldap$i ; echo "123456" | passwd --stdin ldap$i ; done
[root@localhost ~]# grep -E ^ldap[1-5]+/etc/passwd > /tmp/users.txt
[root@localhost ~]# grep -E ^ldap[1-5]+/etc/group > /tmp/groups.txt 生成使用者群組ldif檔案
[root@localhost ~]# cd/usr/share/openldap/migration/
[root@localhost migration]#./migrate_passwd.pl /tmp/users.txt > /tmp/users.ldif
[root@localhost migration]#./migrate_group.pl /tmp/groups.txt > /tmp/groups.ldif
[root@localhost migration]# ldapadd -x -D"cn=admin,dc=hn,dc=ny,dc=com" -w 123456 -f ny.ldif
[root@localhost migration]# ldapadd -x -D"cn=admin,dc=hn,dc=ny,dc=com" -w 123456 -f groups.ldif
[root@localhost migration]# ldapadd -x -D"cn=admin,dc=hn,dc=ny,dc=com" -w 123456 -f users.ldif
注:ny.ldif必須最先導入。
-x 簡單驗證
-D 指定身份
-W 提示輸入密碼
[root@localhost migration]# netstat -tunpl| grep slapd
tcp 0 0 0.0.0.0:389 0.0.0.0:* LISTEN 17873/slapd
tcp 0 0 :::389 :::* LISTEN 17873/slapd
在伺服器上使用ldapsearch指令查詢ldap資料庫中的資訊,檢查ldif檔案是否已經導入成功:
[root@localhost ~]# ldapsearch -x -w 123456-h 127.0.0.1 -D "cn=admin,dc=hn,dc=ny,dc=com" -b "dc=hn,dc=ny,dc=com"
#system-config-authentication
<a href="http://s3.51cto.com/wyfs02/M02/23/4D/wKiom1M1iUnheqyOAAF4ugWtbzQ214.jpg" target="_blank"></a>
更改密碼
如何改變自己的密碼 ldappasswd 使用你自己的DN
# ldappasswd -xD "uid=ldap1,ou=People,dc=hn,dc=ny,dc=com"-WS #如果沒有指定-S,會建立一個新的密碼
ldapwhoami測試登入
# ldapwhoami -xD "uid=ldap1,ou=People,dc=hn,dc=ny,dc=com"–W
WEB管理LDAP
# yum install httpd phpphp-ldap
# vim /etc/httpd/conf.d/php.conf
# tar zxvfphpldapadmin-1.2.0.4.tgz
# mv phpldapadmin-1.2.0.4/var/www/html/ldap
# service httpd start
<a href="http://s3.51cto.com/wyfs02/M01/23/4E/wKioL1M1iTLh6S6PAALZtOxNMgE955.jpg" target="_blank"></a>
LAM
# rpm -ivh ldap-account-manager-2.6.0-0.fedora.1.noarch.rpm
全局配置檔案:(語言)
# cd/var/www/html/lam/config
[root@localhostconfig]# cp config.cfg_sample config.cfg
對應伺服器配置檔案:
[root@localhostconfig]# cp lam.conf_sample ldapserver1.conf
[root@localhostconfig]# vim ldapserver.conf
admins: cn=admin,dc=hn,dc=ny,dc=com
passwd:{MD5}4QrcOUm6Wau+VuBX8g+IPg==
treesuffix: dc=hn,dc=ny,dc=com
注意:
vim ldapuser1.conf 将全部my-domain替換ny.
[root@localhostconfig]# chown -R apache.apache *
<a href="http://s3.51cto.com/wyfs02/M02/23/4D/wKiom1M1iWuT5fWFAAMRMMqbaTk984.jpg" target="_blank"></a>
Apache+ldap
1.配置apache驗證功能。驗證頁面如何連接配接ldap來驗證
2.讓ldap存apache相關帳号,apache通過一個自有子產品來支援連接配接ldap(mod_authz_ldap)
[root@localhost ~]# yuminstall mod_authz_ldap
# vim/etc/httpd/conf.d/authz_ldap.conf
LoadModuleauthz_ldap_module modules/mod_authz_ldap.so
<IfModule mod_authz_ldap.c>
<Directory "/var/www/html/"> #設定在IfModule裡面,如果子產品生效,執行下面
AuthzLDAPMethod ldap
AuthzLDAPServer 192.168.0.116
AuthzLDAPUserBase ou=People,dc=hn,dc=ny,dc=com
AuthzLDAPUserKey uid
AuthzLDAPUserScope base
AuthType basic
AuthName "ldap test"
require valid-user
</Directory>
</IfModule>
# service httpd restart
測試:
用戶端通路web server,提示需要身份驗證,輸入ldap伺服器的帳号密碼,驗證通過後可以看到web内容
例如賬号:ldap1 密碼:上面自己設定的
<a href="http://s3.51cto.com/wyfs02/M00/23/4E/wKioL1M1iVLA010YAAMdl3qC2yc332.jpg" target="_blank"></a>
本文轉自潘闊 51CTO部落格,原文連結:http://blog.51cto.com/pankuo/1386295,如需轉載請自行聯系原作者