1. 程式人生 > >CentOS 7.0 部署OpenLDAP目錄服務

CentOS 7.0 部署OpenLDAP目錄服務

centos openldap

什麽是目錄服務?

目錄服務則是由目錄數據庫和一套能夠訪問和處理數據庫信息的協議組成的服務協議,用於集中的管理主機帳號密碼,員工名字等數據,大大的提升了管理工作效率。輕量級目錄訪問協議LDAP(Lightweight Directory Access Protocol)是在目錄訪問協議X.500的基礎上研發的

主要的優勢是:

X.500目錄協議功能非常臃腫,消耗大量資源,無法做到快速查詢且不支持TCP/IP協議網絡。


什麽是LDAP?

LDAP采用樹狀結構存儲數據(類似於我們學習的DNS服務程序),用於在IP網絡層面實現對分布式目錄的訪問和管理操作,條目是LDAP協議中最基本的元素,可以想象成字典中的單詞或者數據庫中的記錄,通常對LDAP服務程序的添加、刪除、更改、搜索都是以條目為基本對象的。



技術分享

>dn:每個條目的唯一標識符,如上圖中linuxprobe的dn值是


cn=linuxprobe,ou=marketing,ou=people,dc=mydomain,dc=org


>rdn:一般為dn值中最左側的部分,如上圖中linuxprobe的rdn值是


cn=linuxprobe


>base DN:此為基準DN值,表示頂層的根部,上圖中的base DN值是


dc=mydomain,dc=org


每個條目可以有多個屬性(如姓名、地址、電話等),每個屬性中會保存著對象名稱與對應值,LDAP已經為運維人員對常見的對象定義了屬性:

屬性名稱 屬性別名 語法 描述值(舉例)
commonName cnDirectory String名字 sean
surname sn Directory String 姓氏Chow
organizationalUnitName ouDirectory String 單位(部門)名稱IT_SECTION
organization oDirectory String組織(公司)名稱linuxprobe
telephoneNumber
Telephone Number電話號碼110
objectClass

內置屬性organizationalPerson

目錄服務實驗


OpenLdap是基於LDAP協議的開源程序,它的程序名稱叫做slapd。


實驗環境

主機名稱 操作系統IP地址
LDAP服務端(node1.com) CentOS 7.0 192.168.9.203
LDAP客戶端 CentOS 7.0192.168.9.206


配置LDAP服務端

配置LDAP服務器的主機名


[[email protected] ~]# vim /etc/hostname


node1.com


安裝openldap與相關的軟件包

```

[[email protected] ~]# yum install -y openldap openldap-clients openldap-servers migrationtools

```

生成密鑰文件

```

[[email protected] ~]# slappasswd -s node1 -n > /etc/openldap/passwd

[[email protected] ~]# cat /etc/openldap/passwd


{SSHA}DyfSP8lIHLUofsSlX3CSiM+J542KAU0n

```

寫入一條主機與IP地址的解析記錄到hosts文件

```

[[email protected] ~]# echo "192.168.9.203 node1.com" >> /etc/hosts

```

因為LDAP目錄服務是以明文的方式在網絡中傳輸數據的(包括密碼),這樣真的很不安全,所以我們采用TLS加密機制來解決這個問題,使用openssl工具生成X509格式的證書文件(有效期為365天)

```

[[email protected] ~]# openssl req -new -x509 -nodes -out /etc/openldap/certs/cert.pem -keyout /etc/openldap/certs/priv.pem -days 365

Generating a 2048 bit RSA private key

..................................................................+++

...........................................+++

writing new private key to ‘/etc/openldap/certs/priv.pem‘

-----

You are about to be asked to enter information that will be incorporated

into your certificate request.

What you are about to enter is what is called a Distinguished Name or a DN.

There are quite a few fields but you can leave some blank

For some fields there will be a default value,

If you enter ‘.‘, the field will be left blank.

-----

Country Name (2 letter code) [XX]:敲擊回車

State or Province Name (full name) []:敲擊回車

Locality Name (eg, city) [Default City]:敲擊回車

Organization Name (eg, company) [Default Company Ltd]:敲擊回車

Organizational Unit Name (eg, section) []:敲擊回車

Common Name (eg, your name or your server‘s hostname) []:node1.com

Email Address []:敲擊回車


```

修改證書的所屬與權限

