OpenLDAP編譯安裝及配置,openldap編譯配置
原文發表於cu:2016-06-20
參考文檔:
一.環境
Server:基於CentOS-7-x86_64-1511
Server IP: 172.18.12.203
二.軟體擷取OpenLDAP
OpenLDAP官網:http://www.openldap.org/software/download/
OpenLDAP 2.4.44:ftp://ftp.openldap.org/pub/OpenLDAP/openldap-release/openldap-2.4.44.tgz
BDB
berkeley-db-5.1.29 (OpenLDAP當前與6.x版本不相容,READEME中明確寫出相容4.4~4.8或5.0~5.1):
http://download.oracle.com/berkeley-db/db-5.1.29.tar.gz
LDAP Administrtor
ldapadmin 2015.2:
官網:http://www.ldapbrowser.com/download.htm
64-bit:http://softerra-downloads.com/ldapadmin/ldapadmin-4.13.16704.0-x64-eng.msi
三.準備
1. 關閉selinux;
2. 開啟防火牆tcp 389 / 636連接埠。
# tcp 389 是openldap 明文傳輸連接埠,tcp 636是ssl加密傳輸的連接埠。
# centos7預設內建firewalld服務,可以停用之後安裝iptables。
四.安裝OpenLDAP1. 依賴包
# 涉及libtool-ltdl與libtool-ltdl-devel,如不安裝,在編譯時間報錯:configure: error: could not locate libtool ltdl.hyum install *ltdl* -y
2. 安裝BDB
#必須在解壓包的build_unix目錄中編譯安裝,否則會報錯[root@localhost ~]# cd /usr/local/src/[root@localhost src]# tar -zxvf db-5.1.29.tar.gz[root@localhost src]# cd db-5.1.29/build_unix/[root@localhost build_unix]# ../dist/configure --prefix=/usr/local/berkeleydb-5.1.29[root@localhost build_unix]# make[root@localhost build_unix]# make install
3. 更新lib庫
#保證在後面編譯openldap時能找到lib和include下的庫[root@localhost build_unix]# cd /usr/local/src/[root@localhost src]# echo "/usr/local/berkeleydb-5.1.29/lib/" > /etc/ld.so.conf[root@localhost src]# ldconfig -v
4. 安裝OpenLDAP
#編譯選項可以通過./configure --help查看;#其中make test一步時間較長;#如果未設定CPPFLAGS,configure過程可能會提示configure: error: BDB/HDB: BerkeleyDB not available 或 configure: error: BerkeleyDB version incompatible with BDB/HDB backends[root@localhost ~]# cd /usr/local/src/[root@localhost src]# tar -zxvf openldap-2.4.44.tgz[root@localhost src]# cd openldap-2.4.44[root@localhost openldap-2.4.44]# ./configure --prefix=/usr/local/openldap-2.4.44 --enable-syslog --enable-modules --enable-debug --with-tls CPPFLAGS=-I/usr/local/berkeleydb-5.1.29/include/ LDFLAGS=-L/usr/local/berkeleydb-5.1.29/lib/[root@localhost openldap-2.4.44]# make depend[root@localhost openldap-2.4.44]# make[root@localhost openldap-2.4.44]# make test[root@localhost openldap-2.4.44]# make install
5. 設定可執行命令
#對openldap用戶端(bin)與伺服器端(sbin)相關執行檔添加軟連結,也可以通過增加環境變數的方式設定[root@localhost openldap-2.4.44]# cd /usr/local/openldap-2.4.44[root@localhost openldap-2.4.44]# ln -s /usr/local/openldap-2.4.44/bin/* /usr/local/bin/[root@localhost openldap-2.4.44]# ln -s /usr/local/openldap-2.4.44/sbin/* /usr/local/sbin/
五.簡單配置1. 安裝完成的openldap-2.4.44目錄結構
openldap安裝完成後相關目錄承載的功能如下:
bin/ --用戶端工具如ldapadd、ldapsearch
etc/ --包含主設定檔slapd.conf、schema、DB_CONFIG等
include/
lib/
libexec/ --服務端啟動工具slapd
sbin/ --服務端工具如slappasswd
share/
var/ --bdb資料、log存放目錄
2. 配置rootdn密碼(optional)
#設定rootdn密碼,這裡設定為123456;#這樣rootdn密碼為密文方式,複製輸出密文到主設定檔rootdn對應的位置即可,如果不想麻煩,可以忽略此步,在主設定檔中使用明文即可。[root@localhost ~]# cd /usr/local/openldap-2.4.44/[root@localhost openldap-2.4.44]# slappasswdNew password:Re-enter new password:{SSHA}K9+WK/t1e0V0K6pUMOyTsaTwkDBNEDiP3. 主設定檔slapd.conf
[root@localhost openldap-2.4.44]# cd /usr/local/openldap-2.4.44/etc/openldap/[root@localhost openldap]# vim slapd.conf#以下修改紫紅色字型是未做修改的部分,紅色字型是有修改部分,藍色字型表示新增部分#schema預設只有core.schema,各級需要添加,這裡將同設定檔一個目錄的schema目錄中有的schema檔案都加到設定檔中;5 include /usr/local/openldap-2.4.44/etc/openldap/schema/core.schema6 include /usr/local/openldap-2.4.44/etc/openldap/schema/collective.schema7 include /usr/local/openldap-2.4.44/etc/openldap/schema/corba.schema8 include /usr/local/openldap-2.4.44/etc/openldap/schema/cosine.schema9 include /usr/local/openldap-2.4.44/etc/openldap/schema/duaconf.schema10 include /usr/local/openldap-2.4.44/etc/openldap/schema/dyngroup.schema11 include /usr/local/openldap-2.4.44/etc/openldap/schema/inetorgperson.schema12 include /usr/local/openldap-2.4.44/etc/openldap/schema/java.schema13 include /usr/local/openldap-2.4.44/etc/openldap/schema/misc.schema14 include /usr/local/openldap-2.4.44/etc/openldap/schema/nis.schema15 include /usr/local/openldap-2.4.44/etc/openldap/schema/openldap.schema16 include /usr/local/openldap-2.4.44/etc/openldap/schema/pmi.schema17 include /usr/local/openldap-2.4.44/etc/openldap/schema/ppolicy.schema25 pidfile /usr/local/openldap-2.4.44/var/run/slapd.pid26 argsfile /usr/local/openldap-2.4.44/var/run/slapd.args#新增記錄檔層級與路徑,需要在編譯時間--enable-debug,否則記錄檔輸出,不影響偵錯模式;28 loglevel 25629 logfile /usr/local/openldap-2.4.44/var/slapd.log#這裡使用mdb做後端資料庫,也可修改為"bdb"參數,在OpenLDAP 官方文檔" 11.4. LMDB"章節中有介紹mdb是推薦使用的後端資料庫;67 database mdb#使用mdb做後端資料庫時,根據官方文檔中說明需要設定一個空間值," In addition to the usual parameters that a minimal configuration requires, the mdb backend requires a maximum size to be set. This should be the largest that the database is ever anticipated to grow (in bytes). The filesystem must also provide enough free space to accommodate this size.";如果使用bdb做後端資料庫,需要將此項參數注釋;68 maxsize 1073741824#修改網域名稱及管理員賬戶名;69 suffix "dc=sys,dc=com"70 rootdn "cn=admin,dc=sys,dc=com"#使用密文密碼,即前面使用slappasswd產生的密文;74 rootpw {SSHA}K9+WK/t1e0V0K6pUMOyTsaTwkDBNEDiP#openldap資料目錄,採用mdb時,在相應目錄產生" data.mdb"與" lock.mdb"檔案;採用bdb時,在相應目錄產生" dn2id.bdb"與" id2entry.bdb",及多個" __db.00*"檔案。78 directory /usr/local/openldap-2.4.44/var/openldap-data80 index objectClass eq4. 初始化OpenLADP(optional)
#如果採用mdb做後端資料庫,此步可忽略,DB_CONFIG是 bdb/hdb資料庫使用的;#與主設定檔中的配置有關,主設定檔確定使用bdb與資料存放路徑。[root@localhost openldap]# cd /usr/local/openldap-2.4.44/var/openldap-data/[root@localhost openldap-data]# cp DB_CONFIG.example DB_CONFIG
5. 啟動OpenLADP
#直接在後台工作;#非root使用者不能監聽連接埠1~1024,如果是非root使用者,有可能需要重新定義服務連接埠[root@localhost ~]# /usr/local/openldap-2.4.44/libexec/slapd#在前端工作,輸出debug資訊[root@localhost ~]# /usr/local/openldap-2.4.44/libexec/slapd -d 256
6. 驗證
[root@localhost ~]# ldapsearch -x -b '' -s base'(objectclass=*)'#或者[root@localhost ~]# ldapsearch -x -b '' -s base '(objectclass=*)' namingContexts#或者使用netstat -tunlp | grep 389,ps -ef | grep slapd,ps aux | grep slapd等也可。
示,說明openldap已經啟動運行成功:
六.簡單使用(樣本)1. 建立1個管理員帳號編輯ldif檔案
#注意與slapd.conf檔案中保持一致,下面的命令同理,#本人曾在下面的ldapadd命令中將"admin"寫成了"amdin",導致一直報" ldap_bind: Invalid credentials (49)"錯(每次都是翻回上一條命令執行),排查了3個小時左右才發現。[root@localhost ~]# vim test.ldifdn: dc=sys,dc=comobjectclass: dcObjectobjectclass: organizationo: SYS.Incdc: sysdn: cn=admin,dc=sys,dc=comobjectclass: organizationalRolecn: admin
插入資料庫
[root@localhost ~]# ldapadd -x -D "cn=admin,dc=sys,dc=com" -W -f test.ldif
可以看到帳號已經加入到ldap中:
驗證
[root@localhost ~]# ldapsearch -x -b 'dc=sys,dc=com' '(objectClass=*)'
2. 建立1個具有部門屬性的員工編輯ldif檔案
#這裡其實是先建立1個部門”it”,再在”it”部門建立了1個員工,實際上是兩條命令[root@localhost ~]# vim test2.ldif dn: ou=it,dc=sys,dc=comou: itobjectClass: organizationalUnitdn: cn=test1,ou=it,dc=sys,dc=comou: itcn: test1sn: t1objectClass: inetOrgPersonobjectClass: organizationalPerson
插入資料庫
[root@localhost ~]# ldapadd -x -D "cn=admin,dc=sys,dc=com" -W -f test2.ldif
可以看到已經員工帳號加入到ldap中:
驗證
[root@localhost ~]# ldapsearch -x -b 'dc=sys,dc=com' '(objectClass=*)'
3. ldapadmin運行ldapadmin
建立資料庫檔案
New—>New Profile,或者在主介面中Ctrl+P;
輸入Profile名字,名字任意,下一步;
輸入Host地址,Port預設為389(注意iptables是否開放389連接埠);
BaseDN處預設為空白,標示在根節點上,如果不想顯示根節點,可在後方下拉式清單中選擇具體的一級資料庫,下一步;
選擇"Other credenti",
Mechanism選擇"Simple",
Princip處輸入"cn=adminnnnn,dc=sys,dc=com",
Password處填寫相應密碼,儲存密碼,點擊"完成"即建立成功。
驗證
七.附錄1. 常見對象屬性
LDAP為人員組織機構中常見的對象都設計了屬性(比如commonName,surname)。下面有一些常用的別名:
屬性名稱 |
屬性別名 |
文法 |
描述 |
值(舉例) |
commonName |
cn |
Directory String |
名子 |
sean |
surname |
sn |
Directory String |
姓氏 |
Chow |
organizationalUnitName |
ou |
Directory String |
單位(部門)名稱 |
IT_SECTION |
organization |
o |
Directory String |
組織(公司)名稱 |
linuxprobe |
telephoneNumber |
|
Telephone Number |
電話號碼 |
911 |
objectClass |
|
|
內建屬性 |
organizationa |
2. ldif檔案樣本
http://seanlook.com/2015/01/22/openldap_ldif_example/