[hermit auto="0" loop="1" unexpand="1" fullheight="0"]collect#:193025074[/hermit]
《Linux就該這么學》最新正式版已出版上市,同學們可在線上京東網、當當網、淘寶網及亞馬遜等電商平臺購買。
*亦可就近在新華書店購買*
章節概述:
本章節將理解目錄服務的概念,學習OpenLdap服務程序與TLS加密協議的部署方法,并自動掛載用戶目錄。
通過部署目錄服務實現對系統帳戶的集中式管理,相比于X.500協議具有更快的查詢速度、更少的消耗資源等優勢。
23.1 了解目錄服務
回憶前面所學的章節,我們發現其實目錄可以被理解成是一種為查詢、瀏覽或搜索的數據庫,但數據庫又分為了目錄數據庫和關系數據庫,目錄數據庫主要用于存儲較小的信息(如姓名、電話、主機名等),同時具有很好的讀性能,但在寫性能方面比較差,所以不適合存放那些需要經常修改的數據。
目錄服務則是由目錄數據庫和一套能夠訪問和處理數據庫信息的協議組成的服務協議,用于集中的管理主機帳號密碼,員工名字等數據,大大的提升了管理工作效率。輕量級目錄訪問協議LDAP(Lightweight Directory Access Protocol)是在目錄訪問協議X.500的基礎上研發的,主要的優勢是:
X.500目錄協議功能非常臃腫,消耗大量資源,無法做到快速查詢且不支持TCP/IP協議網絡。
LDAP采用樹狀結構存儲數據(類似于前面學習的DNS服務程序),用于在IP網絡層面實現對分布式目錄的訪問和管理操作,條目是LDAP協議中最基本的元素,可以想象成字典中的單詞或者數據庫中的記錄,通常對LDAP服務程序的添加、刪除、更改、搜索都是以條目為基本對象的。
LDAP樹狀結構存儲數據
dn:每個條目的唯一標識符,如上圖中linuxprobe的dn值是:
cn=linuxprobe,ou=marketing,ou=people,dc=mydomain,dc=orgrdn:一般為dn值中最左側的部分,如上圖中linuxprobe的rdn值是:
cn=linuxprobebase DN:此為基準DN值,表示頂層的根部,上圖中的base DN值是:
dc=mydomain,dc=org
而每個條目可以有多個屬性(如姓名、地址、電話等),每個屬性中會保存著對象名稱與對應值,LDAP已經為運維人員對常見的對象定義了屬性,其中有:
屬性名稱 | 屬性別名 | 語法 | 描述 | 值(舉例) |
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 | 內置屬性 | organizationalPerson |
出現問題?大膽提問!
因讀者們硬件不同或操作錯誤都可能導致實驗配置出錯,請耐心再仔細看看操作步驟吧,不要氣餒~
Linux技術交流請加A群:560843(滿),B群:340829(推薦),C群:463590(推薦),點此查看全國群。
*本群特色:通過口令驗證確保每一個群員都是《Linux就該這么學》的讀者,答疑更有針對性,不定期免費領取定制禮品。
23.2 目錄服務實驗
OpenLdap是基于LDAP協議的開源程序,它的程序名稱叫做slapd,本次實驗需要用到兩臺主機:
主機名稱 | 操作系統 | IP地址 |
LDAP服務端 (instructor.linuxprobe.com) |
紅帽RHEL7操作系統 | 192.168.10.10 |
LDAP客戶端 | 紅帽RHEL7操作系統 | 192.168.10.20 |
23.2.1 配置LDAP服務端
安裝openldap與相關的軟件包:
[[email protected] ~]# yum install -y openldap openldap-clients openldap-servers migrationtools Loaded plugins: langpacks, product-id, subscription-manager ………………省略部分安裝過程……………… Installing: migrationtools noarch 47-15.el7 rhel7 26 k openldap-clients x86_64 2.4.39-3.el7 rhel7 183 k openldap-servers x86_64 2.4.39-3.el7 rhel7 2.1 M ………………省略部分安裝過程……………… Complete!
生成密鑰文件(記下生成出的值,后面要用):
[[email protected] ~]# slappasswd -s linuxprobe -n > /etc/openldap/passwd [[email protected] ~]# cat /etc/openldap/passwd {SSHA}v/GJvGG8SbIuCxhfTDVhkmWEuz2afNIR
寫入一條主機與IP地址的解析記錄:
[[email protected] ~]# echo "192.168.10.10 instructor.linuxprobe.com" >> /etc/hosts
因為LDAP目錄服務是以明文的方式在網絡中傳輸數據的(包括密碼),這樣真的很不安全,所以我們采用TLS加密機制來解決這個問題,使用openssl工具生成X509格式的證書文件(有效期為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 hostname) []:instructor.linuxprobe.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 total 8 drwxr-xr-x. 2 root root 36 Oct 5 13:41 . drwxr-xr-x. 5 root root 100 Oct 5 13:39 .. -rw-r--r--. 1 ldap ldap 1318 Oct 5 13:41 cert.pem -rw-------. 1 ldap ldap 1704 Oct 5 13:41 priv.pem
復制一份LDAP的配置模板:
[[email protected] ~]# cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
生成數據庫文件(不用擔心報錯信息):
[[email protected] ~]# slaptest 5610aaa9 hdb_db_open: database "dc=my-domain,dc=com": db_open(/var/lib/ldap/id2entry.bdb) failed: No such file or directory (2). 5610aaa9 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數據庫的所屬主與組:
[[email protected] ~]# chown ldap:ldap /var/lib/ldap/*
啟動slapd服務程序并設置為開機啟動:
[root@linuxprobe ~]# systemctl enable slapd
ln -s '/usr/lib/systemd/system/slapd.service' '/etc/systemd/system/multi-user.target.wants/slapd.service'
在LDAP目錄服務中使用LDIF(LDAP Interchange?Format)格式來保存信息,而LDIF是一種標準的文本文件且可以隨意的導入導出,所以我們需要有一種“格式”標準化LDIF文件的寫法,這中格式叫做“schema”,schema用于指定一個目錄中所包含對象的類型,以及每一個類型中的可選屬性,我們可以將schema理解為面向對象程序設計中的“類”,通過“類”定義出具體的對象,因此其實LDIF數據條目則都是通過schema數據模型創建出來的具體對象:
ldapadd命令用于將LDIF文件導入到目錄服務數據庫中,格式為:“ldapadd [參數] LDIF文件”。
參數 | 作用 |
-x | 進行簡單認證。 |
-D | 用于綁定服務器的dn。 |
-h: | 目錄服務的地址。 |
-w: | 綁定dn的密碼。 |
-f: | 使用LDIF文件進行條目添加的文件。 |
添加cosine和nis模塊:
[[email protected] ~]# cd /etc/openldap/schema/ [[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" [[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文件,并將下面的信息復制進去(注意有一處要修改的地方):
[[email protected] ~]# vim /etc/openldap/changes.ldif dn: olcDatabase={2}hdb,cn=config changetype: modify replace: olcSuffix olcSuffix: dc=linuxprobe,dc=com dn: olcDatabase={2}hdb,cn=config changetype: modify replace: olcRootDN olcRootDN: cn=Manager,dc=linuxprobe,dc=com dn: olcDatabase={2}hdb,cn=config changetype: modify replace: olcRootPW olcRootPW: 此處輸入之前生成的密碼(如{SSHA}v/GJvGG8SbIuCxhfTDVhkmWEuz2afNIR) 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=linuxprobe,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=linuxprobe,dc=com dc: linuxprobe objectClass: top objectClass: domain dn: ou=People,dc=linuxprobe,dc=com ou: People objectClass: top objectClass: organizationalUnit dn: ou=Group,dc=linuxprobe,dc=com ou: Group objectClass: top objectClass: organizationalUnit
創建目錄的結構服務:
adding new entry "dc=linuxprobe,dc=com"
adding new entry "ou=People,dc=linuxprobe,dc=com"
adding new entry "ou=Group,dc=linuxprobe,dc=com"
創建測試用的用戶:
[[email protected] ~]# useradd -d /home/ldap ldapuser
設置帳戶的遷移(修改第71與74行):
[[email protected] ~]# vim /usr/share/migrationtools/migrate_common.ph $DEFAULT_MAIL_DOMAIN = "linuxprobe.com"; $DEFAULT_BASE = "dc=linuxprobe,dc=com";
將當前系統中的用戶遷移至目錄服務:
[root@linuxprobe migrationtools]# grep ":10[0-9][0-9]" /etc/passwd > passwd
[root@linuxprobe migrationtools]# ./migrate_passwd.pl passwd users.ldif
[root@linuxprobe migrationtools]# ldapadd -x -w linuxprobe -D cn=Manager,dc=linuxprobe,dc=com -f users.ldif
adding new entry "uid=linuxprobe,ou=People,dc=linuxprobe,dc=com"
adding new entry "uid=ldapuser,ou=People,dc=linuxprobe,dc=com"
將當前系統中的用戶組遷移至目錄服務:
[root@linuxprobe migrationtools]# ./migrate_group.pl group groups.ldif
[root@linuxprobe migrationtools]# ldapadd -x -w linuxprobe -D cn=Manager,dc=linuxprobe,dc=com -f groups.ldif
adding new entry "cn=linuxprobe,ou=Group,dc=linuxprobe,dc=com"
adding new entry "cn=ldapuser,ou=Group,dc=linuxprobe,dc=com"
測試linuxprobe用戶的配置文件:
[[email protected] ~]# ldapsearch -x cn=ldapuser -b dc=linuxprobe,dc=com # extended LDIF # # LDAPv3 # base <dc=linuxprobe,dc=com> with scope subtree # filter: cn=ldapuser # requesting: ALL # # ldapuser, People, linuxprobe.com dn: uid=ldapuser,ou=People,dc=linuxprobe,dc=com uid: ldapuser cn: ldapuser objectClass: account objectClass: posixAccount objectClass: top objectClass: shadowAccount userPassword:: e2NyeXB0fSQ2JFdtcXFveHFIJFFNaU1pZDAuL01KLnBrR1ZKLkdVSVlWalguTXh xLlB5Uk1IeGJseGdkVTBwOUxwcTBJT2huYnkwNFkzdXh1Zi9QaWFpUUtlLk0wUHdQNFpxRXJQV0cv shadowLastChange: 16713 shadowMin: 0 shadowMax: 99999 shadowWarning: 7 loginShell: /bin/bash uidNumber: 1001 gidNumber: 1001 homeDirectory: /home/ldapuser # ldapuser, Group, linuxprobe.com dn: cn=ldapuser,ou=Group,dc=linuxprobe,dc=com objectClass: posixGroup objectClass: top cn: ldapuser userPassword:: e2NyeXB0fXg= gidNumber: 1001 # search result search: 2 result: 0 Success # numResponses: 3 # numEntries: 2
安裝httpd服務程序:
[[email protected] ~]# yum install httpd Loaded plugins: langpacks, product-id, subscription-manager ………………省略部分安裝過程……………… Installing: httpd x86_64 2.4.6-17.el7 rhel7 1.2 M Installing for dependencies: apr x86_64 1.4.8-3.el7 rhel7 103 k apr-util x86_64 1.5.2-6.el7 rhel7 92 k httpd-tools x86_64 2.4.6-17.el7 rhel7 77 k mailcap noarch 2.1.41-2.el7 rhel7 31 k ………………省略部分安裝過程……………… Complete!
將密鑰文件上傳至網站目錄:
[[email protected] ~]# cp /etc/openldap/certs/cert.pem /var/www/html
將httpd服務程序重啟,并添加到開機啟動項:
[root@linuxprobe ~]# systemctl enable httpd
ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'
清空防火墻的規則并保存狀態:
[[email protected] ~]# iptables -F success [[email protected] ~]# service iptables save success
在日志記錄服務的配置文件中追加下面語句,并重啟日志服務:
[[email protected] ~]# vim /etc/rsyslog.conf local4.* /var/log/ldap.log [[email protected] ~]# systemctl restart rsyslog
出現問題?大膽提問!
因讀者們硬件不同或操作錯誤都可能導致實驗配置出錯,請耐心再仔細看看操作步驟吧,不要氣餒~
Linux技術交流請加A群:560843(滿),B群:340829(推薦),C群:463590(推薦),點此查看全國群。
*本群特色:通過口令驗證確保每一個群員都是《Linux就該這么學》的讀者,答疑更有針對性,不定期免費領取定制禮品。
23.2.2 配置LDAP客戶端
將LDAP服務端主機名與IP地址的解析記錄寫入:
[[email protected] ~]# echo "192.168.10.10 instructor.linuxprobe.com" >> /etc/hosts
安裝相關的軟件包:
[[email protected] Desktop]# yum install openldap-clients nss-pam-ldapd authconfig-gtk pam_krb5 Loaded plugins: langpacks, product-id, subscription-manager ………………省略部分的安裝過程……………… Installing: authconfig-gtk x86_64 6.2.8-8.el7 rhel 105 k nss-pam-ldapd x86_64 0.8.13-8.el7 rhel 159 k openldap-clients x86_64 2.4.39-3.el7 rhel 183 k pam_krb5 x86_64 2.4.8-4.el7 rhel 158 k Installing for dependencies: nscd x86_64 2.17-55.el7 rhel 250 k ………………省略部分的安裝過程……………… Complete!
運行系統認證工具,并填寫LDAP服務信息:
[[email protected] ~]# system-config-authentication
填寫證書地址:
稍等片刻后,驗證本地是否已經有了ldapuser用戶:
[[email protected] ~]# id ldapuser uid=1001(ldapuser) gid=1001(ldapuser) groups=1001(ldapuser)
此時說明已經可以通過LDAP服務端驗證了,并且ldapuser用戶的帳號信息也不會保存在您本地的/etc/passwd文件中~
23.3 自動掛載用戶目錄
雖然在客戶端已經能夠使用LDAP驗證帳戶了,但是當切換到ldapuser用戶時會提示沒有該用戶的家目錄:
[[email protected] ~]# su - ldapuser su: warning: cannot change directory to /home/ldapuser: No such file or directory mkdir: cannot create directory '/home/ldapuser': Permission denied
原因是本機并沒有該用戶的家目錄,我們需要配置NFS服務將用戶的家目錄自動掛載過來:
在LDAP服務端添加共享信息(NFS服務程序已經默認安裝,我們之前學過還記得嗎?):
[[email protected] ~]# vim /etc/exports /home/ldap 192.168.10.20 (rw,sync,root_squash)
重啟nfs-server服務程序:
[[email protected] ~]# systemctl restart nfs-server
在LDAP客戶端查看共享信息:
[[email protected] ldap]# showmount -e 192.168.10.10 Export list for 192.168.10.10: /home/ldap 192.168.10.20
將共享目錄掛載到本地:
[[email protected] ~]# mkdir /home/ldap [[email protected] ldap]# mount -t nfs 192.168.10.10:/home/ldap /home/ldap
再次嘗試切換到ldapuser用戶,這樣非常順利:
[[email protected] ldap]# su - ldapuser Last login: Tue Oct 6 11:51:25 CST 2015 on pts/3 [[email protected] ~]$
設置為開機自動掛載:
[[email protected] ~]# vim /etc/fstab 192.168.10.10:/home/ldap /home/ldap nfs defaults 0 0
出現問題?大膽提問!
因讀者們硬件不同或操作錯誤都可能導致實驗配置出錯,請耐心再仔細看看操作步驟吧,不要氣餒~
Linux技術交流請加A群:560843(滿),B群:340829(推薦),點此查看全國群。
*本群特色:通過口令驗證確保每一個群員都是《Linux就該這么學》的讀者,答疑更有針對性,不定期免費領取定制禮品。
本章節的復習作業(答案就在問題的下一行哦,用鼠標選中即可看到的~)
1:LDAP目錄服務的特點是?
答案:消耗資源少,適合存儲較小的信息、具有很好的讀性能以及支持TCP/IP協議網絡。
2:目錄服務的唯一標識符dn的作用是?
答案:用于定義某個條目或屬性的唯一性。
3:為什么需要啟用TLS加密?
答案:因為openldap服務程序默認使用明文傳送數據(包括密碼)。
4:ldapadd命令的作用是?
答案:ldapadd命令用于將LDIF文件導入到目錄服務數據庫中。
5:部署目錄服務后配置nfs服務的作用是?
答案:掛載用戶的家目錄。
本文原創地址:http://www.52437949.com/chapter-23.html編輯:劉遄,審核員:暫無