```

[[email protected] ~]# cd /etc/openldap/certs/


[[email protected] certs]# chown ldap:ldap *


[[email protected] certs]# chmod 600 priv.pem


[[email protected] certs]# ls -al

總用量 96

drwxr-xr-x. 2 root root 4096 6月 10 19:43 .

drwxr-xr-x. 5 root root 100 6月 10 19:41 ..

-rw-r--r--. 1 ldap ldap 65536 6月 10 19:31 cert8.db

-rw-r--r-- 1 ldap ldap 1318 6月 10 19:43 cert.pem

-rw-r--r--. 1 ldap ldap 16384 6月 10 19:31 key3.db

-r--r-----. 1 ldap ldap 45 12月 9 2016 password

-rw------- 1 ldap ldap 1704 6月 10 19:43 priv.pem

-rw-r--r--. 1 ldap ldap 16384 12月 9 2016 secmod.db

-rw-r--r-- 1 root root 0 6月 10 19:31 .slapd-leave


```

復制一份LDAP的配置模板

```

[[email protected] ~]# cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG

```

生成數據庫文件

>不用擔心報錯信息

```

[[email protected] ~]# slaptest

593bdc38 hdb_db_open: database "dc=my-domain,dc=com": db_open(/var/lib/ldap/id2entry.bdb) failed: No such file or directory (2).

593bdc38 backend_startup_one (type=hdb, suffix="dc=my-domain,dc=com"): bi_db_open failed! (2)

slap_startup failed (test would succeed using the -u switch)


```

修改LDAP數據庫的所屬主與組並啟動slapd服務

```

[[email protected] ~]# chown ldap:ldap /var/lib/ldap/*


[[email protected] ~]# systemctl restart slapd

[[email protected] ~]# systemctl enable slapd

Created symlink from /etc/systemd/system/multi-user.target.wants/slapd.service to /usr/lib/systemd/system/slapd.service.


```

>在LDAP目錄服務中使用LDIF(LDAP Interchange Format)格式來保存信息,而LDIF是一種標準的文本文件且可以隨意的導入導出,所以我們需要有一種“格式”標準化LDIF文件的寫法,這中格式叫做“schema”,schema用於指定一個目錄中所包含對象的類型,以及每一個類型中的可選屬性,我們可以將schema理解為面向對象程序設計中的“類”,通過“類”定義出具體的對象,因此其實LDIF數據條目則都是通過schema數據模型創建出來的具體對象。


ldapadd命令用於將LDIF文件導入到目錄服務數據庫中

參數作用
-x 進行簡單認證
-D 用於綁定服務器的dn
-h目錄服務的地址
-w 綁定dn的密碼
-f 使用LDIF文件進行條目添加的文件


添加cosine和nis模塊


[[email protected] ~]# cd /etc/openldap/schema/


添加cosine模塊

[[email protected] schema]# ldapadd -Y EXTERNAL -H ldapi:/// -D "cn=config" -f cosine.ldif

SASL/EXTERNAL authentication started

SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth

SASL SSF: 0

adding new entry "cn=cosine,cn=schema,cn=config"


添加nis模塊

[[email protected] schema]# ldapadd -Y EXTERNAL -H ldapi:/// -D "cn=config" -f nis.ldif

SASL/EXTERNAL authentication started

SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth

SASL SSF: 0

adding new entry "cn=nis,cn=schema,cn=config"



```

創建/etc/openldap/changes.ldif文件,並將下面的信息復制進去

>在你做時,下面的olcRootPW:處一定要寫你之前生成的密鑰文件(如{SSHA}DyfSP8lIHLUofsSlX3CSiM+J542KAU0n)

```

[[email protected] ~]# vim /etc/openldap/changes.ldif


dn: olcDatabase={2}hdb,cn=config

changetype: modify

replace: olcSuffix

olcSuffix: dc=node1,dc=com


dn: olcDatabase={2}hdb,cn=config

changetype: modify

replace: olcRootDN

olcRootDN: cn=Manager,dc=node1,dc=com


dn: olcDatabase={2}hdb,cn=config

changetype: modify

replace: olcRootPW

olcRootPW: {SSHA}DyfSP8lIHLUofsSlX3CSiM+J542KAU0n


dn: cn=config

changetype: modify

replace: olcTLSCertificateFile

olcTLSCertificateFile: /etc/openldap/certs/cert.pem


dn: cn=config

changetype: modify

replace: olcTLSCertificateKeyFile

olcTLSCertificateKeyFile: /etc/openldap/certs/priv.pem


dn: cn=config

changetype: modify

replace: olcLogLevel

olcLogLevel: -1


dn: olcDatabase={1}monitor,cn=config

changetype: modify

replace: olcAccess

olcAccess: {0}to * by dn.base="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth" read by dn.base="cn=Manager,dc=node1,dc=com" read by * none


```

將新的配置文件更新到slapd服務程序

```

[[email protected] ~]# ldapmodify -Y EXTERNAL -H ldapi:/// -f /etc/openldap/changes.ldif

SASL/EXTERNAL authentication started

SASL username: gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth

SASL SSF: 0

modifying entry "olcDatabase={2}hdb,cn=config"


modifying entry "olcDatabase={2}hdb,cn=config"


modifying entry "olcDatabase={2}hdb,cn=config"


modifying entry "cn=config"


modifying entry "cn=config"


modifying entry "cn=config"


modifying entry "olcDatabase={1}monitor,cn=config"



```

創建/etc/openldap/base.ldif文件,並將下面的信息復制進去

```

[[email protected] ~]# vim /etc/openldap/base.ldif


dn: dc=node1,dc=com

dc: node1

objectClass: top

objectClass: domain


dn: ou=People,dc=node1,dc=com

ou: People

objectClass: top

objectClass: organizationalUnit


dn: ou=Group,dc=node1,dc=com

ou: Group

objectClass: top

objectClass: organizationalUnit

```

創建目錄的結構服務

```

[[email protected]e1 ~]# ldapadd -x -w node1 -D cn=Manager,dc=node1,dc=com -f /etc/openldap/base.ldif

adding new entry "dc=node1,dc=com"


adding new entry "ou=People,dc=node1,dc=com"


adding new entry "ou=Group,dc=node1,dc=com"



```

創建測試用的用戶

```

[[email protected] ~]# useradd -d /home/ldap ldapuser


```

設置帳戶的遷移

```

[[email protected] ~]# vim /usr/share/migrationtools/migrate_common.ph


71 $DEFAULT_MAIL_DOMAIN = "node1.com";

74 $DEFAULT_BASE = "dc=node1,dc=com";

```

將當前系統中的用戶遷移至目錄服務

```

[[email protected] ~]# cd /usr/share/migrationtools/

[[email protected] migrationtools]# grep ":10[0-9][0-9]" /etc/passwd > passwd

[[email protected] migrationtools]# ./migrate_passwd.pl passwd users.ldif

[[email protected] migrationtools]# ldapadd -x -w node1 -D cn=Manager,dc=node1,dc=com -f users.ldif

adding new entry "uid=ldapuser,ou=People,dc=node1,dc=com"

```

將當前系統中的用戶組遷移至目錄服務

```

[[email protected] migrationtools]# grep ":10[0-9][0-9]" /etc/group > group

[[email protected] migrationtools]# ./migrate_group.pl group groups.ldif

[[email protected] migrationtools]# ldapadd -x -w node1 -D cn=Manager,dc=node1,dc=com -f groups.ldif

adding new entry "cn=ldapuser,ou=Group,dc=node1,dc=com"



```

測試linuxprobe用戶的配置文件

```

[[email protected] ~]# ldapsearch -x cn=ldapuser -b dc=node1,dc=com

# extended LDIF

#

# LDAPv3

# base <dc=node1,dc=com> with scope subtree

# filter: cn=ldapuser

# requesting: ALL

#


# ldapuser, People, node1.com

dn: uid=ldapuser,ou=People,dc=node1,dc=com

uid: ldapuser

cn: ldapuser

objectClass: account

objectClass: posixAccount

objectClass: top

objectClass: shadowAccount

userPassword:: e2NyeXB0fSEh

shadowLastChange: 17327

shadowMin: 0

shadowMax: 99999

shadowWarning: 7

loginShell: /bin/bash

uidNumber: 1000

gidNumber: 1000

homeDirectory: /home/ldap


# ldapuser, Group, node1.com

dn: cn=ldapuser,ou=Group,dc=node1,dc=com

objectClass: posixGroup

objectClass: top

cn: ldapuser

userPassword:: e2NyeXB0fXg=

gidNumber: 1000


# search result

search: 2

result: 0 Success


# numResponses: 3

# numEntries: 2


```

安裝httpd服務程序

```

[[email protected] ~]# yum install -y httpd


```

將密鑰文件上傳至網站目錄並啟動httpd服務

```

[[email protected] ~]# cp /etc/openldap/certs/cert.pem /var/www/html/


[[email protected] ~]# systemctl restart httpd

[[email protected] ~]# systemctl enable httpd


```

配置防火墻和SELinux

```

關閉防火墻


[[email protected] ~]# systemctl stop firewalld


關閉SELinux


[[email protected] ~]# setenforce 0

```

在日誌記錄服務的配置文件中追加下面語句,並重啟日誌服務

```

[[email protected] ~]# vim /etc/rsyslog.conf

local4.* /var/log/ldap.log


[[email protected] ~]# systemctl restart rsyslog

```

配置LDAP客戶端

將LDAP服務端主機名與IP地址的解析記錄寫入到hosts文件裏

```

[[email protected] ~]# echo "192.168.9.203 node1.com" >> /etc/hosts

```

安裝相關的軟件包

```

[[email protected] ~]# yum install openldap-clients nss-pam-ldapd authconfig-gtk pam_krb5


```

運行系統認證工具,並填寫LDAP服務信息

```

[[email protected] ~]# system-config-authentication

```

技術分享

技術分享

技術分享


稍等片刻後,驗證本地是否已經有了ldapuser用戶

```

[[email protected] ~]# id ldapuser

uid=1000(ldapuser) gid=1000(ldapuser) 組=1000(ldapuser)

```

技術分享



>此時說明已經可以通過LDAP服務端驗證了,並且ldapuser用戶的帳號信息也不會保存在您本地的/etc/passwd文件中~


自動掛載用戶目錄

>雖然在客戶端已經能夠使用LDAP驗證帳戶了,但是當切換到ldapuser用戶時會提示沒有該用戶的家目錄

```

[[email protected] ~]# su - ldapuser

上一次登錄:三 5月 31 16:24:16 CST 2017:0 上

su: 警告:無法更改到 /home/ldap 目錄: 沒有那個文件或目錄

-bash-4.2$


```

技術分享


>原因是本機並沒有該用戶的家目錄,我們需要配置NFS服務將用戶的家目錄自動掛載過來


在LDAP服務端添加共享信息

```

[[email protected] ~]# vim /etc/exports


/home/ldap 192.168.9.206 (rw,sync,root_squash)


```

重啟nfs-server服務程序

```

[[email protected] ~]# systemctl restart nfs-server


```

註:如果出現如下錯誤

nfs-server服務起不來

```

[[email protected] ~]# systemctl restart nfs-server

Failed to restart nfs-server.service: Unit nfs-server.service failed to load: No such file or directory.


```

解決辦法

安裝autofs和nfs-utils

```

[[email protected] ~]# yum install -y autofs

[[email protected] ~]# yum install -y nfs-utils


```

再重啟nfs-server服務

```

[[email protected] ~]# systemctl restart rpcbind

[[email protected] ~]# systemctl enable rpcbind

[[email protected] ~]# systemctl restart nfs-server


```

在LDAP客戶端查看共享信息

```

[[email protected] ~]# showmount -e 192.168.9.203

Export list for 192.168.9.203:

/home/ldap (everyone)


```

將共享目錄掛載到本地

```

[[email protected] ~]# mkdir /home/ldap

[[email protected] ~]# mount -t nfs 192.168.9.203:/home/ldap/ /home/ldap/


```

再次嘗試切換到ldapuser用戶,這樣非常順利

```

[[email protected] ~]# su - ldapuser

上一次登錄:一 6月 12 16:16:49 CST 2017pts/0 上

[[email protected] ~]$ exit

登出

[[email protected] ~]#

```

設置為開機自動掛載

```

[[email protected] ~]# vim /etc/fstab

192.168.9.203:/home/ldap /home/ldap nfs defaults 0 0

```


#完



2017.10.10

----->唐傑<-----

本文出自 “新網學會博客” 博客,請務必保留此出處http://xwxhvip.blog.51cto.com/13020757/1971268

CentOS 7.0 部署OpenLDAP目錄服務